首页 > 其他 > 详细

hdu 1052 田忌赛马(贪心)

时间:2020-02-29 02:02:39      阅读:57      评论:0      收藏:0      [点我收藏+]

题意:输入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;
    }
}

 

hdu 1052 田忌赛马(贪心)

原文:https://www.cnblogs.com/QingFengDaHui/p/12380885.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!