Codeforces A. Shovels and Swords (思维 / 均分) (Round 89 Rated for Div2)

传送门

题意: 你有a,,b两个数,可在其中一个数取1,另一个数取2;试问你最多能进行多少次操作?
Codeforces A. Shovels and Swords (思维 / 均分) (Round 89 Rated for Div2)_第1张图片
思路:

  • 交换使得a是大数,b是小数。
  • 在a中建2,b中减1直到两者相等。
  • a,b两数同取3.
  • 最后若a和b都还有余就可再操作一次。

代码实现:

#include
#define endl '\n'
#define null NULL
#define ll long long
#define int long long
#define pii pair
#define lowbit(x) (x &(-x))
#define ls(x) x<<1
#define rs(x) (x<<1+1)
#define me(ar) memset(ar, 0, sizeof ar)
#define mem(ar,num) memset(ar, num, sizeof ar)
#define rp(i, n) for(int i = 0, i < n; i ++)
#define rep(i, a, n) for(int i = a; i <= n; i ++)
#define pre(i, n, a) for(int i = n; i >= a; i --)
#define IOS ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);
const int way[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
using namespace std;
const int  inf = 0x7fffffff;
const double PI = acos(-1.0);
const double eps = 1e-6;
const ll   mod = 1e9 + 7;
const int  N = 2e5 + 5;

int t, a, b;

signed main()
{
    IOS;

    cin >> t;
    while(t --){
        cin >> a >> b;
        if(a < b) swap(a, b);
        if(!a || !b || a < 2 && b < 2){
            cout << 0 << endl;
            continue;
        }
        int tmp = min(a - b, b);
        b -= tmp;a -= tmp * 2;
        int minn = min(a, b) / 3;
        a -= minn * 3; b -= minn * 3;
        cout << tmp + minn * 2 + (a >= 2 && b >= 1) << endl;
    }

    return 0;
}

你可能感兴趣的:(比赛&训练,数学)