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

正式更换编译器为: 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;
}
posted @   RioTian  阅读(174)  评论(0编辑  收藏  举报
编辑推荐:
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 全程不用写代码,我用AI程序员写了一个飞机大战
点击右上角即可分享
微信分享提示

📖目录