传送门:

http://codeforces.com/problemset/problem/348/A

A. Mafia
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

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 - 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

Copy
3
3 2 2
Output

Copy
4
Input

Copy
4
2 2 2 2
Output

Copy
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个人,其中每个人最少参加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=;
for(int i=;i<n;i++)
{
if(mid<a[i])
return ;//局数还小于玩家想玩的次数 那么局数肯定是不行的
else
cnt+=(mid-a[i]);
}
if(cnt>=mid)//所有玩家当裁判的最大值的和大于局数 意味着可以找到裁判 局数符合要求 但不一定是最小的符合要求的局数 所以需要二分
return ;
return ;
} int main()
{
LL maxc;
LL ans;
LL l,h,mid;
while(cin>>n)
{
maxc=;
for(int i=; i<n; i++)
{
scanf("%I64d",&a[i]);
maxc=max(maxc,a[i]);
}
l=;
h=maxc*;//*10是估算的 边界最多也这么大 找完了还找不到就再扩大点
while(h-l>=)
{
mid=(l+h)/;
if(f(mid))
{
ans=mid;
h=mid-;
}else
{
l=mid+;
}
}
printf("%I64d\n",ans);
}
return ;
}

CodeForces - 348A Mafia (巧妙二分)的更多相关文章

  1. Codeforces 348A Mafia

    题目链接:http://codeforces.com/problemset/problem/348/A 题目大意:N个人中找出1个人主持,剩下N-1个人参与游戏,给出每个人想参与游戏的次数,问要满足每 ...

  2. [Codeforces 1199C]MP3(离散化+二分答案)

    [Codeforces 1199C]MP3(离散化+二分答案) 题面 给出一个长度为n的序列\(a_i\)和常数I,定义一次操作[l,r]可以把序列中<l的数全部变成l,>r的数全部变成r ...

  3. 【Codeforces 348A】Mafia

    [链接] 我是链接,点我呀:) [题意] 每轮游戏都要有一个人当裁判,其余n-1个人当玩家 给出每个人想当玩家的次数ai 请你求出所需要最少的玩游戏的轮数 使得每个人都能满足他们当玩家的要求. [题解 ...

  4. CodeForces 670D1 暴力或二分

    今天,开博客,,,激动,第一次啊 嗯,,先来发水题纪念一下 D1. Magic Powder - 1   This problem is given in two versions that diff ...

  5. codeforces 895B XK Segments 二分 思维

    codeforces 895B XK Segments 题目大意: 寻找符合要求的\((i,j)\)对,有:\[a_i \le a_j \] 同时存在\(k\),且\(k\)能够被\(x\)整除,\( ...

  6. Codeforces 626C Block Towers(二分)

    C. Block Towers time limit per test:2 seconds memory limit per test:256 megabytes input:standard inp ...

  7. codeforces 803D Magazine Ad(二分+贪心)

    Magazine Ad 题目链接:http://codeforces.com/contest/803/problem/D ——每天在线,欢迎留言谈论. 题目大意: 给你一个数字k,和一行字符 例: g ...

  8. Success Rate CodeForces - 807C (数学+二分)

    You are an experienced Codeforces user. Today you found out that during your activity on Codeforces ...

  9. Codeforces 1132D - Stressful Training - [二分+贪心+优先队列]

    题目链接:https://codeforces.com/contest/1132/problem/D 题意: 有 $n$ 个学生,他们的电脑有初始电量 $a[1 \sim n]$,他们的电脑每分钟会耗 ...

随机推荐

  1. 关于TypeScript中null,undefined的使用

    TypeScript本质是javascript,因此基本上js所有的功能在ts上完全可以照搬照抄过来使用.根据ts的文档,有些我觉得值得商榷的——比如null,undefined就是例子. 文档上说一 ...

  2. unity向量-数学-三角函数

    1.如何在unity写cos60 Mathf.Cos(Mathf.Deg2Rad * ) Deg2Rad将 60 角度转换为弧度,因为里面参数只能填弧度数 2.计算一个Vector3绕旋转中心旋转指定 ...

  3. 6、Modal

    1.首先Modal是一个内容窗格.通常用来做一个选择或编辑. 先来看一下 tabs.html 做了什么. /* --- tabs.html ----*/ <ion-navbar *navbar ...

  4. css3轮播渐显效果 2016/11/29

    css3因为其兼容性的问题,被我忽略很久,这次正好做到一个轮播渐显的效果,想了想,正好复习下css3的相关内容,废话不多说,直接上代码. <ul class="cb-slideshow ...

  5. RestTemplate请求出现401错误

    最近遇到一个请求API接口总是报401 Unauthorized错误,起初是认为这个是平台返回的,后来用Postman请求,发现平台其实返回的是一串json,里面带有一些权限验证失败的消息,但到我们代 ...

  6. java中的集合和视图

    一.集合的概念 何为集合,集合就是相当于一个对象的容器.集合是类似数组的一个作用.既然有了数组,为何还要有集合呢,由于数组对象一旦创建,其大小便不可以更改,我们只能往数组中存放创建时数量的对象.而集合 ...

  7. VS中bin,app_code,app_data,app_browser,app_GlobalResources等文件夹的作用 .

    1.  Bin文件夹 Bin文件夹包含应用程序所需的,用于控件.组件或者需要引用的任何其他代码的可部署程序集.该目录中存在的任何.dll文件将自动地链接到应用程序.如果在该文件夹中留有不用的或过期的文 ...

  8. win10下安装pytorch,torchvision

    电脑里以前安装了 tensorflow,现在因为学习需要,需要安装pytorch.还是在原来安装tensorflow的位置安装pytorch. 由于采用在线安装太慢了,而且中途还会因为网速不稳定终端! ...

  9. 学习笔记-java 集合

    背景: 看的是<java核心技术 第8版>,覆盖jdk1.6.主要是对集合全局和细节进行全面掌握,较深入的理解集合.本人对java比较熟悉,但是对于细节的理解不深,知识点还不全,这是知识的 ...

  10. 使用sqlloader向oracle导入文本数据

    文本文件如下,注意文件名必须有后缀,文本行首也需要|分隔符:[oracle@ycr test]$ more person.txt|aaa|123|m|aaa|123|m|aaa|123|m|aaa|1 ...