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

题目描述:

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<iostream>

#include<algorithm>

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<m;i++)

        {

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

        }

        for(int i=0;i<m; 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]<b[wk])

                {

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;

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

转载于:https://www.cnblogs.com/ZHOUXIAOHAO/p/5722702.html

HDU杭电1052 Tian Ji -- The Horse Racing解题报告相关推荐

  1. 杭电1042c语言循环,HDU杭电1052 Tian Ji - The Horse Racing答题报告

    HDU杭电1052 Tian Ji -- The Horse Racing解题报告 本人第一次写博客,希望各位大神多多指导与包涵,不足的地方还请指出,新手在此谢过啦!!! 题目描述: Time Lim ...

  2. 贪心法田忌赛马问题Java代码,hdoj 1052 Tian Ji - The Horse Racing【田忌赛马】 【贪心】...

    hdoj 1052 Tian Ji -- The Horse Racing[田忌赛马] [贪心] 思路:先按从小到大排序, 然后从最快的开始比(假设i, j 是最慢的一端, flag1, flag2是 ...

  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. HDU 1052 Tian Ji -- The Horse Racing

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

  5. hdu-1052 Tian Ji -- The Horse Racing

    HOT!!! 欢迎参加"金山西山居-2013创意游戏程序挑战赛"! Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Ja ...

  6. Tian Ji -- The Horse Racing(田忌赛马)/贪心算法

    Tian Ji – The Horse Racing(田忌赛马) 田忌赛马的故事 (可以直接看题) Here is a famous story in Chinese history. "T ...

  7. OpenJudge Tian Ji -- The Horse Racing

    目录 Tian Ji -- The Horse Racing 要求: 描述: 输入 : 输出: 样例输入: 样例输出: 问题分析: 情况一: 情况二: 情况三: 最终代码: 总结: 其他思路: Tia ...

  8. Tian Ji -- The Horse Racing(贪心+STL)

    Problem:Tian Ji – The Horse Racing Description: Here is a famous story in Chinese history. "Tha ...

  9. 贪心算法 003:Tian Ji -- The Horse Racing

    003:Tian Ji – The Horse Racing 总时间限制: 5000ms 内存限制: 65536kB 描述 Here is a famous story in Chinese hist ...

最新文章

  1. react-native 打包apk
  2. linux下C/C++编译时系统搜索 include 和 链接库 文件路径的指定
  3. 浅谈一下我了解的PWA
  4. linux下jboss分析helloword,JBoss 中运行servlet 的helloworld程序(转)
  5. python变量名称跟着循环_python在循环中存储每次迭代使用不同名称的输入变量
  6. Java Singleton类中的线程安全性的示例代码
  7. 应用市场中的应用转让
  8. 在VB.NET中初始化网格实现方法知识讲解
  9. fread 单独测试没有问题 在正式项目里面丢数据 可能是系统资源不足 预读出了问题
  10. UVA722 LA5359 Lakes【DFS】
  11. 五秒原则,做一件事之前数 5 秒,1,2,3,4,5 立马去做。比如睡觉:数五秒,立马放下手机,闭眼。...
  12. 我是如何在GitHub上开源一个项目的(截图说明) (VS2010可以安装git插件)
  13. GUI学习之二十一——QSlider、QScroll、QDial学习总结
  14. 编写树莓派引脚驱动代码
  15. gson读取json字符串_通过Gson解析Json数据
  16. 软件安全之动态链接库的使用 Libzplay 播放音乐
  17. 三目运算符 c语言求最小值,三目运算符
  18. 最长的指定瑕疵度的元音字串 —— 最优解法(C++实现)
  19. 海盗比酒量--蓝桥杯
  20. 计算器算贝塞尔公式_买车贷款怎么算利息?

热门文章

  1. automake创建Makefile
  2. github中fork,clone,push,pull request的简单理解
  3. Python画图如何显示各点横纵坐标(x, y)?
  4. 混战数月后,国产AI办公神器彻底学会“自己办公”了吗?
  5. ant design mobile listView 使用
  6. 【八芒星计划】绕过canary
  7. STM32 —— 串口通讯
  8. Archlinux安装xfce4桌面及美化流程
  9. 计算机毕业设计ssm人力资源管理系统0600t系统+程序+源码+lw+远程部署
  10. iOS----------检测域名是否支持ipv6