Codeforces Round #577 (Div. 2) C. Maximum Median

Meaning of the questions: is to give a n (odd) elements of the array, its elements can perform k +1 times, in ascending order, the median is the largest number seeking.

After that we drained order, the median element before it can matter, as long as the operation of the median line, we have to determine the median and equal to a few elements, then subtract k it, cycling look, did not jump out when k is less than the reduction 0, that is, the array elements can not be combined with the time.

Ac attached Code:

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<algorithm>
 6 #define ma (int)2e5+1
 7 using namespace std;
 8 int main(){
 9     int n,k,a[ma];
10     cin>>n>>k;
11     for(int i=1;i<=n;i++){
12         cin>>a[i];
13     }
14     if(n==1){
15         cout<<a[1]+k;
16         return 0;
17     }
18     sort(a+1,a+1+n);
19     int i,l=0,q=(n+1)/2;
20     for(i=a[q];k>=0;i++){
21         while(i==a[q+1])q++;
22         k-=q-(1+n)/2+1;
23     }
24     cout<<i-1;
25     return 0;
26 }

因为我这k=0时还会操作,但是其实不能操作了,所以i++后我输出又要减回去;如果循环内操作完k<0,那说明之前执行i++的操作无效,所以也是i减回去,最后都输出i-1.

Guess you like

Origin www.cnblogs.com/warmingtxdy/p/11324809.html