AtCoder Regular Contest 095 C - Many Medians(去掉一个数求中位数)

该博客介绍了AtCoder Regular Contest 095的一道题目,涉及计算在一组偶数个整数中,去掉其中一个数后新的中位数。问题的核心是确定在排除某个特定元素后,序列的中位数如何变化。解决方案指出,根据原始中位数和被移除数字的位置,中位数的变化分为两种情况:如果移除的数字小于等于原中位数,中位数位置将加1;否则,中位数不变。

Problem Statement

When l is an odd number, the median of l numbers a1,a2,…,al is the (

l+1
2

)-th largest value among a1,a2,…,al.

You are given N numbers X1,X2,…,XN, where N is an even number. For each i=1,2,…,N, let the median of X1,X2,…,XN excluding Xi, that is, the median of X1,X2,…,Xi1,Xi+1,…,XN be Bi.

Find Bi for each i=1,2,…,N.

Constraints

  • 2N200000
  • N is even.
  • 1Xi109
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
X1 X2 ... XN

Output

Print N lines. The i-th line should contain Bi.


Sample Input 1

Copy
4
2 4 4 3

Sample Output 1

Copy
4
3
3
4
  • Since the median of X2,X3,X4 is 4B1=4.
  • Since the median of X1,X3,X4 is 3B2=3.
  • Since the median of X1,X2,X4 is 3B3=3.
  • Since the median of X1,X2,X3 is 4B4=4.

Sample Input 2

Copy
2
1 2

Sample Output 2

Copy
2
1

Sample Input 3

Copy
6
5 5 4 4 3 3

Sample Output 3

Copy
4
4
4
4
4
4

题意:去掉第i个数之后的中位数

思路:很容易分析得到,1-n的中位数去掉第i个数之后只有2种情况,原本数字中排序后的中位数和中位数的位数+1,如果当前数字小于等于中位数,则会对结果造成影响,所以最后结果为中位数的位数+1,否则无影响。

#include <bits/stdc++.h>
 
using namespace std;
const int N = 200005;
typedef long long ll;
int a[N];
int b[N];
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        b[i]=a[i];
   }
   sort(a+1,a+1+n);
    int mid=n/2;
    for(int i=1;i<=n;i++){
        if(b[i]<=a[mid])
        printf("%d\n",a[mid+1]);
        else
        printf("%d\n",a[mid]);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值