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,…,Xi−1,Xi+1,…,XN be Bi.
Find Bi for each i=1,2,…,N.
Constraints
- 2≤N≤200000
- N is even.
- 1≤Xi≤109
- 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 4, B1=4.
- Since the median of X1,X3,X4 is 3, B2=3.
- Since the median of X1,X2,X4 is 3, B3=3.
- Since the median of X1,X2,X3 is 4, B4=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]);
}
}

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

被折叠的 条评论
为什么被折叠?



