E. Building an Aquarium
Building an Aquarium
Building an Aquarium
题面翻译
你需要修一个水池,水池的底部高低不一,第
如果修了一个高
- 对于第
格,需要向其注水 个单位的水,如果 ,则不用注水。 - 水可以有剩余,但不能少,少了就无法修建高度为
的水池。
你一共有
题目描述
You love fish, that's why you have decided to build an aquarium. You have a piece of coral made of
- Pick an integer
— the height of the tank. Build walls of height on either side of the tank. - Then, fill the tank up with water so that the height of each column is
, unless the coral is taller than ; then no water should be added to this column.
For example, with You can use at most
输入格式
The first line contains a single integer
The first line of each test case contains two positive integers
The second line of each test case contains
The sum of
输出格式
For each test case, output a single positive integer
We have a proof that under these constraints, such a value of
样例 #1
样例输入 #1
5
7 9
3 1 2 4 6 2 5
3 10
1 1 1
4 1
1 4 3 4
6 1984
2 6 5 9 1 8
1 1000000000
1
样例输出 #1
4
4
2
335
1000000001
提示
The first test case is pictured in the statement. With
In the second test case, we can pick
In the third test case, we can pick
题解
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int t, n, x, arr[N];
bool check(int h)
{
long long c = 0;
for(int i = 1; i <= n; i++)
{
if(arr[i] < h)
c += h - arr[i];
}
return c <= x;
}
int main()
{
scanf("%d", &t);
while(t--)
{
scanf("%d %d", &n, &x);
for(int i = 1; i <= n; i++)
scanf("%d", arr + i);
int l = 1, r = 2e9, mid, h;
while(l <= r)
{
mid = (r-l)/2+l;
if(check(mid))
{
l = mid + 1;
h = mid;
}
else
{
r = mid - 1;
}
}
printf("%d\n", h);
}
return 0;
}