Codeforces 651A Joysticks 【贪心】

本文提供Codeforces651A题目的详细解答过程,通过贪心算法模拟实现,每次选择较大数减2,较小数加1,直至两数之一非正,给出最大操作次数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:Codeforces 651A Joysticks

题意:给定两个数 n m,你每次可以选择将其中一个数减 2 ,另一个加1。直到两个数中至少有一个非正数停止,问最多可以执行的次数。

贪心模拟搞就可以了,每次将较大的减 2

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#define CLR(a, b) memset(a, (b), sizeof(a))
using namespace std;
typedef long long LL;
typedef pair<int, char> pii;
const int MAXN = 1000+10;
const int INF = 0x3f3f3f3f;
void getmax(int &a, int b) {a = max(a, b); }
void getmin(int &a, int b) {a = min(a, b); }
int main()
{
    int x, y; cin >> x >> y;
    int ans = 0;
    while(1)
    {
        if(x > y) swap(x, y);
        if(x == 2 && y == 2) {
            ans++;
            break;
        }
        if(x == 1 && y == 2) {
            ans++;
            break;
        }
        if(x == 1 && y == 1) {
            break;
        }
        if(y & 1) {
            ans += y / 2;
            x += y / 2;
            y = 1;
        }
        else {
            ans += y / 2 - 1;
            x += y / 2 - 1;
            y = 2;
        }
        //cout << x << " " << y << endl;
    }
    cout << ans << endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值