Loading

P1843 奶牛晒衣服(二分)

思路:就是一个模板,只是找最小化而已。在判断函数里面:当湿度<=x*A不判断, 反之sum+=(a[i]-x*A)/B+(a[i]-x*A)%B?1:0;

#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn = 5e5 + 10;
int a[maxn], n, A, B, maxx, ans, mid;

bool check(int x){
    int sum = 0;
    for (int i = 1; i <= n;++i)
    if (a[i] > x*A){
        sum += (a[i] - x*A) / B;
        if ((a[i] - x*A) % B)sum++;
    //    cout << i << " a=" << a[i] << " a-x*A=" << a[i] - x*A << endl;
    }
//    cout << "sum=" << sum << endl;
    return sum <= x;
}

void half(){
    int l = 1, r = maxx;
    while (l <= r){
        mid = (l + r) >> 1;
    //    cout << "l=" << l << " mid=" << mid << " r=" << r << endl;
        if (check(mid)){ ans = mid; r = mid - 1; }
        else l = mid + 1;
    }
}

int main(){
    cin >> n >> A >> B;
    for (int i = 1; i <= n; ++i)
        cin >> a[i], maxx = max(maxx, a[i]);
    half();    //二分
    cout << ans << endl;
}

 

posted @ 2019-03-01 22:01  青山新雨  阅读(397)  评论(0编辑  收藏  举报