Tian Ji -- The Horse Racing

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

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
 
Sample Output
200
0
0
 
Source
 

引用了古代田忌赛马的故事,不过此题将这个故事的三马,扩充到了n配马,面对这样一个比赛,解题方法多样。

方法一:

我们不妨先让这些马按从小到大的顺序排序,比如样列中的数据一:

3
92 83 71   ---》  71 83 92
95 87 74            74 87 95 
由于得到了有序的马匹,所以只需要逐步将将角标逐步的挪一下然后比较这个过程的最大值即为所求
  答案:因而我们不妨,设置一个循环数组什么的,然后就可以模拟上面的东西了..
代码如下:
   当然可以使用mode来节约空间开销,但是明显增大了时间开销!!!(所以不推荐)
 /*coder hdu 1055@Gxjun*/
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
int aa[*maxn],bb[maxn];
int main()
{
int n,i,j,count,cnt;
while(scanf("%d",&n)!=EOF&&!=n){
for(i=;i<n;i++) scanf("%d",aa+i);
for(i=;i<n;i++) scanf("%d",bb+i);
sort(aa,aa+n); //从大到小排序 general
sort(bb,bb+n); //从小到大排序 king
for (i=;i<n;i++) aa[i+n]=aa[i];
count=-0x3f3f3f3f;
for(i=;i<n;i++){
cnt=;
for(j=;j<n;j++){
if(aa[i+j]<bb[j]) cnt--;
else if(aa[i+j]>bb[j]) cnt++;
}
if(count<cnt) count=cnt;
}
printf("%d\n",count*);
}
return ;
}

对于这道题可以用二分匹配,但是tle ,面对这一个问题,我们还可以使用贪心什么的求解,最终得到我们所需要的答案!

反正总之一句话,如果田忌的慢马比国王的慢马快,直接比较,否则这个慢马
        毫无价值,用来当炮灰,去跟国王的快马相比,如果田忌的快马比国王快,直接比较
       否则这个快马也是个炮灰,只好去跟国王后边的慢马比,但是到底跟那个比可不一定,
       所以不用去一个一个的试,当快马比不过的时候,用慢马去当炮灰,然后用快马跟国王
      的下一匹快马比较

HDUOJ-------1052Tian Ji -- The Horse Racing(田忌赛马)的更多相关文章

  1. 【贪心】[hdu1052]Tian Ji -- The Horse Racing(田忌赛马)[c++]

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

  2. hdu 1052 Tian Ji -- The Horse Racing (田忌赛马)

    Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  3. [HDU1052]Tian Ji -- The Horse Racing(田忌赛马)

    题目大意:田忌赛马问题,给出田忌和齐威王的马的数量$n$和每匹马的速度$v$,求田忌最多赢齐威王多少钱(赢一局得200,输一局扣200,平局不得不扣). 思路:贪心. 1.若田忌最慢的马可以战胜齐王最 ...

  4. 【OpenJ_Bailian - 2287】Tian Ji -- The Horse Racing (贪心)

    Tian Ji -- The Horse Racing 田忌赛马,还是English,要不是看题目,我都被原题整懵了,直接上Chinese吧 Descriptions: 田忌和齐王赛马,他们各有n匹马 ...

  5. hdoj 1052 Tian Ji -- The Horse Racing【田忌赛马】 【贪心】

    思路:先按从小到大排序, 然后从最快的開始比(如果i, j 是最慢的一端, flag1, flag2是最快的一端 ),田的最快的大于king的 则比較,如果等于然后推断,有三种情况: 一:大于则比較, ...

  6. Hdu 1052 Tian Ji -- The Horse Racing

    Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  7. Tian Ji -- The Horse Racing

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

  8. UVA 1344 Tian Ji -- The Horse Racing

    Tian Ji -- The Horse Racing Here is a famous story in Chinese history. That was about 2300 years ago ...

  9. hdu-1052-Tian Ji -- The Horse Racing(经典)

    /* hdu-1052 Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/3 ...

随机推荐

  1. CHECKBOX_CHECKED built-in in Oracle D2k Forms

    CHECKBOX_CHECKED built-in in Oracle D2k Forms DescriptionA call to the CHECKBOX_CHECKED function ret ...

  2. 使用OmniGraffle制作原型图

    原型图设计是一个艺术创作的过程,所以我们应当使用能够提高工作效率.激发创作灵感的工具,让工具为创作服务,而不是为创作去学习如何使用工具.从这一点上说,我觉得Mac下的很多软件做的非常好,OmniGra ...

  3. CMD方式修改MySQL的root用户密码

    1.CMD下,进行mysql的bin目录下; 2.输入mysql -u root -p,输入旧密码,进入mysql状态: 3.MySQL>use MySQL; 4.update user set ...

  4. [CF738D]Sea Battle(贪心)

    题目链接:http://codeforces.com/contest/738/problem/D 题意:1*n的格子里有a条长为b的船.有一个人射了k发子弹都没打中船,现在问最少再打多少次一定能保证射 ...

  5. 【原创】pads layout 画多边形copper,出现Self-Intersecting Polygon,解决办法

    在做线性位移传感器的电路板时,需要在一个很小的多边形Copper操作,总是提示“Self-Intersecting Polygon”报错,意思是outline线自身交叉,换句话说就是线宽与多边形尺寸没 ...

  6. 验证SMB登入

    使用SMB登入扫描器对大量主机的用户名和口令进行猜解,不过扫描动静很大,容易被察觉,而且每一次登入尝试都会被扫描的主机系统日志记录下来,留下痕迹不建议使用. 实例 第一步: msf > use ...

  7. 手机页面中的meta标签

    以前看书的时候,觉得meta标签就只有一个charset对于我来说是有用的.前段时间有个学弟让我写个手机版的网页,我才知道原来meta标签有那么多学问. meta指元素可提供有关页面的元信息(meta ...

  8. spring的下载地址(转)

    http://hi.baidu.com/jaimy_jie/item/d699cecdc5d3a354bdef6990 SPRING官方网站改版后,建议都是通过 Maven和Gradle下载,对不使用 ...

  9. winform打开唯一窗体、构造函数传值

    制作登入窗体: 制作一个登陆窗体,实现点击按钮关闭此窗体并打开另一个窗体 直接在按钮点击事件中,实例化一个想要打开的窗体 使用show方法打开,并把登陆窗体的visible属性改为false Form ...

  10. 进击的Hybrid App,量身定做缓存机制

    引用张图,简单粗俗的解释下 Native App.Web App 和 Hybrid App Navtie App: 使用平台系统提供的原生语言来编写的 App,如果Android用java,ios用o ...