题目:
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 other n - 1people take part in the game. For each person we know in how many rounds he wants to be a player, not the supervisor: the i-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 contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the i-th number in the list is the number of rounds the i-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 the i-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 the cin, cout streams or the %I64dspecifier.
Examples
input
3
3 2 2
output
4
input
4
2 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个人玩游戏,一个人当裁判,第i个人想玩a[i]盘,问总共玩多少盘能够让每个人满足
题意分析:
第一种方法:贪心,设需要玩x局,那么一共需要人当x局的裁判,
假设有3个人每个人想玩的局数是a、b、c,那么所有人可以当裁判的局数一共有3*x-(a+b+c)
求至少需要玩几盘,那当然是充分利用每个人啦(也就是说完成他想要玩的局数后剩下的局他都不再玩都当裁判),
转换成数学语言就是3*x-(a+b+c)=x;解出x就可以。特别注意x至少要是a、b、c中的最大值。
贪心代码:
/*设需要玩x局,那么一共需要人当x局的裁判,
假设有3个人每个人想玩的局数是a、b、c,那么所有人可以当裁判的局数一共有3*x-(a+b+c),求至少需要玩几盘,
那当然是充分利用每个人啦(也就是说完成他想要玩的局数后剩下的局他都不再玩都当裁判),
转换成数学语言就是3*x-(a+b+c)=x;解出x就可以。特别注意x至少要是a、b、c中的最大值。
*/
#include<bits/stdc++.h>
#define LL long long
#define N 100005
using namespace std;
LL ans=0,n,k,sum=0;
LL a[N];
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
sum+=a[i];
ans=max(ans,a[i]);
}
if(sum%(n-1)==0)
{
ans=max(ans,sum/(n-1));
}
else
{
ans=max(ans,sum/(n-1)+1);
}
/*sort(a+1,a+n+1);
k=(a[1]/(n-1));
ans+=n*k;
a[1]%=(n-1);
ans+=(a[1]+1);
for(int i=2;i<=n;i++)
{
if(i<=(a[1]+1))
{
num=max(num,a[i]-(ans-k-1));
}
else num=max(num,a[i]-(ans-k));
}
cout<<ans+num;*/
cout<<ans;
return 0;
}
第二种方法:二分,把焦点聚集身上在裁判,不要聚集在玩家身上因为每一局裁判只有一个,假设一共玩了k局,那么某个人当裁判的局数最多就是k-a[i](就是他玩够之后一直都在当裁判),把所有人当裁判的最大值加起来,如果总值大于等于k,说明每一场比赛都可以找到裁判,(意味着k是可以的),当前k可以就减小k,(左移),当前k不可以就增加k(右移),这就是二分的思想了
代码:
#include<bits/stdc++.h>
#define LL long long
#define N 100005
using namespace std;
LL n,maxx,l,r,ans;
LL a[N];
bool make(LL x)
{
LL k=0;
for(int i=1;i<=n;i++)
{
if(x<a[i])return 0;
else
{
k+=(x-a[i]);
}
}
if(k>=x)return 1;
else return 0;
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
maxx=max(maxx,a[i]);
}
l=1;
r=maxx*10+1;
while(l<=r)
{
LL mid=(l+r)>>1;
if(make(mid)==0)l=mid+1;
else
{
r=mid-1;
ans=mid;
}
}
cout<<ans;
return 0;
}