Tian Ji -- The Horse Racing

Time Limit: 2000/1000 MS (Java/Others)   Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 41290  Accepted Submission(s): 12427

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
2004 Asia Regional Shanghai.

题意

田忌赛马问题,赢一局赚200,输一局赔200,平局不赚不赔

思路

贪心,分三种情况:

  1. 当前田忌最慢的马比齐王最慢的马快,那么一定是田忌赢
  2. 当前田忌最慢的马比齐王最慢的马慢,田忌输,并用最慢的马和齐王最快的马进行比赛
  3. 当前田忌最慢的马和齐王最慢的马一样快,此时又分两种情况:
    1. 田忌最快的马比齐王最快的马慢,用田忌最慢的马和齐王最快的马比赛
    2. 田忌最快的马比齐王最快的马快,田忌赢

代码

 1 #include <bits/stdc++.h>
2 #define ll long long
3 #define ull unsigned long long
4 #define ms(a,b) memset(a,b,sizeof(a))
5 const int inf=0x3f3f3f3f;
6 const ll INF=0x3f3f3f3f3f3f3f3f;
7 const int maxn=1e6+10;
8 const int mod=1e9+7;
9 const int maxm=1e3+10;
10 using namespace std;
11 int a[maxn],b[maxn];
12 int main(int argc, char const *argv[])
13 {
14 #ifndef ONLINE_JUDGE
15 freopen("/home/wzy/in.txt", "r", stdin);
16 freopen("/home/wzy/out.txt", "w", stdout);
17 srand((unsigned int)time(NULL));
18 #endif
19 ios::sync_with_stdio(false);
20 cin.tie(0);
21 int n;
22 while(cin>>n&&n)
23 {
24 for(int i=0;i<n;i++)
25 cin>>a[i];
26 for(int i=0;i<n;i++)
27 cin>>b[i];
28 sort(a,a+n);sort(b,b+n);
29 int awin,bwin;
30 awin=bwin=0;
31 int alow,blow;
32 int afast,bfast;
33 alow=blow=0;afast=bfast=n-1;
34 for(int i=0;i<n;i++)
35 {
36 if(a[alow]>b[blow])
37 awin++,alow++,blow++;
38 else if(a[alow]<b[blow])
39 bwin++,alow++,bfast--;
40 else
41 {
42 if(a[afast]>b[bfast])
43 awin++,afast--,bfast--;
44 else if(a[alow]<b[bfast])
45 bwin++,bfast--,alow++;
46 }
47 }
48 cout<<200*(awin-bwin)<<endl;
49 }
50 #ifndef ONLINE_JUDGE
51 cerr<<"Time elapsed: "<<1.0*clock()/CLOCKS_PER_SEC<<" s."<<endl;
52 #endif
53 return 0;
54 }

HDU 1052:Tian Ji -- The Horse Racing(贪心)的更多相关文章

  1. HDU 1052 Tian Ji -- The Horse Racing(贪心)

    题目来源:1052 题目分析:题目说的权值匹配算法,有点误导作用,这道题实际是用贪心来做的. 主要就是规则的设定: 1.田忌最慢的马比国王最慢的马快,就赢一场 2.如果田忌最慢的马比国王最慢的马慢,就 ...

  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. HDU 1052 Tian Ji -- The Horse Racing【贪心在动态规划中的运用】

    算法分析: 这个问题很显然可以转化成一个二分图最佳匹配的问题.把田忌的马放左边,把齐王的马放右边.田忌的马A和齐王的B之间,如果田忌的马胜,则连一条权为200的边:如果平局,则连一条权为0的边:如果输 ...

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

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

  6. HDU 1052 Tian Ji -- The Horse Racing(贪心)(2004 Asia Regional Shanghai)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1052 Problem Description Here is a famous story in Ch ...

  7. hdu 1052 Tian Ji -- The Horse Racing【田忌赛马】

    题目 这道题主要是需要考虑到各种情况:先对马的速度进行排序,然后分情况考虑: 1.当田忌最慢的马比国王最慢的马快则赢一局 2.当田忌最快的马比国王最快的马快则赢一局 3.当田忌最快的马比国王最快的马慢 ...

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

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

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

  10. POJ-2287.Tian Ji -- The Horse Racing (贪心)

    Tian Ji -- The Horse Racing Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 17662   Acc ...

随机推荐

  1. tensorboard 拒绝连接无法打开相应页面

    启动tensorboard时没有报错,但打开页面却拒绝连接. 解决方法:tensorboard --logdir=TEC4FN --host=127.0.0.1 在命令最后添加 --host=127. ...

  2. keeper及er表示被动

    一些像employ这样的动词有employer和employee两个名词,而keep的名词只有keeper,keepee不是词.美剧FRIENDS和TBBT里出现了He/she is a keeper ...

  3. Spark基础:(二)Spark RDD编程

    1.RDD基础 Spark中的RDD就是一个不可变的分布式对象集合.每个RDD都被分为多个分区,这些分区运行在分区的不同节点上. 用户可以通过两种方式创建RDD: (1)读取外部数据集====> ...

  4. Flume(四)【配置文件总结】

    目录 一.Agent 二.Source taildir arvo netstat exec spooldir 三.Sink hdfs kafka(待续) hbase(待续) arvo logger 本 ...

  5. CAS你知道吗

    1.比较并交换 CASDemo /** * CAS => compareAndSet * 比较并交换 */ public class CASDemo { public static void m ...

  6. git stash命令及提交指定文件

    一.git stash命令 常用git stash命令: (1)git stash save "save message" : 执行存储时,添加备注,方便查找,只有git stas ...

  7. xtrabackup原理

    常用命令 innobackupex --defaults-file=/data/mysql_3306/my.cnf --no-timestamp --slave-info --compress --c ...

  8. GO Exit Fatal panic

    Exit() 应用程序(不只是函数)退出执行 defer 不会被执行(因为程序都退出了) log.Fatal() 输出打印内容 应用程序退出 defer 不会被执行 panic() 函数停止执行(不是 ...

  9. Mysql百万级数据索引重新排序

    参考https://blog.csdn.net/pengshuai007/article/details/86021689中思路解决自增id重排 方式一 alter table `table_name ...

  10. OpenStack之七: compute服务(端口8774)

    注意此处的bug,参考o版 官网地址 https://docs.openstack.org/nova/stein/install/controller-install-rdo.html 控制端配置 # ...