Codeforces 651A Joysticks 【贪心】

来源:互联网 发布:linux 进程调度ppt 编辑:程序博客网 时间:2024/05/01 10:52

题目链接:Codeforces 651A Joysticks

题意:给定两个数nm,你每次可以选择将其中一个数减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;}
0 0
原创粉丝点击