每日一题 第二十六期 Codeforces Round 936 (Div. 2)

A. Median of an Array

time limit per test: 1 second

memory limit per test: 256 megabytes

input: standard input

output: standard output

You are given an array a a a of n n n integers.

The median of an array q 1 , q 2 , … , q k q_1, q_2, \ldots, q_k q1,q2,,qk is the number p ⌈ k 2 ⌉ p_{\lceil \frac{k}{2} \rceil} p2k, where p p p is the array q q q sorted in non-decreasing order. For example, the median of the array [ 9 , 5 , 1 , 2 , 6 ] [9, 5, 1, 2, 6] [9,5,1,2,6] is 5 5 5, as in the sorted array [ 1 , 2 , 5 , 6 , 9 ] [1, 2, 5, 6, 9] [1,2,5,6,9], the number at index ⌈ 5 2 ⌉ = 3 \lceil \frac{5}{2} \rceil = 3 25=3 is 5 5 5, and the median of the array [ 9 , 2 , 8 , 3 ] [9, 2, 8, 3] [9,2,8,3] is 3 3 3, as in the sorted array [ 2 , 3 , 8 , 9 ] [2, 3, 8, 9] [2,3,8,9], the number at index ⌈ 4 2 ⌉ = 2 \lceil \frac{4}{2} \rceil = 2 24=2 is 3 3 3.

You are allowed to choose an integer i i i ( 1 ≤ i ≤ n 1 \le i \le n 1in) and increase a i a_i ai by 1 1 1 in one operation.

Your task is to find the minimum number of operations required to increase the median of the array.

Note that the array a a a may not necessarily contain distinct
numbers.
Input

Each test consists of multiple test cases. The first line contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1t104) — the number of test cases. Then follows the description of the test cases.

The first line of each test case contains a single integer n n n ( 1 ≤ n ≤ 1 0 5 1 \le n \le 10^5 1n105) — the length of the array a a a.

The second line of each test case contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an ( 1 ≤ a i ≤ 1 0 9 1 \le a_i \le 10^9 1ai109) — the array a a a.

It is guaranteed that the sum of the values of n n n over all test cases does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105.

Output

For each test case, output a single integer — the minimum number of operations required to increase the median of the array.

Example
inputCopy
8
3
2 2 8
4
7 3 3 1
1
1000000000
5
5 5 5 4 5
6
2 1 2 3 1 4
2
1 2
2
1 1
4
5 5 5 5
outputCopy
1
2
1
3
2
1
2
3

Note

In the first test case, you can apply one operation to the first number and obtain the array [ 3 , 2 , 8 ] [3, 2, 8] [3,2,8], the median of this array is 3 3 3, as it is the number at index ⌈ 3 2 ⌉ = 2 \lceil \frac{3}{2} \rceil = 2 23=2 in the non-decreasing sorted array [ 2 , 3 , 8 ] [2, 3, 8] [2,3,8]. The median of the original array [ 2 , 2 , 8 ] [2, 2, 8] [2,2,8] is 2 2 2, as it is the number at index ⌈ 3 2 ⌉ = 2 \lceil \frac{3}{2} \rceil = 2 23=2 in the non-decreasing sorted array [ 2 , 2 , 8 ] [2, 2, 8] [2,2,8]. Thus, the median increased (KaTeX parse error: Expected 'EOF', got '&' at position 3: 3 &̲gt; 2) in just one operation.

In the fourth test case, you can apply one operation to each of the numbers at indices 1 , 2 , 3 1, 2, 3 1,2,3 and obtain the array [ 6 , 6 , 6 , 4 , 5 ] [6, 6, 6, 4, 5] [6,6,6,4,5], the median of this array is 6 6 6, as it is the number at index ⌈ 5 2 ⌉ = 3 \lceil \frac{5}{2} \rceil = 3 25=3 in the non-decreasing sorted array [ 4 , 5 , 6 , 6 , 6 ] [4, 5, 6, 6, 6] [4,5,6,6,6]. The median of the original array [ 5 , 5 , 5 , 4 , 5 ] [5, 5, 5, 4, 5] [5,5,5,4,5] is 5 5 5, as it is the number at index ⌈ 5 2 ⌉ = 2 \lceil \frac{5}{2} \rceil = 2 25=2 in the non-decreasing sorted array [ 4 , 5 , 5 , 5 , 5 ] [4, 5, 5, 5, 5] [4,5,5,5,5]. Thus, the median increased (KaTeX parse error: Expected 'EOF', got '&' at position 3: 6 &̲gt; 5) in three operations. It can be shown that this is the minimum possible number of operations.

In the fifth test case, you can apply one operation to each of the numbers at indices 1 , 3 1, 3 1,3 and obtain the array [ 3 , 1 , 3 , 3 , 1 , 4 ] [3, 1, 3, 3, 1, 4] [3,1,3,3,1,4], the median of this array is 3 3 3, as it is the number at index ⌈ 6 2 ⌉ = 3 \lceil \frac{6}{2} \rceil = 3 26=3 in the non-decreasing sorted array [ 1 , 1 , 3 , 3 , 3 , 4 ] [1, 1, 3, 3, 3, 4] [1,1,3,3,3,4]. The median of the original array [ 2 , 1 , 2 , 3 , 1 , 4 ] [2, 1, 2, 3, 1, 4] [2,1,2,3,1,4] is 2 2 2, as it is the number at index ⌈ 6 2 ⌉ = 3 \lceil \frac{6}{2} \rceil = 3 26=3 in the non-decreasing sorted array [ 1 , 1 , 2 , 2 , 3 , 4 ] [1, 1, 2, 2, 3, 4] [1,1,2,2,3,4]. Thus, the median increased (KaTeX parse error: Expected 'EOF', got '&' at position 3: 3 &̲gt; 2) in two operations. It can be shown that this is the minimum possible number of operations.

AC代码:

#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<queue>
#include<string>
#include<bitset>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<numeric>
#include<iomanip>
#define endl '\n'
using namespace std;

typedef long long ll;
typedef pair<int, int>PII;
const int N=3e5+10;
const int MOD=998244353;
const int INF=0X3F3F3F3F;
const int dx[]={-1,1,0,0,-1,-1,+1,+1};
const int dy[]={0,0,-1,1,-1,+1,-1,+1};
const int M = 1e6 + 10;

int t;
int n;
int a[N];
int main()
{
	cin >> t;
	while(t --){
		int n;
		cin >> n;
		for(int i = 1; i <= n; i++)
		{
			cin >> a[i];
		}
		sort(a + 1, a + 1 + n);
		if(n & 1)
		{
			int cnt = 1;
			for(int i = (n + 1) / 2; i <= n; i ++)
			{
				if(a[i] == a[i + 1] && i != n) cnt ++ ;
				else break;
			}
			cout << cnt << endl;
		}
		else {
			int cnt = 1;
			for(int i = n / 2; i <= n; i ++)
			{
				if(a[i] == a[i + 1] && i != n) cnt ++;
				else break;
			}
			cout << cnt << endl;
		}
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值