【Codeforces】[348A]Mafia

Problem Description

Loading...

Problem Analysis

N people take part in a game. In each round, one person must step out to act as the supervisor, and the remaining N-1 people are players. The i-th person requires that they be a player at least ai times. What is the minimum number of rounds the game must run?

Binary Search on whether x rounds can be completed.

To check a candidate answer x, first add up everyone’s requirements to get sum, then test x*n-sum. If this value is greater than or equal to x, the answer is feasible, because this value represents the maximum number of supervisor turns these people can provide; as long as the number of supervisor turns can reach x, x rounds can be played.

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<stdio.h>
__int64 n,sum;
bool judge(__int64 x) {
    return x*n-sum>=x;
}
int main() {
    while(scanf("%d",&n)!=EOF) {
        __int64 min=0;
        sum=0;
        for(int i=0; i<n; i++) {
            __int64 t;
            scanf("%I64d",&t);
            sum+=t;
            if(min<t)
                min=t;
        }
        __int64 l=min,r=sum,res;
        while(l<=r) {
            __int64 mid=(l+r)/2;
            if(judge(mid)) {
                res=mid;
                r=mid-1;
            } else
                l=mid+1;
        }
        printf("%I64d\n",res);
    }
    return 0;
}

Loading...