URAL 1876. Centipede's Morning (贪心)

48 篇文章 0 订阅

1876. Centipede's Morning

Time limit: 0.5 second
Memory limit: 64 MB
A centipede has 40 left feet and 40 right feet. It keeps  a left slippers and  b right slippers under its bed. Every morning the centipede puts on the slippers. It pokes its first left foot under the bed and puts on a random slipper, doing it in one second. If the slipper is left, the centipede passes to shoeing the second left foot. Otherwise, it takes off the slipper and puts it on any unshod right foot, spending one more second, so that it takes two seconds altogether to put on such a slipper. If there are no unshod right feet, the centipede throws the slipper to a corner of the room, also in one second, so that two seconds are spent altogether for this slipper. The process is continued until all the left feet are in left slippers. Then the centipede starts shoeing its right feet until all of them are shod.
Today the centipede has got out of bed on the wrong side, so it is preparing for the worst. How many seconds will it need for shoeing?

Input

The only line contains the integers  a and  b (40 ≤  ab ≤ 100).

Output

Output the number of seconds the centipede will need for shoeing in the worst case.

Sample

input output
40 40
120




题意:一只蜈蚣有40条左腿和40条右腿。它有a只左鞋和b只右鞋。穿一只左鞋2s,穿一只右鞋1s,扔鞋1s。问给所有脚都穿上鞋最坏需要多长时间。

解析:贪心一下,看什么情况下会花最长的时间。无非两种情况下时间最长:

1、先拿所有右鞋去试左脚,再穿左脚:2*b + 40;

2、先给39只右脚穿上鞋,再给所有的左脚穿上鞋,然后再把所有的剩下的左鞋去试右脚,然后再穿上右鞋:39 * 2 + 40 + 2 *(a - 40)+ 1;



AC代码:

#include <cstdio>
#include <algorithm>
using namespace std;

int main(){
    int a, b;
    while(scanf("%d%d", &a, &b)==2){
        int x = 2 * b + 40;                   
        int y = 39 * 2 + 40 + 2*(a - 40) + 1;
        printf("%d\n", max(x, y));
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值