CodeForces - 348A Mafia (巧妙二分)
传送门:
http://codeforces.com/problemset/problem/348/A
One day n friends gathered together to play "Mafia". During each round of the game some player must be the supervisor and other n - 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: 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?
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.
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 %I64d specifier.
3
3 2 2
4
4
2 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次比赛。比赛是这样定义的:n个人之中出1个裁判,其中n-1个人参加
问:最少需要多少场比赛可以满足题目要求?
做法:
把焦点聚集身上在裁判,不要聚集在玩家身上
因为每一局裁判只有一个嘛
假设一共玩了k局,那么某个人当裁判的局数最多就是k-a[i]
(就是他玩够之后一直都在当裁判)
把所有人当裁判的最大值加起来,如果总值大于等于k
说明每一场比赛都可以找到裁判,(意味着k是可以的)
当前k可以就减小k呗,(左移)
当前k不可以就增加k呗(右移)
这就是二分的思想了
#include<bits/stdc++.h> using namespace std; typedef long long LL; #define max_v 100005 LL a[max_v]; int n; int f(LL mid)//判断当前局数是否满足要求 { LL cnt=0; for(int i=0;i<n;i++) { if(mid<a[i]) return 0;//局数还小于玩家想玩的次数 那么局数肯定是不行的 else cnt+=(mid-a[i]); } if(cnt>=mid)//所有玩家当裁判的最大值的和大于局数 意味着可以找到裁判 局数符合要求 但不一定是最小的符合要求的局数 所以需要二分 return 1; return 0; } int main() { LL maxc; LL ans; LL l,h,mid; while(cin>>n) { maxc=0; for(int i=0; i<n; i++) { scanf("%I64d",&a[i]); maxc=max(maxc,a[i]); } l=1; h=maxc*10;//*10是估算的 边界最多也这么大 找完了还找不到就再扩大点 while(h-l>=0) { mid=(l+h)/2; if(f(mid)) { ans=mid; h=mid-1; }else { l=mid+1; } } printf("%I64d\n",ans); } return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南