HDU ACMSteps 1.3.3:: Tian Ji ——The Horse Racing

#include

#include <memory.h> 

#include <algorithm>

using namespace std;

/*

HDU题目:: Tian Ji — The Horse Racing

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

分析:

输入马匹规模n,输入田忌n匹马的速度,输入国王n匹马的速度.其思路是用最小的耗费赢取最大的胜利。对于快马的比较,能赢则赢。不能赢则考虑尽量用最小的代价打平不输钱(这里主要是代价观点)

*/

bool cmp(int a,int b)

{

    return a>b;

}

int main(){

int tian[1002];

int king[1002];

memset(tian,0,1002*sizeof(int));

memset(king,0,1002*sizeof(int));

int n,i,fast,fask,slot,slok,win_num;

while(true){

cin>>n;

if(n==0) break;

for(i=0;i<n;i++)//输入两人马匹速度 

cin>>tian[i]; 

for(i=0;i<n;i++)

cin>>king[i]; 

sort(tian,tian+n,cmp);

sort(king,king+n,cmp);

fast=0,fask=0,slot=slok=n-1,win_num=0;

while(fast<=slot&&fask<=slok){

if(tian[fast]>king[fask]){//快马比皇上快 

win_num++;

fast++,fask++;

}

else//不赢的时候主要考虑能否用以获取平局

if(tian[slot]>king[slok]){//田忌的慢马比皇上的慢马快 

win_num++;

slot–,slok–;

}

else

{//慢马无法赢,那么用来挡输或打平 

if(tian[slot]<king[fask])//用田忌的慢马去输皇上最快的马 

win_num–;

slot–,fask++;

}

}

cout<<(win_num<<1)*100<<endl;

memset(tian,0,n*sizeof(int));//清零

memset(king,0,n*sizeof(int));

}

return 0;

点赞