CodeForces 349 C. Mafia

来源:互联网 发布:sql 分页查询 编辑:程序博客网 时间:2024/05/17 00:17

不写不知道自己的二分有多么弱啊大哭

知道思路了还错了近10次...

输入时每人要求的游戏次数存在a数组中

首先我们假设这道题的答案为x

玩了x次游戏,每次有n-1个人玩,因此玩游戏的总数为(n-1)*x

如果x为答案,必然有(n-1)*x>=sum(a[i])

同时也要保证x大于每一个a[i]

在这两个条件的基础上进行二分

二分初始下界为max(a[i]),上界为sum(a[i]);

因为要使(n-1)*x>=sum(a[i]),所以二分类型为向上取整

即:

judge(M)为真时,R=M

judge(M)为假时,L=M-1;

还要说一句:下午的比赛又坑了队友发火

代码如下:

#include <cstdio>#include <iostream>#include <algorithm>#define INF 0x7fffffff#define MAXN 100010#define ll long longusing namespace std;int n;ll a[MAXN];ll sum;bool judge(ll M) {    for(int i=0; i<n; ++i) {        if(M-a[i] < 0)            return false;    }    if(M*(n-1) < sum)        return false;    return true;}int main(void) {    while(cin >> n) {        ll R = 0;        ll L = 0;        ll M = 0;        sum = 0;        for(int i=0; i<n; ++i) {            cin >> a[i];            R += a[i];            L = max(L, a[i]);        }        sum = R;        L = a[n-1];        while(L < R) {            M = (L+R)/2;            if(judge(M)) {                R = M;            }            else    L = M+1;        }        cout << L << endl;    }    return 0;}


0 0