HDU杭电1052 Tian Ji -- The Horse Racing解题报告

本人第一次写博客,希望各位大神多多指导与包涵,不足的地方还请指出,新手在此谢过啦!!!

题目描述:

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 26442    Accepted Submission(s): 7799

Problem Description

Here is a famous story in Chinese history.

"That was about 2300 years ago. General Tian Ji was a high official in the country Qi. He likes to play horse racing with the king and others."

"Both of Tian and the king have three horses in different classes, namely, regular, plus, and super. The rule is to have three rounds in a match; each of the horses must be used in one round. The winner of a single round takes two hundred silver dollars from the loser."

"Being the most powerful man in the country, the king has so nice horses that in each class his horse is better than Tian's. As a result, each time the king takes six hundred silver dollars from Tian."

"Tian Ji was not happy about that, until he met Sun Bin, one of the most famous generals in Chinese history. Using a little trick due to Sun, Tian Ji brought home two hundred silver dollars and such a grace in the next match."

"It was a rather simple trick. Using his regular class horse race against the super class from the king, they will certainly lose that round. But then his plus beat the king's regular, and his super beat the king's plus. What a simple trick. And how do you think of Tian Ji, the high ranked official in China?"

Were Tian Ji lives in nowadays, he will certainly laugh at himself. Even more, were he sitting in the ACM contest right now, he may discover that the horse racing problem can be simply viewed as finding the maximum matching in a bipartite graph. Draw Tian's horses on one side, and the king's horses on the other. Whenever one of Tian's horses can beat one from the king, we draw an edge between them, meaning we wish to establish this pair. Then, the problem of winning as many rounds as possible is just to find the maximum matching in this graph. If there are ties, the problem becomes more complicated, he needs to assign weights 0, 1, or -1 to all the possible edges, and find a maximum weighted perfect matching...

However, the horse racing problem is a very special case of bipartite matching. The graph is decided by the speed of the horses --- a vertex of higher speed always beat a vertex of lower speed. In this case, the weighted bipartite matching algorithm is a too advanced tool to deal with the problem.

In this problem, you are asked to write a program to solve this special case of matching problem.

Input

The input consists of up to 50 test cases. Each case starts with a positive integer n (n <= 1000) on the first line, which is the number of horses on each side. The next n integers on the second line are the speeds of Tian’s horses. Then the next n integers on the third line are the speeds of the king’s horses. The input ends with a line that has a single 0 after the last test case.

Output

For each input case, output a line containing a single number, which is the maximum money Tian Ji will get, in silver dollars.

Sample Input

3

92 83 71

95 87 74

2

20 20

20 20

2

20 19

22 18

0

本题大意就是先输入比赛马匹的个数如果为0,结束程序,然后依次输入田忌与齐桓王的马匹速度,每赢一局输的须向赢得支付200元,最后输出总共田忌赢得钱数或输的钱数。。。

附上个人认为比较刁钻的数据:

10

12 3 4 5 23 5 23 6 12 8

23 6 7 8 8 9 23 4 6 3

附上AC代码:

//主题是运用C语言,但是运用了c++的排序手法

#include

#include

using namespace std;

int main()

