【杭电OJ】1052 - 田忌赛马 (贪心)

题目链接

题意:就是田忌赛马,不过是n匹马。

思路:就是田忌赛马的思路,那最弱的和对方最强的比赛。
本来想的是只考虑最强的就行了,然后发现

3
300 200 100
300 300 100

这种情况不正确,很多情况漏掉,就换了思路。

就是四个指针移来移去的,模拟就完事了。

AC代码:

#include <bits/stdc++.h>
using namespace std;

int T[1100],Q[1100];
bool cmp(int a,int b){
    return a>b;
}
int main(){
    int n;
    int sum;
    int TFast,QFast,TLow,QLow;
    while(scanf("%d",&n) && n){
        for(int i=0;i<n;++i)    scanf("%d",&T[i]);
        for(int i=0;i<n;++i)    scanf("%d",&Q[i]);
        sort(T,T+n,cmp);
        sort(Q,Q+n,cmp);
        sum = 0;
        TFast = QFast = 0;
        TLow = QLow = n-1;
        while(n--){
            if(T[TFast] > Q[QFast]){
                TFast++;
                QFast++;
                sum++;
            }
            else if(T[TLow] > Q[QLow]){
                TLow--;
                QLow--;
                sum++;
            }
            else if(T[TLow] < Q[QFast]){
                TLow--;
                QFast++;
                sum--;
            }
        }
        printf("%d\n",sum*200);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41009682/article/details/82155338