bzoj 1816 二分

思路:二分答案,然后我们贪心地先不填最小的一堆,看在最小的一堆消耗完之前能不能填满其他堆。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PII pair<int, int>
#define y1 skldjfskldjg
#define y2 skldfjsklejg
using namespace std;

const int N = 1e5 + 7;
const int M = 5e5 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 20100403;

int n, m, a[N];

bool check(int x) {
    LL sum = 0;
    for(int i = 1; i <= n; i++)
        if(a[i] < x) sum += x - a[i];
    if(m < sum) return false;
    if(a[1] >= x) return true;
    sum -= x - a[1];
    if(sum <= a[1]) return true;
    return false;
}

int main() {
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; i++)
        scanf("%d", &a[i]);
    sort(a + 1, a + 1 + n);
    int l = 0, r = 1e9, ans = -1;
    while(l <= r) {
        int mid = l + r >> 1;
        if(check(mid)) l = mid + 1, ans = mid;
        else r = mid - 1;
    }
    printf("%d\n", ans);
    return 0;
}

 

posted @ 2018-09-09 00:02  NotNight  阅读(117)  评论(0编辑  收藏  举报