首页 > 其他分享 ># [Codeforces Round 898 (Div. 4)] E. Building an Aquarium

# [Codeforces Round 898 (Div. 4)] E. Building an Aquarium

时间:2023-09-22 16:47:36浏览次数:50  
标签:Building 10 tank leq 898 Codeforces water int test

Codeforces Round 898 (Div. 4) E. 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 \(h≥1\) — 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 \(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?

Input

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\).

Output

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.

  • Example

Input
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


Output
4
4
2
335
1000000001

思路分析:二分答案标准模版,从0-2e9依次枚举x,直到找到合适的h。使用二分是将左右边界尽量开大,在logn的时间复杂度下,这个数据并不是很大。

#include<iostream>  
using namespace std;  
//#define int long long  
const int N = 2e5 + 20;  
int a[N], n,x;  
int t;  
bool check(int h) {  
	int s = 0;  
	for (int i = 0; i < n; i++)  
	if (h > a[i]) {  
		s = s + (h - a[i]);  
		if (s > x) return false;  
	}  
	return true;  
}  
int main() {  
	cin.tie(0);  
	cout.tie(0);  
	cin >> t;  
	while (t--) {  
		cin >> n >> x;  
		for (int i = 0; i < n; i++) cin >> a[i];  
		int l = 0, r = 1e9+500;  
		while (l < r) {  
			int mid = (l + r + 1) / 2;  
			if (check(mid)) l=mid; //如果mid符合更新l,继续找  
			else r=mid-1;  
		}  
		cout << l << endl;  
	}  
	return 0;  
}

标签:Building,10,tank,leq,898,Codeforces,water,int,test
From: https://www.cnblogs.com/neko333/p/17722789.html

相关文章

  • Codeforces Round 898 (Div. 4)
    CodeforcesRound898(Div.4)A.ShortSort解题思路:遍历所有交换情况,看是否有\(abc\).代码:#include<bits/stdc++.h>usingnamespacestd;usingll=longlong;constintN=1e6+10;constintM=2*M;typedefpair<int,int>pii;#definefifirst#define......
  • Educational Codeforces Round 123 - D(思维题)
    目录D.CrossColoringD.CrossColoring题意$n\timesm$的方格纸上进行q次操作,每次操作选择某整行x和某整列y,使得行x和列y均涂上k种颜色中的一种。问你最终的方案数思路倒序考虑操作,因为对于同一行或者同一列,后面的操作覆盖前面的操作利用数组标记某行或者某......
  • DesignWare Building Block IP学习
    DesignWareBuildingBlock1.基本介绍DesignWareBuildingBlockIP(以下简称DWBB),也叫做FoundationLibrary,是一个紧密集成在Synopsys综合环境中的可重用智能功能块集合。使用DWBB可以在综合时实现透明且高水平的性能优化。DWBB中含有大量组件,可以实现设计重用并显著地提升生......
  • Educational Codeforces Round 154 (Rated for Div. 2) A-D
    传送门:edu154/div2A.PrimeDeletion题意:给定一个0-9的排列,要求一个长度>=2的子序列,使得该子序列是质数做法:考虑31或者13即可。不过当时没有立刻想到,感觉1000以内的质数必有答案,打了暴力。用时就多了点。Code#include<bits/stdc++.h>usingnamespacestd;intpri[10......
  • Educational Codeforces Round 143
    A.TwoTowers#include<bits/stdc++.h>usingnamespacestd;#defineintlonglong#definempmake_pairusingpii=pair<int,int>;usingvi=vector<int>;constexprintinf=1e18;voidsolve(){intn,m,cnt=0;stringa,......
  • Educational Codeforces Round 107
    依然是四题,但是感觉太久没打,好像变得迟钝了。B题大概就是令\[c={10}^k,a=c*3^k,b=c*2^k\]C的话直接暴力维护每种颜色的第一个位置就行,反正只有50个D的话刚开始没什么想法,构造题什么的真的不会啊打表之后发现,对于k,在cost为0的情况下,最多能造出长度为\(k^2+1\)的串,也就是能够......
  • Codeforces Round 897 (Div. 2) A-E
    A.green_gold_dog,arrayandpermutation题意:给出一个长为\(n\)的数组\(a\),找到一个长为\(n\)的排列\(b\),使得\(a\)与\(b\)对应位置上的元素的差尽可能大Solution将数组\(a\)排序,然后令排列\(n,n-1,...,2,1\)对应到对应的元素即可structnode{intx,id;boolope......
  • CodeForces 1863G Swaps
    洛谷传送门CF传送门看到\(a_{a_i}\)和\(a_i\in[1,n]\),果断连边\(i\toa_i\),得到内向基环森林。那么每次相当于把\(a_i\)变成自环,连边\(i\toa_{a_i}\)。但是每次操作都改变图的形态很不好办,考虑打标记。每次\(\operatorname{swap}(a_i,a_{a_i})\),我们就把\(i......
  • Codeforces Global Round 17 A. Anti Light's Cell Guessing
    给一个\(n\timesm\)的网格,里面藏了一个炸弹\((x_0,y_0)\)。你可以选择\(k\)个坐标\((x_1,y_1),(x_2,y_2),\cdots,(x_k,y_k)\)。第\(i\)次选择计算机会回复你一个数\(d_i=|x_0-x_i|+|y_0-y_i|\)。至少需要选出多少个坐标才能确定\((x_0,y_0)\)的位......
  • Codeforces Round 764 (Div. 3) B. Make AP
    有三个正整数\(a,b,c\)。需要执行以下操作严格一次:选择任意一个正整数\(m\)并让严格一个\(a,b,c\)之一乘以\(m\)。但不能改变他们的顺序。回答是否可以经过一次操作后使\(a,b,c\)变为等差。分类讨论题:三种情况满足一种即可。(已知\(a,b,c\geq1\))\(ma......