ZOJ 2397:Tian Ji -- The Horse Racing
Tian Ji -- The Horse Racing
Time Limit: 5 Seconds Memory Limit: 32768 KB
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: Asia 2004, Shanghai (Mainland China)
迷失在幽谷中的鸟儿,独自飞翔在这偌大的天地间,却不知自己该飞往何方……
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
int a[1005],b[1005];
int main()
{
int n;
while(cin>>n&&n)
{
for(int i=0; i<n; i++)
cin>>a[i];
for(int i=0; i<n; i++)
cin>>b[i];
sort(a,a+n);
sort(b,b+n);
int s=0;
for(int i=0,j=0,k=n-1,l=n-1; i<=k;)
{
if(a[i]>b[j])s++,i++,j++;
else if(a[k]>b[l])s++,k--,l--;
else
{
if(a[i]<b[l])s--;
i++,l--;
}
}
cout<<s*200<<endl;
}
return 0;
}
ZOJ 2397:Tian Ji -- The Horse Racing的更多相关文章
- ZOJ Problem Set - 2397 Tian Ji -- The Horse Racing
#include<iostream> #include<cmath> #include<algorithm> #define REP(i,n) for(int i= ...
- 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 ...
- Tian Ji -- The Horse Racing
Tian Ji -- The Horse Racing Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Jav ...
- 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 ...
- hdoj 1052 Tian Ji -- The Horse Racing【田忌赛马】 【贪心】
思路:先按从小到大排序, 然后从最快的開始比(如果i, j 是最慢的一端, flag1, flag2是最快的一端 ),田的最快的大于king的 则比較,如果等于然后推断,有三种情况: 一:大于则比較, ...
- hdu1052 Tian Ji -- The Horse Racing 馋
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:pid=1052">http://acm.hdu.edu.cn/showproblem.php ...
- 杭州电 1052 Tian Ji -- The Horse Racing(贪婪)
http://acm.hdu.edu.cn/showproblem.php? pid=1052 Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS ...
- 【贪心】[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 ...
- 【OpenJ_Bailian - 2287】Tian Ji -- The Horse Racing (贪心)
Tian Ji -- The Horse Racing 田忌赛马,还是English,要不是看题目,我都被原题整懵了,直接上Chinese吧 Descriptions: 田忌和齐王赛马,他们各有n匹马 ...
随机推荐
- CAD在网页中绘图,并为新绘的对象写扩展数据和读取扩展数据
在网页中绘图,并为新绘的对象写扩展数据和读取扩展数据.下面帮助的完整例子,在控件安装目录的 Sample\Ie\iedemo.htm 中. 主要用到函数说明: _DMxDrawX::InsertBlo ...
- 【nginx】解决nginx搭建图片服务器访问图片404
图片通过ftp服务上传到/home/ftpuser/www/images目录下后访问 http://192.168.128.128/images/xxx.jpg 还是 404 NOT FOUND ,解 ...
- react antD 日期选择
<DatePicker disabledDate={disabledDate} onChange={this.onChange} /> //创建时间禁用大于当前时间 <moment( ...
- Docker 的基本使用
一.简介 Docker 是一个开源的应用容器引擎,基于 Go 语言.Docker 支持将软件编译成一个镜像,然后在镜像中为软件做好配置,将镜像发布出去,其他使用者就可以直接使用这个镜像,而不需再和以前 ...
- 【最短路】Dijkstra+ 链式前向星+ 堆优化(优先队列)
Dijkstra+ 链式前向星+ 优先队列 Dijkstra算法 Dijkstra最短路算法,个人理解其本质就是一种广度优先搜索.先将所有点的最短距离Dis[ ]都刷新成∞(涂成黑色),然后从起点 ...
- Luogu P2176 [USACO14FEB]路障Roadblock
解题思路 这是一道最短路题目,不知道大家有没有做过玛丽卡这道题目,如果没做,在做完这道题之后可以去拿个双倍经验哦 先求出一张图中的最短路径,并将其记录下来,我们首先思考:要有增量的前提是新的最短路径比 ...
- C/C++格式化输入,输出
C/C++格式化输入,输出 1.C语言 1. 语言函数 scanf(); printf(); sscanf() --> 不安全 sscanf_s() ---> 安全 sprintf() - ...
- [Algorithm] 4. Ugly Number II
Description Ugly number is a number that only havefactors 2, 3 and 5. Design an algorithm to find th ...
- buf.readInt32BE()
buf.readInt32BE(offset[, noAssert]) buf.readInt32LE(offset[, noAssert]) offset {Number} 0 <= offs ...
- Import CAD geometry in BladeModeler, turbogrid
Table of Contents 1. Import CAD geometry in BladeModeler, turbogrid 1 Import CAD geometry in BladeMo ...