codeforces Mafia

qingyezhu發表於2013-10-12
/*
 * Mafia.cpp
 *
 *  Created on: 2013-10-12
 *      Author: wangzhu
 */

/**
 * 每個人都想玩若干場,求至少需要玩幾場才可以滿足大家的需求。
 * 結果必然在某個人想玩的次數nmax(此人想玩的是最多的)與所有人想玩的次數和sum之間,
 * 故二分,left = nmax,right = sum,
 * 只需要需要玩的次數 * (總人數-1) >= 大家想玩的次數和即可
 *
 */
#include<cstdio>
#include<iostream>
using namespace std;
#define LL long long
#define NMAX 100010
int arr[NMAX];
LL binary(int n, int nmax, LL sum) {
    LL mid = -1, left = nmax, right = sum;
    while (left <= right) {
        mid = left + (right - left) / 2;
        if (sum <= (mid * n)) {
            right = mid - 1;
        } else {
            left = mid + 1;
        }
    }
    return left;
}
int main() {
    freopen("data.in", "r", stdin);
    int n, nmax;
    LL sum;
    while(~scanf("%d",&n)) {
        nmax = -1;
        sum = 0;
        for(int i = 0;i < n;i++) {
            scanf("%d",arr + i);
            if(nmax < arr[i]) {
                nmax = arr[i];
            }
            sum += arr[i];
        }
        printf("%I64d\n",binary(n - 1,nmax,sum));

    }
    return 0;
}

 

相關文章