hdu 1052 TianJi
来源:互联网 发布:xplay6知乎 编辑:程序博客网 时间:2024/05/24 07:06
#include<stdio.h>#include<string.h>#include<iostream>#include<algorithm>using namespace std;int a[1005]; //田忌的马 int b[1005]; //国王的马 int main(){int n;while(scanf("%d", &n) && n){for(int i = 1; i <= n; i++){scanf("%d", &a[i]);}for(int i = 1; i <= n; i++){scanf("%d", &b[i]);}int m = n; //n是田忌的马的总数,m是国王的马的总数 sort(a+1, a+n+1);sort(b+1, b+n+1);int sum = 0;int sub = 0;int i = 1, j = 1;while(i <= n && j <= m){if(a[i] > b[j]){ //如果田忌最慢的马比国王最慢的马快,赢一场 sum++;i++;j++;}else if(a[i] < b[j]){ //如果田忌最慢的马比国王最慢的马慢,输一场 //用田忌最慢的马输国王最快的马 sub++;i++;m--;} else{ //田忌最慢的马和国王最慢的马速度相同 if(a[n] > b[m]){ //如果田忌最快的马比国王最快的马还快,赢一场 sum++;n--;m--;}else if(a[n] < b[m]){ //如果田忌最快的马比国王最慢的马慢, //用田忌最慢的马输给国王最快的马 ,输 sub++;i++;m--;}else{ //田忌最快的马和国王最快的马速度相等 if(a[i] < b[m]){ //如果田忌最慢的马比国王最快的马慢,输 sub++; //画图分析这 i++;m--;} else if(a[i]==b[m]){ //最快的马和最慢的马速度相同,打平 break;}}}}printf("%d\n", (sum-sub)*200); }return 0;}
Tian Ji -- The Horse Racing
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 30758 Accepted Submission(s): 9316
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
阅读全文
0 0
- hdu 1052 TianJi
- pku 2287 TianJi -- The Horse Racing 贪婪的dp 解题报告
- hdu 1052
- hdu-1052
- hdu 1052
- hdu 1052
- hdu 1052
- HDU--1052
- hdu 1052
- hdu 1052
- HDU 1052
- hdu 1052贪心算法
- hdu 1052 (水)
- hdu 1052 贪心
- hdu 1052 贪心
- HDU 1052(贪心)
- hdu 1052 贪心
- HDU ACM 1052
- c语言单向链表的基本功能的实现
- (C++模板类)顺序表、双向链表
- ZOJ3541与时间相关的区间dp+记录路径
- 杭电acm 2802F(N)(循环节)
- 数组(1)
- hdu 1052 TianJi
- POJ :Networking
- 如何配置myeclipse10,Spring配置文件中输入class时的自动提示,spring本身的xml可以提示
- 阿里fastJson几种Json方式
- python virtualenv 强制修改环境变量迁移环境
- logistics回归--梯度上升算法以及改进--用于二分类
- 《代码整洁之道》读书笔记
- if __name__ == '__main__'的作用与原理
- 深入理解java虚拟机学习笔记-4.java类加载机制