田忌赛马 (Tian Ji - The Horse Racing,Shanghai 2004,LA 3266)
1 #include <iostream> 2 #include <string.h> 3 #include <string> 4 #include <fstream> 5 #include <algorithm> 6 #include <stdio.h> 7 #include <vector> 8 #include <queue> 9 using namespace std; 10 #define MAXN 10002 11 int main() 12 { 13 int tian[MAXN],king[MAXN]; 14 int n; 15 while(scanf("%d",&n),n!=0) 16 { 17 for(int i=0;i<n;i++) 18 { 19 scanf("%d",&tian[i]); 20 } 21 sort(tian,tian+n); 22 for(int i=0;i<n;i++) 23 { 24 scanf("%d",&king[i]); 25 } 26 sort(king,king+n); 27 int t=0,k=0; 28 int tn=n-1,kn=n-1; 29 int cost=0; 30 while(t<=tn&&k<=kn) 31 { 32 if(tian[t]>king[k]) 33 {cost+=200;tian[t++]=0;king[k++]=0;} 34 else if(tian[t]<king[k]) 35 {if(tian[t]<king[kn])cost-=200;tian[t++]=0;king[kn--]=0;} 36 else if(tian[t]==king[k]) 37 { 38 if(tian[tn]>king[kn]) 39 {cost+=200;tian[tn--]=0;king[kn--]=0;} 40 else if(tian[tn]<king[kn]) 41 {if(tian[t]<king[kn])cost-=200;tian[t++]=0;king[kn--]=0;} 42 else if(tian[tn]==king[kn]) 43 {if(tian[t]<king[kn])cost-=200;tian[t++]=0;king[kn--]=0;} 44 } 45 } 46 47 printf("%d\n",cost); 48 } 49 50 51 return 0; 52 }
贪心~