Codeforces 349C Mafia【二分+思维判定】
来源:互联网 发布:淘宝店运营方案 编辑:程序博客网 时间:2024/04/30 15:26
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?
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.
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.
33 2 2
4
42 2 2 2
3
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); }}
- Codeforces 349C Mafia【二分+思维判定】
- codeforces 349C Mafia [贪心]/[二分答案]
- 【CodeForces】348A - Mafia(二分 & 思维)
- Codeforces 348A:Mafia(二分+思维)
- CodeForces 349 C. Mafia
- Codeforces 349C - Mafia
- Codeforces Round #202 (Div. 2)C. Mafia(思维)
- codeforces 626C 二分判定
- CF#348 A. Mafia(二分,思维)
- CodeForces 348A Mafia (思维)
- Codeforces 672D Robin Hood 二分判定+思维
- Codeforces 348A - Mafia (二分答案)
- CodeForces 348A Mafia(二分)
- Codeforces 460C Present【二分+思维优化】
- Codeforces Round #360 (Div. 2) 前三题题解【简单模拟+思维+二分图判定二分染色】
- Codeforces Round #202 (Div. 2) C. Mafia
- Codeforces Round #202 (Div. 2)C. Mafia
- codeforces C. NP-Hard Problem 二分图的判定
- PAT 乙级 1004
- HashSet
- [51Nod 1816] 小C的二分图 口胡
- 浅析TF-GSC
- 【论文阅读笔记】CVPR2015-Long-term Recurrent Convolutional Networks for Visual Recognition and Description
- Codeforces 349C Mafia【二分+思维判定】
- 1001. 害死人不偿命的(3n+1)猜想 (15)
- 蛋白质结构预测(swiss-model,modeller )
- node中https请求
- 第七届蓝桥杯大赛个人赛省赛(软件类)A组第三题 方格填数
- 将web02项目配置上spring--applicationContext.xml
- Servlet中的HttpServletRequest
- C++四种类型转换机制
- block块对象