CodeForces 1201C C. 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.

谜之爆int,必须开long long ,然后就是从中点开始,把i-mid个数补全到num[i]大小,如果最后还有k,均分到每一个里。

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;
//for(i=1;i
//scanf("%d",&n);
//printf("\n",);
long long num[211111];

int main()
{
    long long i,j,l,n,m,k,t,ans;

    scanf("%lld%lld",&n,&m);

    for(i=0;i<n;i++)
    {
        scanf("%lld",&num[i]);
    }
    sort(num,num+n);

    int mid=n/2;ans=num[mid];

    for(i=mid+1;i<n;i++)
    {
        
        t=m;
        m-=(num[i]-num[i-1])*(i-mid);

        if(m<0)
        break;
        
        ans=num[i];

    }

    if(m>=0)
    printf("%lld",ans+m/(mid+1));
    else
    printf("%lld",ans+t/(i-mid));

    return 0;
}



你可能感兴趣的:(CodeForces 1201C C. Maximum Median(思维))