Maximum Median(二分)

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

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][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,问k次操作后,数组的中位数最大为多少。

思路:一般输出就一个数,且为最大值或者最小值的,就用二分枚举答案,再判断枚举出的答案是否符合题意。

 

代码:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const int maxn = 2e5+100;
ll a[maxn];
ll ans,n,k;

int check( ll mid )
{
    ll last = 0;
    for ( int i=n/2+1; i<=n; i++ ) {
        if ( a[i]<=mid ) {
            last += (mid-a[i]);
        }
    }

    if ( last<=k ) {
        return 1;
    }
    else {
        return 0;
    }
}

int main()
{
    int i;
    cin >> n >> k;
    for ( i=1; i<=n; i++ ) {
        cin >> a[i];
    }
    sort(a+1,a+n+1);
    ll l = 1;
    ll r = 3e9;
    while ( l<=r ) { 
        ll mid = (l+r)/2;  // 枚举答案,中位数为mid是否符合题意
        if ( check(mid)==1 ) {
            l = mid+1;
            ans = mid;
        }
        else {
            r = mid-1;
        }
    }
    cout << ans << endl;

    return 0;
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sen-Median趋势分析方法是一种用于描述和分析时间序列数据的统计工具。它通过将数据按照位置进行排序,确定数据的中间位置,将中间位置的数据作为时间序列的趋势模型。 Sen-Median趋势分析方法主要分为以下几个步骤:首先,将时间序列数据按照时间顺序进行排序。然后,计算出各个时间点数据的斜率值,即通过计算两个数据值的差值来衡量数据的变化趋势。接下来,找到数据斜率值的中位数,作为代表数据整体趋势的中间趋势。最后,根据中间趋势值来绘制趋势线,以展示数据的整体趋势。 Sen-Median趋势分析方法具有以下几个特点:首先,它能够减轻极端值对趋势分析结果的影响,使得趋势模型更加平滑和稳定。其次,这种方法不依赖于分布假设,可以适用于不同类型的数据。第三,该方法计算简单,不需要对数据进行复杂的数学计算,比较易于理解和使用。 Sen-Median趋势分析方法在实际应用中具有广泛的应用。例如,在金融领域,可以使用该方法来分析股票价格的趋势,帮助投资者做出决策。在气象学中,可以利用该方法来研究气温变化的趋势,以预测天气情况。此外,在经济学、环境科学等领域,Sen-Median趋势分析方法也有着广泛的应用。 总之,Sen-Median趋势分析方法是一种用于揭示时间序列数据趋势的统计分析工具,它的特点是稳定、简单易懂,广泛应用于各个领域。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值