Tian Ji -- The Horse RacingTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 38193 Accepted Submission(s): 11464 Problem Description Here is a famous story in Chinese history.
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
Source
Recommend JGShining 题意:田忌赛马,找一个可以赢钱最多的方法(贪心的思路不太好想) 分析:1.如果田忌最快的马比齐王最快的马快,那么两者相比。 (因为田忌其他的马很可能赢不了,所有两者相比) 2.如果田忌最快的马比齐王最快的马慢,则用田忌最慢的马和齐王最快的马来比。 (因为连最快的马都赢不了齐王,那就用最慢的马去和齐王比,减小损失) 3.如果田忌最快的马和齐王最快的马相等,则比较两人最慢的马。 3.1.如果田忌最慢的马比齐王最慢的马快,则两者相比。 (田忌的最慢能赢一个就赢一个,而且要和齐王最慢的马比,当然要选最小的) 3.2.其他,则用田忌最慢的马比齐王最快的马。 (连齐王最慢的马都比这马快,那这马唯一的作用就是去弄掉齐王最快的马,这样可以把他的贡献最大化)
|
#include <stdio.h>
#include <algorithm>
using namespace std;
const int maxn=1010;
int tj[maxn],qw[maxn];
int main()
{
int i;
int n,max1,max2,min1,min2,ans,cnt;
while(scanf("%d",&n)!=EOF&&n){
for(i=0;i<n;i++){
scanf("%d",&tj[i]);
}
for(i=0;i<n;i++){
scanf("%d",&qw[i]);
}
sort(tj,tj+n);
sort(qw,qw+n);
max1=max2=n-1;
min1=min2=0;
cnt=0;
ans=0;
while((cnt++)<n){
if(tj[max1]>qw[max2]){
ans+=200;
max1--;
max2--;
}
else if(tj[max1]<qw[max2]){
ans-=200;
min1++;
max2--;
}
else{
if(tj[min1]>tj[min2]){
ans+=200;
min1++;
min2++;
}
else{
if(tj[min1]<qw[max2]){
ans-=200;
max2--;
min1++;
}
}
}
}
printf("%d\n",ans);
}
return 0;
}