codeforces - 651A 题解

Posted 远见望远

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了codeforces - 651A 题解相关的知识,希望对你有一定的参考价值。

大概题意如下:有两个玩具,分别拥有a1的电量和a2的电量;两个玩具每分钟耗电量为2单位,如果有一个或两个的电量下降到1以下就停止游戏;我们还有一个充电器,每分钟只能给一个玩具净充电1单位;问游戏时间长度。

解法很简单,取贪心策略:每次充电给电量较低的玩具充电。

题目做不出来往往是踩了这个坑:如果两个玩具开局电量都是1,那么游戏时间将是0。注意这句话:“ Hence, if at the beginning of minute some joystick is charged by 1 percent, it has to be connected to a charger, otherwise the game stops. ”(好吧我承认我当时在这个坑里爬不出来了)

下面附代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int a,b;
    scanf("%d%d",&a,&b);
    int t=0;
    while(a>0&&b>0)
    {
        if(a==1&&b==1)
        {
            t=0;
            break;
        }
        t+=1;
        if(a<=b)
        {
            a+=1;
            b-=2;
        }
        else
        {
            a-=2;
            b+=1;
        }
    }
    printf("%d\n",t);
    return 0;
}    

  

以上是关于codeforces - 651A 题解的主要内容,如果未能解决你的问题,请参考以下文章

[2016-03-08][codeforces][651][A][Joysticks]

codeforces比赛后怎么看题解和答案

Codeforces 1329 题解

Codeforces Round #805 (Div. 3) 题解

Codeforces Round #479 (Div. 3) 题解

[Codeforces Round #522 (Div. 2, based on Technocup 2019 Elimination Round 3)][C. Playing Piano](代码片段