hdu1052Tian Ji -- The Horse Racing(貪心,細節多)
阿新 • • 發佈:2018-11-07
Tian Ji -- The Horse Racing
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 37436 Accepted Submission(s): 11248
"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.
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
題意:田忌賽馬,知道了田忌的n只馬的速度和齊王的n只馬的速度,贏一場得到200塊,輸一場失去200塊,平局保持不變。田忌想要更多錢。輸出田忌最後最多能拿到多少錢。
題解:把田忌和王的馬按速度從大到小排序。之後就是超多的細節。。。。。。。。。具體看程式碼註釋
1 #include<bits/stdc++.h> 2 using namespace std; 3 int a[3000],b[3000]; 4 bool cmp(int x,int y) 5 { 6 return x>y; 7 } 8 int main() 9 { 10 int n,t1,t2,k1,k2; 11 while(~scanf("%d",&n),n) 12 { 13 memset(a,0,sizeof(a)); 14 memset(b,0,sizeof(b)); 15 for(int i=0;i<n;i++) 16 { 17 scanf("%d",&a[i]); 18 } 19 for(int i=0;i<n;i++) 20 { 21 scanf("%d",&b[i]); 22 } 23 sort(a,a+n,cmp);//按速度從大到小排序 24 sort(b,b+n,cmp); 25 t1=k1=0;//田忌和齊王最強的馬的下標 26 t2=k2=n-1;//田忌和齊王最差的馬的下標 27 int ans=0; 28 while(t1<=t2) 29 { 30 if(a[t1]>b[k1])//田忌的最強比王的最強更強, 31 { 32 ans+=200;t1++;k1++; 33 } 34 else if(a[t1]<b[k1])//田忌的最強比王的最強弱,用最弱的去比 35 { 36 ans-=200;t2--;k1++; 37 } 38 else//最強的馬速度一樣 39 { 40 if(a[t2]>b[k2])//田忌最慢的馬比王最慢的馬快 41 { 42 ans+=200;t2--;k2--; 43 } 44 else if(a[t2]==b[k1])//田忌最慢的馬和王最快的馬速度一樣,平局 45 { 46 k1++;t2--; 47 } 48 else//田忌最慢的馬和齊王最快的馬比 49 { 50 ans-=200;k1++;t2--; 51 } 52 } 53 } 54 printf("%d\n",ans); 55 } 56 return 0; 57 }