UVALive - 3266--贪心算法--田忌赛马

题意:

田忌赛马的故事,如果田忌赢了一把就赢200如果输了就-200,给出马匹的速度,求田忌的最大收益。

思路:

分三种大情况,

1.如果田忌的最快的马能赢国王最快的马那就比,

2.如果不能赢那就用田忌最慢的马去消耗国王最强的马,

3.最后一种大情况比较复杂,单独展开来说:如果田忌最快的与国王最快的马速度相等,别急着比快马。先比较田忌最慢的马和国王最慢的马,如果田忌慢马赢的了国王慢马,那就比,如果赢不了那就用这匹慢马去消耗国王的快马,这样田忌的快马就会保留下来。大概就是这样,一直比下去。还有一些细节讲不完,附上几组坑点,如果wa了,自己测试一下。

2 2 3 4

1 2 3 4

--------输出400

1 1 1 1

1 1 1 1

---------输出0

1 3 4 6

1 4 5 6

---------输出200

特别鸣谢jacknazi提供的样例。

#include <iostream>
#include<cstring>
#include<algorithm> 
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int tmax,tmin,gmax,gmin;
int tian[1005],guo[1005];
int main(int argc, char** argv) {
int n;
while(cin>>n&&n){
int win=0;
for(int i=0;i<n;i++)
cin>>tian[i];
sort(tian,tian+n);
for(int i=0;i<n;i++)
cin>>guo[i];
sort(guo,guo+n);
gmax=tmax=n-1;
tmin=gmin=0;
while(gmax>=gmin){
if(tian[tmax]>guo[gmax]){
tmax--;
gmax--;
win++;
}
else if(tian[tmax]<guo[gmax]){
tmin++;
gmax--;
win--;
}
else{
if(tian[tmin]>guo[gmin]){
//tmax--;gmax--;
tmin++;gmin++;
win++;
}
else if(tian[tmin]==guo[gmin]){
if(guo[gmax]==tian[tmin]){
gmax--;tmin++;
}
else{
gmax--;tmin++;
win--;
}
}
else{
gmax--;tmin++;
win--;
}
}
}
printf("%d\n",win*200);

}
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

竖子敢尔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值