Loading

Codeforces Round #706 (Div. 2) B. Max and Mex(思维/模拟)

You are given a multiset S initially consisting of n distinct non-negative integers. A multiset is a set, that can contain some elements multiple times.

You will perform the following operation 𝑘k times:

  • Add the element ⌈(a+b)2⌉ (rounded up) into S, where a=mex⁡(S) and b=max(S). If this number is already in the set, it is added again.

Here maxmax of a multiset denotes the maximum integer in the multiset, and mexmex of a multiset denotes the smallest non-negative integer that is not present in the multiset. For example:

  • mex⁡({1,4,0,2})=3;
  • mex⁡({2,5,1})=0.

Your task is to calculate the number of distinct elements in S after k operations will be done.

Input

The input consists of multiple test cases. The first line contains a single integer t (1≤𝑡≤100) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers n, k (1≤n≤105, 0≤k≤109) — the initial size of the multiset S and how many operations you need to perform.

The second line of each test case contains 𝑛n distinct integers a1,a2,…,an (0≤ai≤109) — the numbers in the initial multiset.

It is guaranteed that the sum of 𝑛n over all test cases does not exceed 105.

Output

For each test case, print the number of distinct elements in S after 𝑘k operations will be done.

Example

input

Copy

5
4 1
0 1 3 4
3 1
0 1 4
3 0
0 1 4
3 2
0 1 2
3 2
1 2 3

output

Copy

4
4
3
5
3

先对数组排序,求出mex。如果操作后得到的数在数列出现过,那么直接输出n(因为再操作也还是得到这个数),如果没在数列出现过则输出n + 1,因为这并不会改变mex和max,再操作还是得到这个数。如果原数列已经被填满,即mex == n,那么就输出n + k(可以自己模拟一下)。

#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
#include <cstdio>
#include <set>
#include <map>
#define int long long
using namespace std;
int a[100005], n, k;
signed main() {
	freopen("data.txt", "r", stdin);
	int t;
	cin >> t;
	while(t--) {
		cin >> n >> k;
		for(int i = 0; i < n; i++) {
			cin >> a[i];
		}
		if(k == 0) {
			cout << n << endl;
			continue;
		}
		sort(a, a + n);
		int mex = 0;
		for(int i = 0; i <= n; i++) {
			if(a[i] > i || i == n) {
				mex = i;
				break;
			}
		}
		int tmp = (int)ceil((mex * 1.0 + a[n - 1] * 1.0) / 2);
		int pos = lower_bound(a, a + n, tmp) - a;
		if(mex == n - 1 + 1) {
			cout << 1ll * n + 1ll * k << endl;
			continue;
		}
		if(a[pos] == tmp) {
			cout << n << endl;
		} else {
			cout << n + 1 << endl;
		}
	}
	return 0;
}
posted @ 2021-03-10 23:10  脂环  阅读(271)  评论(0编辑  收藏  举报