Codeforces Round 898 (Div. 4) 1873E - Building an Aquarium Solution


  

Problem Link: https://codeforces.com/problemset/problem/1873/E

Solution in C++:

  1. /// Author : AH_Tonmoy
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. #define int long long
  5. int32_t main() {
  6. ios_base::sync_with_stdio(0);
  7. cin.tie(0);
  8. int t;
  9. cin >> t;
  10. while (t--) {
  11. int n , x ; cin >> n >> x ;
  12. int a[n+1] ;
  13. for ( int i = 0 ; i < n ; i++){
  14. cin >> a[i] ;
  15. }
  16. int low = 1 , high = 2e9 ;
  17. int ans = 0 , mid ;
  18. while(low <= high){
  19. mid = ( low + high ) / 2 ;
  20. int sum = 0 ;
  21. for ( int i = 0 ; i < n ; i++){
  22. if ( a[i] < mid ) sum += (mid - a[i]) ;
  23. if(sum > x) break ;
  24. }
  25. if( sum <= x){
  26. ans = mid ;
  27. low = mid + 1 ;
  28. }
  29. else {
  30. high = mid - 1 ;
  31. }
  32. }
  33. cout << ans << '\n';
  34. }
  35. return 0 ;
  36. }

No comments

Most View Post

Recent post

Codeforces Round 925 (Div. 3) 1931D. Divisible Pairs Solution

    Problem Link  :   https://codeforces.com/contest/1931/problem/D S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. ...

Powered by Blogger.