hdu1052田忌赛马,贪心解法
来源:互联网 发布:起名网站源码 编辑:程序博客网 时间:2024/05/18 01:03
http://acm.hdu.edu.cn/showproblem.php?pid=1052
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.
"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
392 83 7195 87 74220 2020 20220 1922 180
Sample Output
20000
此题解法:
1.排序,按照马的速度由慢到快排列,共n匹马。
2.拿田忌最慢的马与齐王最慢的马进行比较,两种情况
①当tj[i]>qw[j],胜场+1,ij后移
证明:田忌最慢的马都比齐王最慢的马快,除了直接取胜外没有更好的方法来使此解更优。
②当tj[i]==qw[j],判断田忌最快的马是否比齐王最快的马快,如果是就让最快的马比一场。
证明:田忌最快的马比齐王最快的马快,没有必要让最慢的马去输一场,举例23 25和23 24。
如果不是,就用田忌最慢的马和齐王最快的马比。
证明:反正都要输,还不如用最烂的马去输。
③当tj[i]<qw[j],让田忌最慢的马与齐王最快的马比较,胜场-1,i后移。
证明:同上。
#include <iostream>#include <algorithm>using namespace std;int main(){//freopen("test.in","r",stdin);int n,money;int tj[1005],king[1005];while(cin >>n){if (0 == n) break;memset(tj,0,sizeof(tj));memset(king,0,sizeof(king));money = 0;for(int i = 0; i < n; i++){cin >>tj[i];}for(int i = 0; i < n; i++){cin >>king[i];}sort(tj,tj+n);sort(king,king+n);int sec_begin,sec_end;int king_begin = sec_begin = 0;int king_end = sec_end = n - 1;while(sec_begin <= sec_end){if (tj[sec_begin] > king[king_begin]){money += 200;sec_begin++;king_begin++;}else if (tj[sec_begin] == king[king_begin]){if (tj[sec_end] > king[king_end]){money += 200;sec_end--;king_end--;}else{if (king[king_begin] != king[king_end]){money -= 200;}sec_begin++;king_end--;}}else if (tj[sec_begin] < king[king_begin]){money -= 200;sec_begin++;king_end--;}}cout <<money <<endl;}return 0;}
- hdu1052田忌赛马,贪心解法
- HDU1052:田忌赛马《贪心》
- hdu1052 田忌赛马 贪心正确性证明
- 田忌赛马--贪心算法 HDU1052
- hdu1052 田忌赛马 —— 贪心
- 田忌赛马 hdu1052
- HDU1052 田忌赛马
- hdu1052-田忌赛马
- 贪心算法:田忌赛马-HDU1052 & POJ2287 & ZOJ2397
- HDU1052——田忌赛马(贪心)
- 贪心——田忌赛马(HDU1052)
- HDU1052 Tian Ji -- The Horse Racing 田忌赛马 贪心
- Acm hdu1052 田忌赛马
- hdu1052 田忌赛马问题
- 田忌赛马: POJ 2287(贪心解法)
- TYVJ 1048 田忌赛马 贪心解法
- zznu 1257 HDU1052 田忌赛马
- hdu1052(田忌赛马题解)
- gvim和gedit显示中文乱码的解决方法
- K60(Cortex-M4)开源开发探索(七)—— kermit的安装与配置
- XML解析(DOM详解)
- C#父类与子类(多态性)
- 函数指针数组和C变量的定义方式
- hdu1052田忌赛马,贪心解法
- Linux子文件和目录的作用及常用的命令
- Nasty Hacks
- linux安装g++
- DX11中的constant buffer的padding原理
- HDUOJ 2046(递推)
- 最大网络流算法
- 单调队列模板
- Fibonacci数列的通项公式