Codeforces Round #725 (Div. 3) G. Gift Set (神奇的二分)

Link

题意:

有 x x x颗红糖, y y y颗蓝糖用来组装成礼包。一个礼包有 a a a个红糖和 b b b个蓝糖或者 a a a个蓝糖和 b b b个红糖。现问有最多能组装成多少个礼包。

Sol:

首先保证: x > y x > y x>y, a > b a > b a>b

然后二分一个方案: s s s个。首先将 x x x和 y y y都减去 b b b,剩下 ( x − b , y − b ) \left ( x- b , y - b\right ) (x−b,y−b),然后就是求 a = a − b a = a - b a=a−b的个数了。首先是 a = = 0 a == 0 a==0显然是可以的;其次是查看剩下除a的到的次数是否能 ≥ s \ge s ≥s

Code:

#include<bits/stdc++.h>
#define int long long
#define IOS ios::sync_with_stdio(false); cin.tie(0);cout.tie(0)
using namespace std;
bool check(int s, int x, int y, int a, int b)
{
    int _x = x, _y = y, _a = a, _b = b;
    if(_x < s * _b || _y < s * _b) return false;
    _x -= s * _b, _y -= s * _b;
    _a -= _b;
    if(_a == 0) return true;
    return _x / _a + _y / _a >= s;
}

void solve()
{
    int x, y, a, b;
    cin >> x >> y >> a >> b;
    if(x < y) swap(x, y);
    if(a < b) swap(a, b);
    int res = 0;
    for(int i = 30; i >= 0; -- i)
    {
        if(res + (1 << i) <= 1e9 && check(res + (1 << i), x, y, a, b)) {
            res += 1 << i;
        }
    }
    cout << res << endl;
}

signed main(){
    IOS;
    int T = 1; 
    cin >> T;
    while(T --) solve();
    return 0;
}
上一篇:Python练习task6:字典与集合


下一篇:D1哪吒开发板wifi连接出错,这是什么情况?