您的位置:首页 > 其它

Codeforces #345 Div.2 A Joysticks 暴力模拟

2016-03-08 17:17 441 查看
有两个操纵杆,和他们的初始电量,和一个充电器,但充电器在每一分钟内只能给一个操纵杆充电,操纵杆如果被充电,一分钟会增加1%的电量,如果没被充电,一分钟会消耗2%的电量,充电器的电量是无限的,操纵杆的电量也可超过100%,当一个操纵杆电量只剩1%且下一分钟不被充电的话,就使用完毕了,或者当一个操纵杆的电量到达0%,则自动使用完毕。

给两个初始电量a1,a2,求最长使用时间。

题解,想了想,其实中间过程不管怎么充电都可以,但是当其中一个到达1%或2%时,就必须给它充电,不然就会结束。所以我的方案是,谁的电量少,就给谁充电。

那么,什么时候结束呢?1,当有至少一个是0%时,自动结束了。2,当两个都是1%时,两个都需要充啊,但是只能充一个,也就结束了。

代码:

#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
//freopen("input.txt", "r", stdin);
int a1, a2, ans = 0;
scanf("%d%d", &a1, &a2);
while (1)
{
if ((a1 == 1 && a2 == 1) || a1 == 0 || a2 == 0)
break;
if (a1 <= a2)
{
++a1;
a2 -= 2;
++ans;
}
else
{
++a2;
a1 -= 2;
++ans;
}
}
printf("%d\n", ans);
//while (1);
//system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: