题意:有两个玩具,分别拥有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;
}