C. Maximum Median(二分)

C. Maximum Median

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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

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

You want to make the median of the array the largest possible using at most kk 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][1,5,2,3,5] is 33.

Input

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

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

Output

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

Examples

input

Copy

3 2
1 3 5

output

Copy

5

input

Copy

5 5
1 2 1 1 1

output

Copy

3

input

Copy

7 7
4 1 2 4 3 4 4

output

Copy

5

Note

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

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

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][5,1,2,5,3,5,5] and the median will be 55.

题意: 给定一个数组,可每次可以选择一个数加1,共执行b次,问执行b次操作之后这个数组的中位数最大是多少?

**思路:**先将数组排序,前半部分不用做任何处理,从中间开始,利用二分的思想查找最大值,显然,二分的下限为其原数组的中位数,上限为其加b;然后不断二分找最大值。

代码:


//Full of love and hope for life

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <queue>
#include <map>
#define inf 0x3f3f3f3f3f3f3f3f
//https://paste.ubuntu.com/

using namespace std;

typedef long long ll;
const int maxn=2e5+10;

ll a,b;
ll n[maxn];

bool check(ll k){
    ll ans=0;
    int t=a/2+1;
    for(int i=a;i>=t;i--){
        if(k>n[i]){
          ans+=k-n[i];//需要加多少
        }
    }
   if(ans<=b){//满足情况
        return true;
      }
   return false;
}

int main(){
    ll sum=0;
    cin >> a >> b;
    for(int i=1;i<=a;i++){//最好从0开始,不然要特判特殊情况
        cin >> n[i];
    }
    sort(n+1,n+a+1);
    ll l=1;
    ll r=2e9;
    while(l<=r){//二分
        int mid=(l+r)/2;
        if(check(mid)){
            l=mid+1;
            sum=mid;
        }
        else{
            r=mid-1;
        }
    }
    cout << sum;
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZZ --瑞 hopeACMer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值