Codeforces Round 886 (Div. 4) 1850E - Cardboard for Pictures Solution
Problem Link: https://codeforces.com/problemset/problem/1850/E
Solution in C++:
- /// Author : AH_Tonmoy
- #include <bits/stdc++.h>
- using namespace std;
- #define int long long
- const int mx = 2e5 + 9 ;
- int a[mx] , n , c ;
- bool ok (int w ){
- int total = 0 ;
- for( int i = 0 ; i < n ; i++){
- int side = a[i] + 2 * w ;
- int area = side * side ;
- if (total + area >= c) return true ;
- total += area ;
- }
- return false ;
- }
- int32_t main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- int t;
- cin >> t;
- while (t--) {
- cin >> n >> c ;
- for ( int i = 0 ; i < n ; i++){
- cin >> a[i] ;
- }
- int low = 1 , high = 1e9 + 9 ;
- int ans = 0 ;
- while(low <= high){
- int mid = ( low + high ) / 2 ;
- if(ok(mid)){
- ans = mid ;
- high = mid - 1 ;
- }
- else{
- low = mid + 1 ;
- }
- }
- cout << ans <<'\n' ;
- }
- return 0 ;
- }
No comments