AcWing 1227.分巧克力

AcWing 1227.分巧克力

题解

使用整数二分,判定应该向哪边分,最后就是答案

#include <bits/stdc++.h>

using namespace std;
const int N = 100010;
int n, k;
vector<pair<int,int>> a;

int check(int x){
    int res = 0;
    for(auto t : a){
        res += (t.first / x) * (t.second / x); 
    }
    return res;
}

int main(){
    cin >> n >> k;
    for(int i = 0; i < n; ++ i){
        int x, y; cin >> x >> y;
        a.push_back({x, y});
    }
    
    int l = 0, r = N;
    while(l < r){
        int mid = (l + r + 1) >> 1;
        if(check(mid) >= k) l = mid;
        else r = mid - 1;
    }
    
    cout << l << endl;
    return 0;
}
posted @ 2021-01-15 10:03  Lngstart  阅读(68)  评论(0编辑  收藏  举报