Building an Aquarium
題目描述
You love fish, that's why you have decided to build an aquarium. You have a piece of coral made of $ n $ columns, the $ i $ -th of which is $ a_i $ units tall. Afterwards, you will build a tank around the coral as follows:
- Pick an integer $ h \geq 1 $ — the height of the tank. Build walls of height $ h $ on either side of the tank.
- Then, fill the tank up with water so that the height of each column is $ h $ , unless the coral is taller than $ h $ ; then no water should be added to this column.
For example, with $ a=[3,1,2,4,6,2,5] $ and a height of $ h=4 $ , you will end up using a total of $ w=8 $ units of water, as shown. You can use at most $ x $ units of water to fill up the tank, but you want to build the biggest tank possible. What is the largest value of $ h $ you can select?
你需要修一個水池,水池的底部高低不一,第 \(i\) 格高 \(a_i\)。
如果修了一個高 \(h\) 的水池,會先在兩側修高度為 \(h\) 的牆壁防止水溢位,然後對每一格注水:
- 對於第 \(i\) 格,需要向其注水 \(h-a_i\) 個單位的水,如果 \(a_i>h\),則不用注水。
- 水可以有剩餘,但不能少,少了就無法修建高度為 \(h\) 的水池。
你一共有 \(x\) 個單位的水,問 \(h\) 最大能是多少。
By @Larryyu
輸入格式
The first line contains a single integer $ t $ ( $ 1 \leq t \leq 10^4 $ ) — the number of test cases.
The first line of each test case contains two positive integers $ n $ and $ x $ ( $ 1 \leq n \leq 2 \cdot 10^5 $ ; $ 1 \leq x \leq 10^9 $ ) — the number of columns of the coral and the maximum amount of water you can use.
The second line of each test case contains $ n $ space-separated integers $ a_i $ ( $ 1 \leq a_i \leq 10^9 $ ) — the heights of the coral.
The sum of $ n $ over all test cases doesn't exceed $ 2 \cdot 10^5 $ .
首先說明第一行包含一個整數\(t\),範圍是\(1\leq t\leq10^4\),代表測試用例的數量。
每個測試用例的第一行包含兩個正整數\(n\)和\(x\),\(n\)表示珊瑚的列數,範圍是\(1\leq n\leq2\cdot10^5\),\(x\)表示可以使用的最大水量,範圍是\(1\leq x\leq10^9\)。
每個測試用例的第二行包含\(n\)個用空格分隔的整數\(a_i\),代表珊瑚的高度,範圍是\(1\leq a_i\leq10^9\)。所有測試用例中\(n\)的總和不超過\(2\cdot10^5\)。
輸出格式
For each test case, output a single positive integer $ h $ ( $ h \geq 1 $ ) — the maximum height the tank can have, so you need at most $ x $ units of water to fill up the tank.
We have a proof that under these constraints, such a value of $ h $ always exists.
對於每個測試用例,輸出一個正整數 $ h $ ( $ h \geq 1 $ )—— 水箱能夠達到的最大高度 $ x $ ,這樣你最多需要單位的水來填滿水箱。
我們有一個證明,在這些約束條件下,這樣的值 $ h $ 總是存在的。
樣例 #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 $ h=4 $ we need $ 8 $ units of water, but if $ h $ is increased to $ 5 $ we need $ 13 $ units of water, which is more than $ x=9 $ . So $ h=4 $ is optimal.
In the second test case, we can pick $ h=4 $ and add $ 3 $ units to each column, using a total of $ 9 $ units of water. It can be shown that this is optimal.
In the third test case, we can pick $ h=2 $ and use all of our water, so it is optimal.
第一個測試案例如語句所示。在 h=4 的情況下,我們需要 8 個單位的水,但如果 h 增加到 5 ,我們需要 13 個單位的水,比 x=9 多。因此, h=4 是最佳方案。
在第二個測試案例中,我們可以選擇 h=4 ,並在每列中新增 3 個單位,總共使用 9 個單位的水。可以證明這是最佳方案。
在第三個測試案例中,我們可以選擇 h=2 ,並使用所有的水,因此這是最優方案。
題解
和上一題砍樹類似,用二分查詢即可
#include <cstdio>
int main()
{
int t;scanf("%d", &t);
while (t--)
{
int left = 0, mid = 0;
int n, x; scanf("%d %d", &n, &x);
int a[200005] = {0};
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
long long right = 2e9+10;
while (left <= right)
{
mid = left + (right - left)/2;
long long sum = 0;
for (int i = 0; i < n; i++)
if (mid >= a[i]){sum += mid - a[i];if(sum > x)break;}
if (sum > x)right = mid-1;
else left = mid + 1;
}
printf("%d\n", right);
}
return 0;
}