hdu 1052 田忌赛马(贪心)

题意:输入n(不为0),然后输入n个整数为田忌的马的速度和n个整数为齐王的马的速度,求田忌的收益,赢一场+200,输一场-200

首先考虑以下四种情况:

(1)田忌最慢的马速度大于齐王最慢的马的速度,此时这两匹马比较胜齐王

(2)田忌最慢的马速度小于齐王最慢的马的速度,此时用齐王最快马比田忌最慢马,输之

(3)田忌最快的马速度大于齐王最快的马的速度,此时这两匹马比较胜齐王

(4)田忌最快的马速度小于齐王最快的马的速度,此时用齐王最快马比田忌最慢马,输之

如果这四种情况都排除了,那么就是田忌最快和最慢马与齐王最快最慢马速度各自相同,此时用田忌最慢马比之齐王最快马,此时存在两种情况

(1)田忌最慢马速度等于齐王最快马,此时平局

(2)田忌最慢马速度小于于齐王最快马,田忌输之

#include <iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;

int main()
{
    int n;
    while (cin >> n && n)
    {
        vector<int> tj(n);
        vector<int> qw(n);
        for (int i = 0; i < n; i++)
        {
            cin >> tj[i];
        }
        for (int i = 0; i < n; i++)
        {
            cin >> qw[i];
        }
        sort(tj.begin(), tj.end());
        sort(qw.begin(), qw.end());
        int res = 0;
        int t1 = 0, t2 = n - 1;
        int q1 = 0, q2 = n - 1;
        int ans = n;
        while (ans > 0)
        {
            if (tj[t1] > qw[q1])
            {
                t1++;
                q1++;
                res += 200;
            }
            else if (tj[t1] < qw[q1])
            {
                t1++;
                q2--;
                res -= 200;
            }
            else if (tj[t2] > qw[q2])
            {
                t2--;
                q2--;
                res += 200;
            }
            else if (tj[t2] < qw[q2])
            {
                t1++;
                q2--;
                res -= 200;
            }
            else
            {
                //关键判断,存在最慢马等于最快马,此时平局
                if (tj[t1] < qw[q2])
                    res -= 200;
                t1++;
                q2--;
            }
            ans--;
        }
        cout << res << endl;
    }
}

 

posted on 2020-02-28 22:49  QingFengDaHui  阅读(152)  评论(0编辑  收藏  举报

导航