Joysticks

来源:互联网 发布:linux没有rc.d 编辑:程序博客网 时间:2024/06/16 10:00

Friends are going to play console. They have two joysticks and only one charger for them. Initially first joystick is charged ata1 percent and second one is charged ata2 percent. You can connect charger to a joystick only at the beginning of each minute. In one minute joystick either discharges by 2 percent (if not connected to a charger) or charges by 1 percent (if connected to a charger).

Game continues while both joysticks have a positive charge. 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. If some joystick completely discharges (its charge turns to 0), the game also stops.

Determine the maximum number of minutes that game can last. It is prohibited to pause the game, i. e. at each moment both joysticks should be enabled. It is allowed for joystick to be charged bymore than 100 percent.

Input

The first line of the input contains two positive integers a1 and a2 (1 ≤ a1, a2 ≤ 100), the initial charge level of first and second joystick respectively.

Output

Output the only integer, the maximum number of minutes that the game can last. Game continues until some joystick is discharged.

Example
Input
3 5
Output
6
Input
4 4
Output
5
之前理解错了题目,一分钟内游戏能够进行,说的就是在这一分钟内两者的点都为positive,也就是说结尾的时候两者的电量都为positive。所以我们需要来想这分钟开始的点,两者的电量,只要一个人的电量>=2,就是说明这个人可以结束这一分钟,我们再来考虑另外一个人,如果开始是0的话,肯定是不行的,所以如果是1的话,我们想是可以的,如果>1的话肯定都是可以的。所以我们的终止条件就是上述。
#include <iostream>#include<cstdio>#include<algorithm>using namespace std;int main(){    int a,b;    scanf("%d %d",&a,&b);    int ans=0;    while((a>=2&&b!=0)||(b>=2&&a!=0))    {        ans++;        if(a<b)        {            a+=1;            b-=2;        }        else        {            b+=1;            a-=2;        }    }    printf("%d\n",ans);    return 0;}


原创粉丝点击