Codeforces 349C Mafia【二分+思维判定】

来源:互联网 发布:淘宝店运营方案 编辑:程序博客网 时间:2024/04/30 15:26

C. Mafia
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

One day n friends gathered together to play "Mafia". During each round of the game some player must be the supervisor and othern - 1 people take part in the game. For each person we know in how many rounds he wants to be a player, not the supervisor: thei-th person wants to play ai rounds. What is the minimum number of rounds of the "Mafia" game they need to play to let each person play at least as many rounds as they want?

Input

The first line contains integer n (3 ≤ n ≤ 105). The second line containsn space-separated integers a1, a2, ..., an(1 ≤ ai ≤ 109) — thei-th number in the list is the number of rounds thei-th person wants to play.

Output

In a single line print a single integer — the minimum number of game rounds the friends need to let thei-th person play at least ai rounds.

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use thecin, cout streams or the%I64d specifier.

Examples
Input
33 2 2
Output
4
Input
42 2 2 2
Output
3
Note

You don't need to know the rules of "Mafia" to solve this problem. If you're curious, it's a game Russia got from the Soviet times:http://en.wikipedia.org/wiki/Mafia_(party_game).


题目大意:

游戏规则:一共有N个人参加游戏,每轮游戏需要有一个人当主管,其余人参与到游戏中,每个人想要参与到游戏中的轮次为Ai次;

问最少多少轮次游戏,能够使得所有人都满足这个条件。


思路:


显然轮次越多,越可以满足所有人的条件,这里包含一个单调性,我们可以二分答案。

那么要如何判定当前mid轮次是否能狗满足所有人的期望呢?

显然一个人现在最少想参与Ai轮次,那么他可以做主管的轮次就是mid-Ai轮次,那么如果Σmid-Ai>=mid.有足够多的人足够多的轮次去当主管即可。

那么我们过程维护一下。

注意不要设定R特别大,可能会爆LL.

同时也不要设定R小了,会Wa.


Ac代码:

#include<stdio.h>#include<string.h>#include<iostream>using namespace std;#define ll __int64ll a[100500]; ll n;ll Slove(ll mid){    ll cnt=0;    for(ll i=0;i<n;i++)    {        if(mid<a[i])return 0;        else cnt+=mid-a[i];    }    if(cnt>=mid)return 1;    else return 0;}int main(){    while(~scanf("%I64d",&n))    {        ll maxn=0;        for(ll i=0;i<n;i++)scanf("%I64d",&a[i]),maxn=max(maxn,a[i]);        ll ans;        ll l=1;        ll r=maxn*10;        while(r-l>=0)        {            ll mid=(l+r)/2;            if(Slove(mid)==1)            {                ans=mid;                r=mid-1;            }            else l=mid+1;        }        printf("%I64d\n",ans);    }}







0 0
原创粉丝点击