LA 3266 || UVALive 3266 Tian Ji -- The Horse Racing 田忌赛马(贪心)

来源:互联网 发布:加强网络基础设施建设 编辑:程序博客网 时间:2024/05/29 16:31

大体题意:

田忌与齐王赛马,他们有n 匹马,告诉你每匹马的速度,田忌赢一场得200,输一场扣200,平局不赚不扣,求最高赚多少?

思路:

贪心思路

都给双方排个序,从最快的 马开始枚举,

如果田忌的当前的马快于齐王的马,那么这局肯定赢,直接得200。

如果慢与齐王,那么这局肯定赢不了,既然赢不了,那不如用田忌当前最慢的马去输齐王当前最快的马。

如果与齐王速度相同,那么这局也赢不了,那就看田忌当前最慢的马,如果当前最慢的马和齐王当前最快的马平速,那么不赚不得,继续循环,如果慢与齐王最快的马,那么肯定输掉200了!

模拟一下钱即可!

详细见代码:

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn = 1000 +10;int a[maxn],b[maxn];int main(){    int n;    while(scanf("%d",&n) == 1 && n){        for (int i = 1; i <= n; ++i)scanf("%d",&a[i]);        for (int i = 1; i <= n; ++i)scanf("%d",&b[i]);        int p1 = 1,p2 = n,q1 = 1,q2 = n;        sort(a+1,a+n+1);        sort(b+1,b+n+1);        int ans = 0;        for (int i = 1; i <= n; ++i){            if (a[p2] > b[q2]){                ans += 200;                p2--;q2--;            }            else if (a[p2] < b[q2]){                ans -= 200;                q2--;p1++;            }            else {                if (a[p1] > b[q1]){                    ans += 200;                    p1++;q1++;                }                else {                    if (a[p1] < b[q2]){                        ans -= 200;                        p1++;                        q2--;                    }                    else {                        p1++;                        q2--;                    }                }            }        }        printf("%d\n",ans);    }    return 0;}



0 0
原创粉丝点击