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?
3 92 83 71 95 87 74 2 20 20 20 20 2 20 19 22 18 0
200 0 0
1 #include <bits/stdc++.h> 2 using namespace std; 3 //思路:最快马能不能赢,不能赢就看最慢马能不能赢,如果头尾都不能赢,干脆我用最慢消耗你的最快 4 int main() { 5 int n; 6 while(cin>>n&&n!=0){ 7 int tj[n],king[n]; 8 for(int i=0;i<n;i++){ 9 cin>>tj[i]; 10 } 11 for(int i=0;i<n;i++){ 12 cin>>king[i]; 13 } 14 sort(tj,tj+n); 15 sort(king,king+n); 16 int ltj=0,lking=0,rtj=n-1,rking=n-1,num=0,ans=0; 17 while(num<n){ 18 if(tj[rtj]>king[rking]){ 19 ans+=200; 20 rtj--; 21 rking--; 22 num++; 23 }else if(tj[ltj]>king[lking]){ 24 ans+=200; 25 ltj++; 26 lking++; 27 num++; 28 } 29 else if(tj[ltj]==king[rking]){ 30 ltj++; 31 rking--; 32 num++; 33 }else{ 34 ans-=200; 35 ltj++; 36 rking--; 37 num++; 38 } 39 } 40 cout<<ans<<endl; 41 } 42 43 return 0; 44 }
2287:Tian Ji -- The Horse Racing(贪心算法)
原文:https://www.cnblogs.com/aiqinger/p/12584804.html