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 ...
随机推荐
- zabbix数据库创建初始化
MariaDB [(none)]> create database zabbix character set utf8; MariaDB [(none)]> grant all privi ...
- java运行Linux命令
<%@ page language="java" import="java.util.*,java.io.*" pageEncoding="UT ...
- 封装class类为jar包提供给其他项目使用
一.完成class类的编写与测试工作 二.完善javadoc注释,生成自己的API 注释要按照标准要求进行 Project -- generate javadoc
- 002 - 配置Pycharm的背景颜色为豆沙绿护眼
本文记录的是Pycharm2017年1月版本 Pycharm有自带的主题可供选择, 好多小伙伴喜欢使用Monoka的风格 但是也有不是很喜欢黑底风格的小伙伴可以使用默认主题+豆沙绿来替代 默认主题是白 ...
- python中通过xlwt、xlrd和xlutils操作xls
xlwt模块用于在内存中生成一个xls/xlsx对象,增加表格数据,并把内存中的xls对象保存为本地磁盘xls文件; xlrd模块用于把本地xls文件加载到内存中,可以读取xls文件的表格数据,查询x ...
- python库学习笔记——分组计算利器:pandas中的groupby技术
最近处理数据需要分组计算,又用到了groupby函数,温故而知新. 分组运算的第一阶段,pandas 对象(无论是 Series.DataFrame 还是其他的)中的数据会根据你所提供的一个或多个键被 ...
- 11.boolean类型
/* 关于java中的Boolean类型: 1)赋给boolean类型的值只有两个:ture或者false 2)boolean类型的数据主要用在逻辑运算和条件控制语句中.*/ public class ...
- js 拦截 窗体关闭事件
<script type="text/javascript"> <!-- window.onbeforeunload = onbeforeun ...
- IReport制作报表——日期时间显示格式
转自:https://blog.csdn.net/linglinglu/article/details/9022679?utm_source=blogxgwz2 IReport工具在制作报表的时候,会 ...
- 安装 Sublime Text 3及其插件
1. 安装 Sublime Text 3 虽然现在的 Sublime 3 还处于 beta 阶段, 但已经非常稳定了, 而且速度比 Sublime 2 得到了增强. Sublime 3 可以到官网下载 ...