1052 Tian Ji -- The Horse Racing(贪心-5)
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
3
92 83 71
95 87 74
2
20 20
20 20
2
20 19
22 18
0
Sample Output
200
0
0
这是根据高手代码改编的一个代码
1 #include<iostream> 2 #include <algorithm> 3 #include <cstring> 4 #include <stdio.h> 5 #include <math.h> 6 using namespace std; 7 8 int main() 9 { 10 int k[1005],t[1005];//定义数组 11 int n,ans,t1,t2,k1,k2; 12 while(cin>>n&&n!=0) 13 { 14 for(int i=0;i<n;i++) 15 cin>>t[i];//输入田忌的数据 16 for(int i=0;i<n;i++) 17 cin>>k[i];//输入国王的数据 18 sort(k,k+n); 19 sort(t,t+n);//排序 20 t1=k1=0;t2=k2=n-1;ans=0; 21 while(t1<=t2)//两者比较 22 { 23 if(t[t1]>k[k1]){ans++;t1++;k1++;}//田忌最慢的马与国王最慢的马比,如果田忌最慢的马比国王最慢的马快,那么田忌胜 24 else if(t[t1]<k[k1]){ans--;t1++;k2--;}//如果国王最慢的马比田忌最慢的马快,那么就用国王最快的马和田忌最慢的马比,田忌负 25 else//如果两者最慢的马速度相同,就拿国王最快的马和田忌最快的马比 26 { 27 if(t[t2]>k[k2]){ans++;t2--;k2--;}//如果国王最快的马没有田忌最快的马快,那么田忌胜 28 else {if(t[t1]<k[k2]) ans--;k2--;t1++;}//注意是t[t1]<k[k2]//如果田忌最快的马不比国王最快的马快,那么就拿国王最快的马和田忌最慢的马比,如果田忌最慢的马比国王最快的马慢,那么田忌负,否则平手 29 } 30 } 31 cout<<ans*200<<endl; 32 } 33 return 0; 34 }
高手代码如下:
1 #include<iostream> 2 using namespace std; 3 int cmp(const void *a,const void *b) 4 { 5 return *(int *)a-*(int *)b; 6 } 7 int main(void) 8 { 9 int n,T[1001],K[1001],i,j,s,e,sum; 10 while(cin>>n&&n) 11 { 12 sum=0; 13 for(i=0;i<n;i++) cin>>T[i]; 14 for(i=0;i<n;i++) cin>>K[i]; 15 qsort(T,n,sizeof(int),cmp); 16 qsort(K,n,sizeof(int),cmp); 17 18 i=s=0;j=e=n-1;sum=0; 19 while(i<=j) 20 { 21 if(T[i]>K[s]) {sum++;s++;i++;} 22 else if(T[i]<K[s]){sum--;e--;i++;} 23 else 24 { 25 if(T[j]>K[e]) {sum++;e--;j--;} 26 else{if(T[i]<K[e])sum--;e--;i++;} 27 } 28 } 29 cout<<200*sum<<endl; 30 } 31 //system("pause"); 32 return 0; 33 }
网上找到的一个没用while的代码
1 #include<stdio.h> 2 int main() 3 { 4 int n,i,j,temp,max,reward; 5 int Tian[1024],King[1024]; 6 int SlowT,FastT,SlowK,FastK,counter; 7 while(scanf("%d",&n)&&n) 8 { 9 for(i=0;i<n;i++) 10 scanf("%d",&Tian[i]); 11 for(i=0;i<n;i++) 12 scanf("%d",&King[i]); 13 for(i=0;i<n-1;i++) 14 { 15 max=i; 16 for(j=i+1;j<n;j++) 17 { 18 if(Tian[max]<Tian[j]) 19 max=j; 20 } 21 temp=Tian[max]; 22 Tian[max]=Tian[i]; 23 Tian[i]=temp; 24 } 25 for(i=0;i<n-1;i++) 26 { 27 max=i; 28 for(j=i+1;j<n;j++) 29 { 30 if(King[max]<King[j]) 31 max=j; 32 } 33 temp=King[max]; 34 King[max]=King[i]; 35 King[i]=temp; 36 } 37 reward=0; 38 SlowT=SlowK=n-1; 39 FastK=FastT=0; 40 for(counter=0;counter<n;counter++) 41 { 42 if(Tian[SlowT]>King[SlowK]) 43 { 44 SlowT--; 45 SlowK--; 46 reward++; 47 } 48 else if(Tian[SlowT]<King[SlowK]) 49 { 50 reward--; 51 SlowT--; 52 FastK++; 53 } 54 else 55 { 56 if(Tian[FastT]>King[FastK]) 57 { 58 reward++; 59 FastK++; 60 FastT++; 61 } 62 else 63 { 64 if(Tian[SlowT]<King[FastK]) 65 reward--; 66 FastK++; 67 SlowT--; 68 } 69 } 70 } 71 reward*=200; 72 printf("%d\n",reward); 73 } 74 return 0; 75 }