Codeforces 1201C Maximum Median

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个数据,操作数为k,每次可以对一个数据加一,可以得到的最大中间数。

思路

先排序,从中间开始处理,因为前面的数不会影响,每次对当前数加一个值,但不能超过后面的数,即前面的数小于等于后面的数,但要尽量靠近。

代码

#include<cstdio>
#include<algorithm>
#define maxn 200200
typedef long long ll;
using namespace std;
ll a[maxn];
int main()
{
	ll k;
	int n;
	scanf("%d%lld",&n,&k);
	for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
	sort(a+1,a+1+n);
	int i=n/2+1,cnt=1;
	ll ans=a[i];//模拟结果,可以只更新这一个数
//	printf("%lld\n",ans);
	while(k>0&&i+1<=n)
	{
		ll v=min(k/cnt,a[i+1]-ans);//每次需要加上的数值
		ans+=v;
		k-=v*cnt;//总体减去用过的值
		cnt++;
		i++;
	}
	if(k>0)ans+=k/cnt;//(k>=n/2+1)
	printf("%lld\n",ans);
	return 0;
 } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值