水池

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 ai units tall. Afterwards, you will build a tank around the coral as follows:

  • Pick an integer h1 — 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 格高 ai

如果修了一个高 h 的水池,会先在两侧修高度为 h 的墙壁防止水溢出,然后对每一格注水:

  • 对于第 i 格,需要向其注水 hai 个单位的水,如果 ai>h,则不用注水。
  • 水可以有剩余,但不能少,少了就无法修建高度为 h 的水池。

你一共有 x 个单位的水,问 h 最大能是多少。

By @Larryyu


输入格式

The first line contains a single integer t ( 1t104 ) — the number of test cases.

The first line of each test case contains two positive integers n and x ( 1n2105 ; 1x109 ) — 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 ai ( 1ai109 ) — the heights of the coral.

The sum of n over all test cases doesn't exceed 2105 .

首先说明第一行包含一个整数t,范围是1t104,代表测试用例的数量。

每个测试用例的第一行包含两个正整数nxn表示珊瑚的列数,范围是1n2105x表示可以使用的最大水量,范围是1x109

每个测试用例的第二行包含n个用空格分隔的整数ai,代表珊瑚的高度,范围是1ai109。所有测试用例中n的总和不超过2105


输出格式

For each test case, output a single positive integer h ( h1 ) — 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 ( h1 )—— 水箱能够达到的最大高度 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;
}
posted @   风掣凧浮  阅读(36)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
点击右上角即可分享
微信分享提示