贪心--hdu-1052- Tian Ji -- The Horse Racing
Tian Ji -- The Horse RacingTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 35544 Accepted Submission(s): 10693 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
Source
Recommend JGShining | We have carefully selected several similar problems for you: 1053 1789 2187 2570 2111
|
题意:给出自己马的速度,对方马的速度,通过调整使得 自己马获胜的次数最多。
题解:1>.己方快马和对方快马比,快-->up++(up 指当前己方所赢次数),慢或等速,执行 2>
2>.己方慢马和对方快马比,等速无操作,慢,执行 3>
3>.up--
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a[1005],b[1005];
bool cmp(int x,int y){
return x>y;
}
int main(){
int n;
while(scanf("%d",&n),n){
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}
for(int i=0;i<n;i++){
scanf("%d",&b[i]);
}
sort(a,a+n,cmp);
sort(b,b+n,cmp);
int up=0,lose=0,end1=n-1,end2=n-1,start1=0,start2=0;
while(start1<=end1&&start2<=end2){
if(a[start1]>b[start2]){
up++;
start1++;
start2++;
}
else{
while(a[end1]>b[end2]){
end1--;
end2--;
up++;
}
if(end1>=start1){
if(a[end1]<b[start2]){
start2++;
end1--;
up--;
}
else{
start2++;
end1--;
}
}
}
}
printf("%d\n",up*200);
}
return 0;
}
上一篇: Java自定义注解的具体介绍
下一篇: Java注解教程及自定义注解