NYOJ364田忌赛马

题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=364


思路:1.当田忌的快马比齐王的快马块时,赢一场

           2.当田忌的慢马比齐王的慢马快时,赢一场

           3.当1和2都不满足时,用田忌的慢马去和齐王的慢马去比。


代码:

#include <cstdio>
#include <iostream>
#include <algorithm>

using namespace std;

int tian[1005];
int king[1005];

int main()

{
    int n;
    while(~scanf("%d",&n))
    {
        for(int i = 0;i < n;++i)
            scanf("%d",&tian[i]);
        for(int i = 0;i < n;++i)
            scanf("%d",&king[i]);
        sort(tian,tian + n);
        sort(king,king + n);
        int win = 0;
        int lose = 0;
        int tl = 0,tr = n - 1;
        int kl = 0,kr = n - 1;
        while(tl <= tr && kl <= kr)
        {
            if(tian[tr] > king[kr])
            {
                win++;
                tr--;
                kr--;
            }
            else if(tian[tl] > king[kl])
            {
                 win++;
                 tl++;
                 kl++;
            }
            else
            {
                if(tian[tl] == king[kr])
                {
                    tl++;
                    kr--;
                }
                else if(tian[tl] < king[kr])
                {
                    lose++;
                    tl++;
                    kr--;
                }
            }
        }
        printf("%d\n",(win - lose) * 200);
    }
    return 0;
}

你可能感兴趣的:(ACM,贪心)