乐逍遥xwl

导航

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

Codeforces Round #577 (Div. 2)

原文链接https://www.cnblogs.com/xwl3109377858/p/11306077.html

 

C - Maximum Median

You are given an array a of n integers, where n is odd. You can make the following operation with it:

  • Choose one of the elements of the array (for example ai) and increase it by 1 (that is, replace it with ai+1).

You want to make the median of the array the largest possible using at most k operations.

The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array [1,5,2,3,5] is 3.

Input

The first line contains two integers n and k (1≤n≤2⋅105, n is odd, 1≤k≤109) — the number of elements in the array and the largest number of operations you can make.

The second line contains n integers a1,a2,…,an (1≤ai≤109).

Output

Print a single integer — the maximum possible median after the operations.

Examples

input

3 2

1 3 5

output

5

input

5 5

1 2 1 1 1

output

3

input

7 7

4 1 2 4 3 4 4

output

5

Note

In the first example, you can increase the second element twice. Than array will be [1,5,5] and it's median is 5.

In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is 3.

In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be [5,1,2,5,3,5,5] and the median will be 5.

 

题意:题目意思是给你n个数(n为奇数),和一个m,你可以进行m次操作:对某个数进行加1.

问你m次操作后能得到的n个数中的最大中位数。

思路:直接二分答案就好了,然后二分的下界是原序列的中位数 num[mid] ,

上界是 num[mid]+k 。

 

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<algorithm>
 6 #include<map>
 7 #include<set>
 8 #include<vector>
 9 #include<queue>
10 #include<list>
11 //#include<unordered_map>
12 #include<stack>
13 using namespace std;
14 #define ll long long 
15 const int mod=1e9+7;
16 const int inf=1e9+7;
17  
18 const int maxn=2e5+10;
19  
20 ll int num[maxn];
21  
22 int n,k;
23  
24 int mid;
25  
26 bool check(ll int x)
27 {
28     ll int sum=0;
29     
30     if(num[mid]>=x)
31         return true;
32     
33     for(int i=mid;i<n;i++)
34     {
35         if(num[i]<x)
36         {
37             sum+=(x-num[i]);
38             
39             if(sum>k)
40                 return false;
41         }
42     }
43     
44     return true;
45 }
46  
47 int main()
48 {
49     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
50     
51     cin>>n>>k;
52     
53     mid=n/2; 
54     
55     for(int i=0;i<n;i++)
56         cin>>num[i];
57     
58     sort(num,num+n);
59     
60     ll int left=num[mid];
61     ll int right=num[mid]+k;
62     
63     ll int midd,ans=0;
64     
65     while(left<=right)
66     {
67         midd=(left+right)>>1;
68         
69         if(check(midd)==true)
70         {
71             ans=midd;
72             left=midd+1;
73         }
74         else
75         {
76             right=midd-1;
77         }
78     }
79     
80     cout<<ans<<endl;
81  
82     return 0;
83 }

 

posted on 2019-08-05 22:36  乐逍遥xwl  阅读(371)  评论(0编辑  收藏  举报