POJ 2287 Tian Ji The Horse Racing(贪心)

题意:田忌和齐王有n匹马,进行n局比赛,每局比赛输者给胜者200,问田忌最多能得多少钱。

分析:如果田忌最下等的马比齐王最下等的马好,是没必要拿最下等的马和齐王最好的马比的。(最上等马同理)

因此,如果田忌最下等的马>齐王最下等的马或者田忌最上等的马>齐王最上等的马,直接得200,如果不满足该条件,那么才让田忌最下等的马与齐王最上等的马来比。

#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
typedef long long ll;
typedef unsigned long long llu;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1};
const int dc[] = {-1, 1, 0, 0};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const double eps = 1e-8;
const int MAXN = 10000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
deque<int> a, b;
int main(){
    int n;
    while(scanf("%d", &n) == 1){
        if(n == 0) return 0;
        a.clear();
        b.clear();
        int x;
        for(int i = 0; i < n; ++i){
            scanf("%d", &x);
            a.push_back(x);
        }
        for(int i = 0; i < n; ++i){
            scanf("%d", &x);
            b.push_back(x);
        }
        sort(a.begin(), a.end());
        sort(b.begin(), b.end());
        int ans = 0;
        while(n--){
            if(a.front() > b.front()){
                ans += 200;
                a.pop_front();
                b.pop_front();
            }
            else if(a.back() >  b.back()){
                ans += 200;
                a.pop_back();
                b.pop_back();
            }
            else{
                if(a.front() < b.back()){
                    ans -= 200;
                    a.pop_front();
                    b.pop_back();
                }
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/tyty-Somnuspoppy/p/6104757.html