Codeforces 348A Mafia

题目链接:http://codeforces.com/problemset/problem/348/A

题目大意:N个人中找出1个人主持,剩下N-1个人参与游戏,给出每个人想参与游戏的次数,问要满足每个人最少要玩多少轮游戏。

 我的算法会烦一点:
首先找出所有人中想参与游戏的最大次数max,即可能的最小解ans。
然后用这个max去减每个人想要参与游戏的次数,得到在max次游戏中每个人能够当主持的次数。把这些次数加起来得到b,与max的大小进行比较,如果b>=max则说明满足了每个人人的期望。
否则,ans+1,然后b+n-1,(即除了想参与max次数的人当主持的次数都+1),再进行比较,直到找到b>=m的情况,这时候的ans就是最终结果。

P.S:有多个max的情况也不影响结果。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<cmath>
 5 #include<algorithm>
 6 using namespace std;
 7 long long a[100005];
 8 long long b[100005];
 9 int main()
10 {
11     int n;
12     scanf("%d",&n);
13     int i,j;
14     long long Max=0,all=0,ans;
15     for(i=0;i<n;i++)
16     {
17         scanf("%I64d",&a[i]);
18         if(Max<a[i])
19         {
20             Max=a[i];
21         }
22     }
23     for(i=0;i<n;i++){
24         b[i]=Max-a[i];
25         all+=b[i];
26     }
27     if(all>Max)
28         ans=Max;
29     else
30     {
31         ans=Max+(Max-all+n-2)/(n-1);
32     }
33     printf("%d
",ans);
34     return 0;
35 }
View Code
原文地址:https://www.cnblogs.com/wuwing/p/3344372.html