Tian Ji -- The Horse Racin

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
#include<iostream>
#include<algorithm>
using namespace std;
int cmp(int a,int b)
{
return a>b;
}
int main()
{
int a[2605],b[2605];
int n,i,j,cou,s;
while(cin>>n&&n)
{
cou=0; for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n;i++)
cin>>b[i];
sort(a,a+n,cmp);
sort(b,b+n,cmp); int ji=0;
i=j=cou=0;
int k=n-1;
int f=n-1;
while(1)
{
if(ji==n) break;
if(b[j]>a[i]) { cou-=200;j++;k--;ji++; continue;}
if(b[j]==a[i]){
if(b[f]<a[k])
{f--;k--;cou+=200;ji++;continue;}
if(b[j]>a[k])
{cou-=200;k--;j++;ji++;}
else
{k--;j++;ji++;}
continue;
}
if(b[j]<a[i]){cou+=200;j++;i++;ji++;continue;}
}
cout<<cou<<endl; } return 0;
}

Tian Ji -- The Horse Racin的更多相关文章

  1. 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 ...

  2. Tian Ji -- The Horse Racing

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

  3. 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 ...

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

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

  5. hdu1052 Tian Ji -- The Horse Racing 馋

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:pid=1052">http://acm.hdu.edu.cn/showproblem.php ...

  6. 杭州电 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 ...

  7. 【贪心】[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 ...

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

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

  9. Tian Ji -- The Horse Racing HDU - 1052

    Tian Ji -- The Horse Racing HDU - 1052 (有平局的田忌赛马,田忌赢一次得200块,输一次输掉200块,平局不得钱不输钱,要使得田忌得到最多(如果只能输就输的最少) ...

随机推荐

  1. c#超时锁定

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  2. CreateThread、_beginthreadex和AfxBeginThread 的区别

    CreateThread._beginthreadex和AfxBeginThread 创建线程好几个函数可以使用,可是它们有什么区别,适用于什么情况呢?参考了一些资料,写得都挺好的,这里做一些摘抄和整 ...

  3. nRF Toolbox 1.2 使用AKII的实现,而Becon始终不好使

    这几天调试使用nRF51822驱动mpu6050及其数据传输到android中,调试的过程遇到一些困难,apptimer不太会用,然后就參考了下ble_app_hrs的程序,结果成功搞定,demo的价 ...

  4. poj 2166 构造

    一个看了解题报告才能想明白的题目,第一点比较容易想明白,就是每次把1交换到堆顶之后如果能够换到最后面的位置那么一定是最优的,但是怎么实现这个没有想明白. 题解的那种构建方法,及从2开始插入,是可以保证 ...

  5. python实现人人网用户数据爬取及简单分析

    这是之前做的一个小项目.这几天刚好整理了一些相关资料,顺便就在这里做一个梳理啦~ 简单来说这个项目实现了,登录人人网并爬取用户数据.并对用户数据进行分析挖掘,终于效果例如以下:1.存储人人网用户数据( ...

  6. Java跨域设置

    Access-Control-Allow-Origin 为允许哪些Origin发起跨域请求. 这里设置为"*"表示允许所有,通常设置为所有并不安全,最好指定一下. Access-C ...

  7. poj1182(并查集)

    题目链接 分析:根据分析,关系的递推满足由[a,b]~[b,c]得:[a,c]=([a,b]+[b,c])%3;[a,d]=([a,b]+[b,c]+[c,d])%3.由rank数组表示关系 0 -  ...

  8. php 汉字转拼音 [包含20902个基本汉字+5059生僻字]

    原文:php 汉字转拼音 [包含20902个基本汉字+5059生僻字] 昨天在转换拼音的时候发现个bug,有好多字都无法转换,不过也不能怪他,毕竟人家的库才8k,应该只有常用的.无奈上网找了下,发现一 ...

  9. Java对Xml进行操作的实例(转)

    这是一个用JAVA W3C DOM 进行XML操作的例子,包含了查询.增加.修改.删除.保存的基本操作.较完整的描述了一个XML的整个操作流程.适合刚入门JAVA XML操作的朋友参考和学习. 假设有 ...

  10. HttpAsyncClient 做并发长连接的一个实例

    HttpAsyncClient 做并发长连接的一个实例 import java.util.concurrent.CountDownLatch; import org.apache.http.HttpR ...