Codeforces Round 886 (Div. 4) E题 二分答案|.图片纸板

二分答案算法 跳转连接

Mircea has n n n pictures. The i i i-th picture is a square with a side length of s i s_i si centimeters.

He mounted each picture on a square piece of cardboard so that each picture has a border of w w w centimeters of cardboard on all sides. In total, he used c c c square centimeters of cardboard. Given the picture sizes and the value c c c, can you find the value of w w w?

A picture of the first test case. Here c = 50 = 5 2 + 4 2 + 3 2 c = 50 = 5^2 + 4^2 + 3^2 c=50=52+42+32, so w = 1 w=1 w=1 is the answer.

Please note that the piece of cardboard goes behind each picture, not just the border.

Input

The first line contains a single integer t t t ( 1 ≤ t ≤ 1000 1 \leq t \leq 1000 1t1000) — the number of test cases.

The first line of each test case contains two positive integers n n n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \leq n \leq 2 \cdot 10^5 1n2105) and c c c ( 1 ≤ c ≤ 1 0 18 1 \leq c \leq 10^{18} 1c1018) — the number of paintings, and the amount of used square centimeters of cardboard.

The second line of each test case contains n n n space-separated integers s i s_i si ( 1 ≤ s i ≤ 1 0 4 1 \leq s_i \leq 10^4 1si104) — the sizes of the paintings.

The sum of n n n over all test cases doesn’t exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105.

Additional constraint on the input:
Such an integer w w w exists for each test case.

Please note, that some of the input for some test cases won’t fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).

Output

For each test case, output a single integer — the value of w w w ( w ≥ 1 w \geq 1 w1) which was used to use exactly c c c squared centimeters of cardboard.
Example
input

10
3 50
3 2 1
1 100
6
5 500
2 2 2 2 2
2 365
3 4
2 469077255466389
10000 2023
10 635472106413848880
9181 4243 7777 1859 2017 4397 14 9390 2245 7225
7 176345687772781240
9202 9407 9229 6257 7743 5738 7966
14 865563946464579627
3654 5483 1657 7571 1639 9815 122 9468 3079 2666 5498 4540 7861 5384
19 977162053008871403
9169 9520 9209 9013 9300 9843 9933 9454 9960 9167 9964 9701 9251 9404 9462 9277 9661 9164 9161
18 886531871815571953
2609 10 5098 9591 949 8485 6385 4586 1064 5412 6564 8460 2245 6552 5089 8353 3803 3764

output

1
2
4
5
7654321
126040443
79356352
124321725
113385729
110961227

Note

The first test case is explained in the statement.

For the second test case, the chosen w w w was 2 2 2, thus the only cardboard covers an area of c = ( 2 ⋅ 2 + 6 ) 2 = 1 0 2 = 100 c = (2 \cdot 2 + 6)^2 = 10^2 = 100 c=(22+6)2=102=100 squared centimeters.

For the third test case, the chosen w w w was 4 4 4, which obtains the covered area c = ( 2 ⋅ 4 + 2 ) 2 × 5 = 1 0 2 × 5 = 100 × 5 = 500 c = (2 \cdot 4 + 2)^2 \times 5 = 10^2 \times 5 = 100 \times 5 = 500 c=(24+2)2×5=102×5=100×5=500 squared centimeters.

题解

1,题意

米尔恰有n张图片。第 i幅是边长为 si厘米的正方形。他把每幅图片都裱在一块正方形的硬纸板上,这样每幅图片都有w厘米的硬纸板边框。他总共用了c平方厘米的硬纸板。给定图片尺寸和c的值,你能求出w的值吗?让这个几个正方形扩展同样的w外边框最后让这些画框的面积的总和为我们所给出的c 的值.

2,解题思路

一眼王看过去,数据没有特殊的排序规律,题目中也没有明显的数学规律,这个时候我们思考朴素算法,从1e9(因为long long 最高是1018,所以开方之后最多是1e9 ),向后面开始寻找知道找到合适的值就可以停下来了,但是这个复杂度是O(N2) 的,由于这个答案有明显的顺序关系,所以我们这个时候想到二分搜索来简化算法,达到一个O(Nlog2N) 的时间复杂度。

考察

二分答案: 答案有顺序性
存储空间爆炸 : 一旦超过就弹出

3,解题代码

#include <iostream>
#include <cmath>
using namespace std ;
typedef long long LL ;
const int N = 2e5 + 10 ;
int q[N] ;
LL  n , m ;
 
bool vp(LL x )
{
    
    
	LL sum = 0 ;
	x = x * 2 ;
	for(int i = 0 ; i < n ; i ++ )
	{
    
    
		sum += (x+ q[i])* (x+ q[i]) ; 
		if(sum > m) break; 
		//重点:由于这个和非常大,一旦这个值超出之后就要马上的弹出
	}
 
	if(sum <= m)	return true ;
	else return false ;
}
void bsearch(int q[] ,long long l ,long long r )//二分
{
    
    
	
	while(l < r)
	{
    
    	LL mid = ( r + l + 1 ) >> 1 ;
		if(vp(mid))
		{
    
    
			l = mid ;
		}
		else r = mid - 1 ;
 
	}
	cout << l << endl ; 
}
int main ()
{
    
    
	int t ;
	cin >> t ;
	while ( t -- )
	{
    
    
		
		cin >> n >> m ;
		for(int i = 0 ; i < n ; i ++ )
			cin >> q[i] ;
		bsearch( q , 0 , 1e9 );
 
	}
	
 
	return 0 ;
}

猜你喜欢

转载自blog.csdn.net/wen030803/article/details/131900048