HDU1052——Tian Ji -- The Horse Racing

http://acm.hdu.edu.cn/showproblem.php?pid=1052

这道题用的是贪心策略,

将双方的马按照速度排序,从小到大,最大的先比,

如果能赢就赢,不能的话就比较两个小的,能赢就赢,不能的话就用这个弱马消耗那个速度快的马。

策略就是我要尽量赢,不能赢就拿弱马消耗。

#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
int a[1001],b[1001],n;
int main(){
     while(~scanf("%d",&n)&&n!=0){
         memset(a,0,sizeof(a));
         memset(b,0,sizeof(b));
         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);
         sort(b,b+n);
         int x=n-1,y=0,w=0,i=n-1,j=0;
         while(i>=j&&x>=y){
             if(a[i]>b[x]){//a的好马比b的好马强
              w+=200;
              i--;
              x--;
             }else
             if(a[j]>b[y]){//a的差马比b的差马强
                 j++;
                 y++;
                 w+=200;
             }
             else
             if(a[j]<b[x]){//a的差马比b的好马弱的话(消耗b的好马)
                 j++;
                 x--;
                 w-=200;
             }  
             else
             if(a[j]==b[y]){//a的差马和b的差马一样
                 j++;
                 y++;
             }else
             if(a[i]==b[x]){//a的好马和b的一样
                 i--;
                 x--;
             }
         } 
         printf("%d\n",w);
    }
}
posted @ 2015-05-23 01:50  Yvettey  阅读(126)  评论(0)    收藏  举报