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 971 (Div. 4) 2009C. The Legend of Freya the Frog Solution

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

Powered by Blogger.