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;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
2020-03-10 Educational Codeforces Round 83 (Rated for Div. 2) C. Adding Powers(数学)
2020-03-10 Educational Codeforces Round 83 (Rated for Div. 2) B. Bogosort(排序/思维)
2020-03-10 Educational Codeforces Round 83 (Rated for Div. 2) A. Two Regular Polygons(水题)