Codeforces Round #202 (Div. 1) A. Mafia 推公式 + 二分答案
http://codeforces.com/problemset/problem/348/A
2 seconds
256 megabytes
standard input
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?
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).
这题用了组合数学的表示方法,这个方法经常用而且很好用,其实就是设未知数。
设x[i]表示第i个人,做了多少次裁判,那么ans = sigma(x[i])
对于第i个人,它参赛了ans - x[i]次,那么需要
ans - x[1] >= a[1]
ans - x[2] >= a[2]
ans - x[3] >= a[3]
.....
ans - x[n] >= a[n]
求和,得到(n - 1) * ans >= sum(a[i])
二分ans即可,
注意ans要大于等于max(a[i])
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = 1e5 + ;
LL a[maxn], sum;
LL mx;
int n;
bool check(LL ans) {
return ans >= (LL)ceil(1.0 * sum / (n - )) && ans >= mx;
}
void work() {
cin >> n;
for (int i = ; i <= n; ++i) {
cin >> a[i];
sum += a[i];
mx = max(mx, a[i]);
}
LL be = , en = 1e18L;
while (be <= en) {
LL mid = (be + en) >> ;
if (check(mid)) {
en = mid - ;
} else be = mid + ;
}
cout << be << endl;
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
IOS;
work();
return ;
}
Codeforces Round #202 (Div. 1) A. Mafia 推公式 + 二分答案的更多相关文章
- Codeforces Round #202 (Div. 1) A. Mafia 贪心
A. Mafia Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/348/problem/A D ...
- Codeforces Round #402 (Div. 2) D. String Game(二分答案水题)
D. String Game time limit per test 2 seconds memory limit per test 512 megabytes input standard inpu ...
- Codeforces Round #402 (Div. 2) D题 【字符串二分答案+暴力】
D. String Game Little Nastya has a hobby, she likes to remove some letters from word, to obtain anot ...
- Codeforces Round #365 (Div. 2) C - Chris and Road 二分找切点
// Codeforces Round #365 (Div. 2) // C - Chris and Road 二分找切点 // 题意:给你一个凸边行,凸边行有个初始的速度往左走,人有最大速度,可以停 ...
- Codeforces Round #202 (Div. 2)
第一题水题但是wa了一发,排队记录下收到的25,50,100,看能不能找零,要注意100可以找25*3 复杂度O(n) 第二题贪心,先找出最小的花费,然后就能得出最长的位数,然后循环对每个位上的数看能 ...
- Codeforces Round #127 (Div. 1) E. Thoroughly Bureaucratic Organization 二分 数学
E. Thoroughly Bureaucratic Organization 题目连接: http://www.codeforces.com/contest/201/problem/E Descri ...
- Codeforces Round #202 (Div. 2) B,C,D,E
贪心 B. Color the Fence time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- Codeforces Round #202 (Div. 1) D. Turtles DP
D. Turtles Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/547/problem/B ...
- Codeforces Round #271 (Div. 2)D(递推,前缀和)
很简单的递推题.d[n]=d[n-1]+d[n-k] 注意每次输入a和b时,如果每次都累加,就做了很多重复性工作,会超时. 所以用预处理前缀和来解决重复累加问题. 最后一个细节坑了我多次: print ...
随机推荐
- Python: PS 滤镜--水波特效
本文用 Python 实现 PS 滤镜中的 水波特效 import numpy as np from skimage import img_as_float import matplotlib.pyp ...
- 基于aspect实现AOP——xml配置的其他操作
将上方配置中的前置通知,可换成环绕通知
- Nwjs简单配置
1.创建一个工程,配置一个 package.json 文件 { "name": "application-name", "version" ...
- for循环的一个注意点
unsigned int i =10; for(i;i > 0; i--) { xxxxx } 因为i是unsigned int 类型的,永远不可能小于0,也就是说是个死循环了.
- Beyond Compare 简体版+注册码
Beyond Compare 3.3.4.14431 官方简体版+注册码 查阅全文 ›
- 推荐 BI Work
推荐阅读 BI Work 的文章,作为学习用 http://www.cnblogs.com/biwork
- 可移植的配置visual studio工程第三方库
现在编程有太多的好用的第三方库,例如 计算机视觉方面的opencv c++的扩充库boost 特殊的第三方库,相机库,通讯库等 使用这些库给我们带来了极大的便利,同时也有很多困扰.这个工程在我电脑上明 ...
- HDOJ-1391
Number Steps Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- TypeScript完全解读(26课时)_8.ES6精讲-ES6中的类(进阶)
8.TypeScript完全解读-ES6精讲-类(进阶) 在index.ts内引入 Food创建的实例赋值给Vegetabled这个原型对象,这样使用Vegetables创建实例的时候,就能继承到Fo ...
- 11.ClientCredential模式总结
11.ClientCredential模式总结 服务端定义的Resource叫做api Resource API默认是被保护的 第三方的客户端先去请求 Server拿到access token.带着t ...