CF EDU 126 C - Water the Trees
C - Water the Trees
二分
因为奇数天 +1, 偶数天 +2,所以找到最大的 \(h[i]\) 记为 maxn,最后的高度只能在 maxn, maxn + 1 中,因为如果答案为 maxn + 2,则可以去掉一些操作,使最大高度为 maxn
二分答案即可
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
typedef long long ll;
const int N = 3e5 + 10;
ll h[N];
int n;
bool check(ll x, ll maxn)
{
ll odd = (x + 1) / 2;
ll even = x / 2;
ll t1 = 0, t2 = 0;
for (int i = 1; i <= n; i++)
{
ll t = maxn - h[i];
t1 += t % 2;
t2 += t / 2;
}
if (odd < t1)
return false;
if (odd + even * 2 < t1 + t2 * 2)
return false;
return true;
}
int main()
{
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int T;
cin >> T;
while(T--)
{
cin >> n;
ll maxn = 0;
for (int i = 1; i <= n; i++)
cin >> h[i], maxn = max(maxn, h[i]);
ll l = -1, r = 1e18;
while(l + 1 != r)
{
ll mid = l + r >> 1;
if (check(mid, maxn))
r = mid;
else
l = mid;
}
ll ans = r;
l = -1, r = 1e18;
while(l + 1 != r)
{
ll mid = l + r >> 1;
if (check(mid, maxn + 1))
r = mid;
else
l = mid;
}
ans = min(ans, r);
cout << ans << endl;
}
return 0;
}