二分天数,用一个数字记录如果不施加魔法第 x x x 后每个水稻的高度,然后找到这些水稻的中位数,想要施加魔法的次数最小肯定是都变成中位数的高度,然后判断天数是否够用即可。
const int N = 2e5 + 10;
int n, m, k;
ll a[N], b[N];
bool check(ll x)
{
rep(i, 1, n)
{
b[i] = a[i] + x / n;
if (x % n >= i)
b[i]++;
}
sort(b + 1, b + 1 + n);
ll res = 0;
rep(i, 1, n)
res += abs(b[i] - b[(n + 1) / 2]);
if (res <= x)
return true;
else
return false;
}
int main()
{
int t;
sd(n);
rep(i, 1, n)
sld(a[i]);
ll l = 0, r = INF, mid;
while (l <= r)
{
mid = (l + r) / 2;
if (check(mid))
r = mid - 1;
else
l = mid + 1;
}
pld(l);
return 0;
}