您的位置:首页 > 其它

CodeForces - 651A Joysticks ( 不难 但有坑 )

2020-10-14 17:22 447 查看

正式更换编译器为: VS Code

如何配置环境:click here

代码格式化工具:clang-format

A. Joysticks

题目连接:

http://www.codeforces.com/contest/651/problem/A

Description

Friends are going to play console. They have two joysticks and only one charger for them. Initially first joystick is charged at a1 percent and second one is charged at a2 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 by more 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.

Sample Input

3 5

Sample Output

6

题意

你有两个手机,和一个充电器,如果手机插上充电器,每秒涨%1的电,如果不插充电器,每秒掉%2的电

问你最多能维持多久两个手机都有电。

可以超过100%

题解

一:DP

dp[i][j]表示第一个手机有i的电,第二个手机有j的电最长能够坚持多久

然后特判一下1 1的情况就好了。

// Author : RioTian
// Time : 20/10/14
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int dp[320][320], vis[320][320];
int solve(int x, int y) {
if (x > y) swap(x, y);
if (x <= 0) return 0;
if (vis[x][y]) return dp[x][y];
vis[x][y] = 1;
dp[x][y] = max(solve(x - 2, y + 1), solve(x + 1, y - 2)) + 1;
return dp[x][y];
}
int main() {
// freopen("in.txt", "r", stdin);
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int a, b;
cin >> a >> b;
if (a == 1 && b == 1)
cout << 0 << endl;
else
cout << solve(a, b) << endl;
}

二:数学

// Author : RioTian
// Time : 20/10/14
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
// freopen("in.txt","r",stdin);
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int a, b; cin >> a >> b;
cout << ((a + b == 2) ? 0 : a + b - 2 - !(a - b) % 3) << endl;
}

三:模拟

// Author : RioTian
// Time : 20/10/14
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
// freopen("in.txt","r",stdin);
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int a, b, cnt = 0;
cin >> a >> b;
while (a > 0 && b > 0) {
if (a < 2 && b < 2) break;
if (a < b) swap(a, b);
a -= 2, b++, cnt++;
}
cout << cnt << endl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: