Codeforces Round 577 (Div. 2) 1201C. Maximum Median Solution
Problem Link : https://codeforces.com/contest/1201/problem/C
Solution in C++:
- /// Author : AH_Tonmoy
- #include <bits/stdc++.h>
- using namespace std;
- int n , k ;
- int a[200009] ;
- bool bs_f(int x){
- int min_operation = 0 ;
- for ( int i = (n + 1 )/ 2 ; i <= n ; i++){
- if (a[i] < x ) {
- min_operation += ( x - a[i]) ;
- }
- if (min_operation > k ) return 0 ;
- }
- return 1 ;
- }
- int32_t main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cin >> n >> k ;
- for ( int i = 1 ; i <= n ; i++){
- cin >> a[i] ;
- }
- sort(a + 1 , a + n + 1 ) ;
- int l = 0 , r = 2e9 , ans = 0 ;
- while( l <= r ){
- int mid = l + (r - l ) / 2 ;
- if(bs_f(mid)){
- ans = mid ;
- l = mid + 1 ;
- }
- else {
- r = mid - 1 ;
- }
- }
- cout << ans <<'\n' ;
- return 0 ;
- }
No comments