标签:c++
3 92 83 71 95 87 74 2 20 20 20 20 2 20 19 22 18
200 0 0
我的代码:
#include<iostream> #include <cstdio> #include <algorithm> using namespace std; int main() { int n,i,ii,j,jj,ans; int a[1010],king[1010]; while (~scanf("%d",&n)) { for (i=0;i<n;++i) { scanf("%d",&a[i]); } for (i=0;i<n;++i) { scanf("%d",&king[i]); } sort(a,a+n); sort(king,king+n); i=0;ii=n-1;j=0;jj=n-1;ans=0; while(j<=jj) { if (king[i]<a[j])//数列末端比较是否比国王的快,快就他俩比 { ans++; i++; j++; } else if (king[ii]<a[jj])//数列首端比较是否比国王的快,快就他俩比 { ans++; ii--; jj--; } else//剩下的就是国王的大或等于田忌的马了 { if (king[ii]>a[j])//若国王的马大于田忌末端的马,田忌输了 { ans--; } ii--;//不管输赢都会消耗国王首段马与田忌末端马才最优 j++; } } printf("%d\n",ans*200); } return 0; }
标程:
#include <stdio.h> #include <algorithm> using namespace std; int T[1002],K[1002],n,win,lose; void read() { for(int i=0;i<n;i++) scanf("%d",&T[i]); for(int i=0;i<n;i++) scanf("%d",&K[i]); sort(T,T+n); sort(K,K+n); } void race() { win = lose =0; int t_slow=0,t_fast=n-1; int k_slow=0,k_fast=n-1; while(t_slow <= t_fast) { if(T[t_slow] > K[k_slow]) { win++; t_slow++; k_slow++; } else if(T[t_slow] < K[k_slow]) { lose++; t_slow++; k_fast--; } else { if(T[t_fast] > K[k_fast]) { win++; t_fast--; k_fast--; } else { if(T[t_slow]!=K[k_fast]) lose++; t_slow++; k_fast--; } /*else if(T[t_fast] < K[k_fast]) { lose++; t_slow++; k_fast--; } else { if(T[t_slow]==K[t_fast]) break; lose++; t_slow++; k_fast--; }*/ } } } int main() { //freopen("1","r",stdin); //freopen("2","w",stdout); while(~scanf("%d",&n)) { read(); race(); //printf("win:%d\tlose:%d\n",win,lose); printf("%d\n",200*(win-lose)); } return 0; }
标签:c++
原文地址:http://blog.csdn.net/zsc2014030403015/article/details/45028343