Tian Ji -- The Horse Racing

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

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

题意:田忌赛马,知道了田忌的n只马的速度和齐王的n只马的速度,赢一场得到200块,输一场失去200块,平局保持不变。田忌想要更多钱。输出田忌最后最多能拿到多少钱。

题解:把田忌和王的马按速度从大到小排序。之后就是超多的细节。。。。。。。。。具体看代码注释

 #include<bits/stdc++.h>
using namespace std;
int a[],b[];
bool cmp(int x,int y)
{
return x>y;
}
int main()
{
int n,t1,t2,k1,k2;
while(~scanf("%d",&n),n)
{
memset(a,,sizeof(a));
memset(b,,sizeof(b));
for(int i=;i<n;i++)
{
scanf("%d",&a[i]);
}
for(int i=;i<n;i++)
{
scanf("%d",&b[i]);
}
sort(a,a+n,cmp);//按速度从大到小排序
sort(b,b+n,cmp);
t1=k1=;//田忌和齐王最强的马的下标
t2=k2=n-;//田忌和齐王最差的马的下标
int ans=;
while(t1<=t2)
{
if(a[t1]>b[k1])//田忌的最强比王的最强更强,
{
ans+=;t1++;k1++;
}
else if(a[t1]<b[k1])//田忌的最强比王的最强弱,用最弱的去比
{
ans-=;t2--;k1++;
}
else//最强的马速度一样
{
if(a[t2]>b[k2])//田忌最慢的马比王最慢的马快
{
ans+=;t2--;k2--;
}
else if(a[t2]==b[k1])//田忌最慢的马和王最快的马速度一样,平局
{
k1++;t2--;
}
else//田忌最慢的马和齐王最快的马比
{
ans-=;k1++;t2--;
}
}
}
printf("%d\n",ans);
}
return ;
}

最新文章

  1. 2000条你应知的WPF小姿势 基础篇&lt;1-7&gt;
  2. Windows Phone 8.1 新特性 - 控件之FlipView
  3. IOS中货币高精度要求使用NSDecialNumber、
  4. 项目中常用SQL语句总结
  5. MongoDB基本命令随便敲敲
  6. bokeh-scala
  7. 在mac上访问自带服务器权限问题
  8. USACO Longest Prefix 【水】
  9. 30第二建筑Github Page
  10. Class Model of Quick Time Plugin
  11. java代码之美(2)---Java8 Stream
  12. Java开发笔记(十四)几种运算符的优先级顺序
  13. flex外包团队—北京动点软件:推荐一本不错的Flex书籍
  14. Webstorm使用教程详解
  15. .NET 获取类内成员的名称,以字符串形式显示
  16. 10分钟让你明白MySQL是如何利用索引的
  17. 转:基于InstallShield2013LimitedEdition的安装包制作
  18. 在Oracle电子商务套件版本12.2中创建自定义应用程序(文档ID 1577707.1)
  19. 【C++】int与string互转
  20. 72.Edit Distance---dp

热门文章

  1. mvc读书笔记
  2. 制作二维码java
  3. Mac--查看公钥
  4. 安全清理Xcode 缓存垃圾
  5. iOS 8及以后版本 如何创建UIAlertView?
  6. WinCE下的串口通信开发(VS2005,VB.Net,VC++)
  7. injection for Xcode10使用方法
  8. 初学pygame
  9. 执行上下文--变量、函数、this
  10. UEditor显示Invalid or unexpected token