{

int a[1050];

int b[1050];

int m;

while(scanf("%d",&m)!=EOF)

{

if(m==0)

break;

for(int i=0;i

{

scanf("%d",&a[i]);

}

for(int i=0;i

{

scanf("%d",&b[i]);

}

sort(a,a+m);

sort(b,b+m);

reverse(a,a+m);

reverse(b,b+m);

int tk=0,tm=m-1,wk=0,wm=m-1,win=0;

//定义变量;

while(m--)

{

if(a[tk]>b[wk])

{

win++;

wk++;

tk++;

}

else if(a[tm]>b[wm])

{

win++;

tm--;

wm--;

}

else if(a[tm]

{

win--;

tm--;

wk++;

}

//可能存在平局的现象所以应在此加上第三个条件

}

printf("%d\n",win*200);

}

}

错误思路:先将田忌和齐桓王的马分别进行降序,再拿田忌的最厉害的马跟齐桓王的马比,对齐桓王的马进行循环,当出现田忌的马可以战胜齐桓王的马时便win++,接着将比赛过后的马进行赋值为0;break跳出齐桓王循环,对田忌进行下一次循环,若该匹马不等于0时,在进行判断。同时还得判断平局的次数,最后总马匹量-胜利局数减去平局即为输的次数,最后win*200;结束

实际上,这样做并不能得到真正的结果,

10

12 3 4 5 23 5 23 6 12 8

23 6 7 8 8 9 23 4 6 3

以上实验思路所写代码实现上组数据时得到的结果是400,而正确答案是1000;

因为在此田忌的最差的马并没有实现自己的最大价值。

正确解题思路如下:

先分别对田忌与齐桓王的马匹进行降序排,先拿田忌最厉害的马跟齐桓王的马进行比,假使田忌可以战胜,则win++,假使不能战胜齐桓王的马匹,则用田忌最差的马拿出来进行比较,跟齐桓王的最差的马进行比较(能赢则赢,尽量减少平局的次数,为什么要和齐桓王的最差的马进行比较,想一下就知道为什么啦),如果田忌最差的马不能战胜齐桓王最差的马(开始献身的价值到了)用田忌最差的马跟齐桓王最厉害的马进行比较【因此在此有三个判断,每次只执行一次判断,每次判断后应进行相应数据的递减或递增】

此句话不能直接写成else{..........

....

}

因为到最后一次如果出现平局的话win是不能减一的;

到最后循环结束,输出win*200;

该题主要还是得看想法,理解田忌赛马怎样用编程实现,实现每匹马的最大值!!!!!!!

杭电1042c语言循环,HDU杭电1052 Tian Ji - The Horse Racing答题报告相关推荐

  1. HDU杭电1052 Tian Ji -- The Horse Racing解题报告

    本人第一次写博客,希望各位大神多多指导与包涵,不足的地方还请指出,新手在此谢过啦!!! 题目描述: Time Limit: 2000/1000 MS (Java/Others)    Memory L ...

  2. HDU 1052 Tian Ji -- The Horse Racing

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1052 题意:田忌赛马,贪心 #include<iostream> #include<al ...

  3. hdu 1052 Tian Ji -- The Horse Racing

    Tian Ji -- The Horse Racing Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Jav ...

  4. 杭电oj1052题:Tian Ji -- The Horse Racing

    题目:Here is a famous story in Chinese history."That was about 2300 years ago. General Tian Ji wa ...

  5. poj hdu Tian Ji -- The Horse Racing 贪心

    http://acm.hdu.edu.cn/showproblem.php?pid=1052 田忌赛马 先把田忌和国王的马排序. 每次取田忌的最快的马与国王最快的马比较,有三种情况. 一,田忌最快的马 ...

  6. 每周一题3_杭电ACM_Tian Ji -- The Horse Racing

    Tian Ji – The Horse Racing 文章目录 Tian Ji -- The Horse Racing 原题描述 测试代码 思路 附: 题目来源:杭电ACM 1.3.1 原题描述 Pr ...

  7. 杭电1052-Tian Ji -- The Horse Racing 贪心算法(有思路和注释)

    Problem Description Here is a famous story in Chinese history. "That was about 2300 years ago. ...

  8. 杭电oj2033c语言,C语言训练之杭电OJ

    C语言训练之杭电OJ 一.水仙花数 Problem Description问题描述 Input输入 Output输出 Sample Input输入样例 Sample Output输出样例 代码如下: ...

  9. 弘辽科技:经济内循环助推电商发展,解读未来电商新趋势

    原标题<弘辽科技:经济内循环助推电商发展,解读未来电商新趋势> 2020双十一已经落下帷幕,今年双十一不仅创多个新高,而且加入了多种新玩法,"尾款人"热词就这么横空出世 ...

最新文章

  1. ws配置 zuul_SpringCloud系列研究---服务网关zuul
  2. Java学习笔记#数组的初始化方式
  3. es6 --- 内置的Symbol值
  4. 运算符与,|与||的区别
  5. C语言指针学习(续)
  6. B75经典门户商业版discuz模板
  7. 主席树【bzoj3524(p3567)】[POI2014]Couriers
  8. 使用CPU时间戳进行高精度计时
  9. MySQL用户权限(Host,User,Password)管理(mysql.user)
  10. 简明python教程电子书下载_简明Python教程PDF
  11. xcode 可以打开xmind_思维导图,原来Xmind这么强大
  12. 企业发卡系统源码/带有代理功能发卡平台源码
  13. [HAOI2014] 贴海报
  14. 第7周编程题在线测试
  15. 沙普利算法的java实现_盖尔-沙普利算法告诉你,你的对象在哪里?
  16. 使用纯css做一个播放器
  17. 本地项目连接虚拟机的数据库oracle
  18. FAST AND HIGH-QUALITY SINGING VOICE SYNTHESIS SYSTEM BASED ON CONVOLUTIONAL NEURAL NETWORKS
  19. 好论文是如何炼成的-林宙辰
  20. 基于at89c51单片机的led数字倒计时器设计c语言,课程设计(论文)-基于AT89C51单片机的LED数字倒计时器设计精选.docx...

热门文章

  1. linux安装压力测试工具vegeta
  2. 嗑瓜子杂谈篇:参与承办2018数博会是一种怎样的体验
  3. 如何将eps文件转换为原visio文件
  4. java基础之Scanner close方法
  5. 强大的Android系统,可自由修改手机型号、SDK版本号等信息。
  6. input文本输入框的type类型
  7. 百度地图——poi搜索
  8. 浅析Docker容器的应用场景
  9. 408数据结构(王道版+邓俊辉版)
  10. 软件打包安装工具第四篇(打包工具原理及技术)