题意:有n个人玩狼人杀,每个人都有最少的局数作为玩家,给定n个玩家至少玩的局数,求至少需要多少局狼人杀可以使所有人玩得尽心。
分析:二分玩的局数x,对于每个人,都至少玩a[i]局,因此,二分的下界为(max(a[1], a[2], ..., a[n])),对于一个分界点,如果((n - 1) * x >= sum),那么这个点就是可行的,(n - 1)是每一局让n - 1个玩家当玩家,另一个玩家当上帝。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
using LL = long long;
const int N = 100005;
int a[N];
LL sum[N];
LL n;
bool check(LL mid)
{
if ((n - 1) * mid >= sum[n])
return true;
return false;
}
int main()
{
cin >> n;
int mx = 0;
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]), mx = max(mx, a[i]);
for (int i = 1; i <= n; ++i) sum[i] = sum[i - 1] + a[i];
LL l = mx, r = 1e14;
while (l < r)
{
LL mid = l + r >> 1;
if (check(mid)) r = mid;
else l = mid + 1;
}
printf("%lld
", l);
return 0;
}