题干:

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?

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 %I64d specifier.

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个人玩游戏,告诉你游戏规则:每轮游戏会有1个裁判,剩下n-1个人当参与者。已知第i个人要求当ai次参与者。。问你至少几轮游戏可以让所有人都心满意足。

解题报告:

二分轮数。如果满足的话,我们让每个人,除了那ai次当参与者,剩下的都当裁判去,如果此时当的裁判数大于轮数,说明这种轮数就可以,(因为我可以让那些多的人在去当参与者嘛,反正他们不嫌多)然后就可以二分了。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
ll a[MAX];
int n;
bool ok(ll x) {ll sum = 0;for(int i = 1; i<=n; i++) sum += (x-a[i]);return sum >= x;}
int main()
{ll l = 0,r = 1e13;cin>>n;for(int i = 1; i<=n; i++) scanf("%lld",a+i),l=max(l,a[i]);ll mid = (l+r)>>1,ans = 0;while(l<=r) {mid=(l+r)>>1;if(ok(mid)) {ans=mid;r=mid-1;}else l=mid+1;}printf("%lld\n",ans);return 0 ;}

两个注意事项:

1.r不能开太大,不能1e13,,因为在ok函数中会越界变成负数就GG了,,所以1e13刚好满足:1e5的n去乘上ok函数中极限状态的x(也就是r的最大取值)要小于longlong的范围、、、所以1e13就很合适、、不能想当然的开个最大1e18!!就炸了!!

2.l不能从0开始取值,而应该从a[i]的最大值  开始。因为如果要从0开始取值,,ok函数中就需要判断如果有一个a[i]<x了就得return 0;因为这就不满足题意,是不可取的轮数!!所以方便起见就在初始化的时候设置好了。。这个和 修路 的那道题 是完全一样的想法。。要先去掉一部分l,,,不要从0开始取,,不然还得在ok函数中return掉,就很麻烦、。(当然也可以AC)

在此也做个总结,二分答案验证的时候,首先要看你的答案 需要满足题目中哪些信息,,然后再决定ok函数中怎么写来一定可以满足这些条件,,比如这个题,几个要求:首先每个人一定要当ai次参与者,剩余的轮数可以随便(也可以当参与者也可以当裁判)其次我要能够成功举办x轮游戏,说明我至少有x个裁判。满足两者点,我们就可以return 1  了。想想其实也不难,是不。

其实啊这题也可以直接O(1)出结果的。因为你看啊其实每次你都在做相同的工作,每次ok函数中都是减掉了所有的a[i],所以直接在main函数中求一个sum=(a[1]+a[2]+....+a[n])。然后求一个方程nx-sum >= x,求解x的最小值就可以了、、、所以啊没必要二分的、、二分就感觉怪怪的,就跟二分求a+b一样。

【CodeForces - 349C】Mafia(思维模拟,优秀的二分)相关推荐

  1. codeforces 808 E. Selling Souvenirs (dp+二分+思维)

    题目链接:http://codeforces.com/contest/808/problem/E 题意:最多有100000个物品最大能放下300000的背包,每个物品都有权值和重量,为能够带的最大权值 ...

  2. CodeForces - 1480C Searching Local Minimum(交互+二分)

    题目链接:点击查看 题目大意:给出一个长度为 nnn 的排列,需要找出一个"局部最小值",所谓"局部最小值"就是对于某个 iii 来说,满足 ai<ai− ...

  3. 【CodeForces - 608C】Chain Reaction (二分 或 dp ,思维)

    题干: 题目大意: 题意是在一条直线上坐落着不同位置的灯塔,每一个灯塔有自己的power level,当作是射程范围.现在从最右边的灯塔开始激发,如果左边的灯塔在这个灯塔的范围之内,那么将会被毁灭.否 ...

  4. Codeforces 895 B XK Segments 思维 二分

    题目链接: http://codeforces.com/contest/895/problem/B 题目描述: 给你长度为n的数列a,然后让你找出所有的对(i,j)满足ai≤aj并且[ai,aj]中能 ...

  5. CodeForces 670D2 Magic Powder - 2(二分+贪心)

    http://codeforces.com/contest/670/problem/D2 简单的二分,二分所有可以做的饼干数,然后遍历就可以啦 #include <iostream> #i ...

  6. Codeforces 1490G - Old Floppy Drive (二分、数学)

    Codeforces Round #702 (Div. 3) G. Old Floppy Drive 题意 给定一个包含nnn个整数的数组{a}\{a\}{a},可以循环延申至无穷个元素(定义编号nn ...

  7. Codeforces 165B Burning Midnight Oil 【二分】

    题目链接:Codeforces 165B Burning Midnight Oil B. Burning Midnight Oil time limit per test2 seconds memor ...

  8. CodeForces - 487B Strip(线段树+dp+二分)

    题目链接:点击查看 题目大意:给出一个长度为 n 的序列,现在要求分成尽可能少的子段,且每个子段需要满足: 最大值与最小值的差值小于等于 s 子段长度大于等于 l 题目分析:dp[ i ] 代表的是前 ...

  9. CodeForces - 1454F Array Partition(线段树+二分)

    题目链接:点击查看 题目大意:给出一个长度为 n 的序列,现在要求求出任意一组 x , y , z,满足下列条件: x + y + z = n max( 1 , x ) = min( x + 1 , ...

最新文章

  1. App icon和启动图的尺寸需求(没有iPad)
  2. 直播系统搭建关键步骤与要点!
  3. 【Windows 逆向】CE 地址遍历工具 ( CE 结构剖析工具 | 尝试进行瞬移操作 | 尝试查找飞天漏洞 )
  4. java获取数据库数据保存到本地txt文件中
  5. axure选中后横线切换_3、开关状态切换 —— Axure实用交互
  6. @SuppressWarnings使用的正确姿势
  7. Spring 阶段总结
  8. 真正智能的语音识别系统离我们还有多远
  9. Reactive Extensions 初识
  10. js-事件处理(重点)
  11. 15 Android系统安全(简要)
  12. Wordnet 与 Hownet 比较
  13. 如何查看android应用签名信息
  14. 东芝Toshiba DP-3003 一体机驱动
  15. C++20新特性—概述
  16. 写贺卡给毕业师姐怎么写计算机系的,[给师姐的毕业祝福语]对师姐的毕业祝福语...
  17. 汇编语言简明教程 实验报告
  18. 在word 页眉插入章编号+标题
  19. DTD和XSD的区别
  20. 分享一些省心的PPT模板下载网站资源(附5G优质PPT模板)

热门文章

  1. 2017安徽二级c语言,2017计算机二级C语言测试题及答案
  2. python选取tensor某一维_Pytorch的Tensor操作(1)
  3. mysql的安装备份恢复_安装使用Percona XtraBackup来备份恢复MySQL的教程
  4. python2发送http不编码_[转]Python 2.x中常见字符编码和解码方面的错误及其解决办法...
  5. threejs指定对象旋转中心
  6. js 正则中冒号代表什么_javascript中正则表达式语法详解
  7. java中检查性异常类_Java异常处理、java语言推崇使用检查类型异常
  8. AM335X 分配大于4M的framebuffer
  9. 用mingw链接msvc生成的库时,无定义chkstk问题的解决
  10. wince6.0开机自启动应用程序