题目链接:题目
大意:
给定一个数组,你可以任意给它们加上 x x x,求最大 M e x Mex Mex。
思路:
容易知道,给一个元素加上
x
x
x只会影响后面的,另外,我们知道,只有重复多余的数才能加
x
x
x去填补后面的空缺。
我一开始从头到尾遍历,发现有重复的就不断加
x
x
x,直到不与其他数重叠,然而超时了,然后我们发现不用一个个加,只用储存它模
x
x
x,存到哈希表里,再访问它模
x
x
x就行了,这是由于加
x
x
x的任意性。
代码:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define MOD 1000000007
#define fi first
#define se second
#define pii pair<int,int>
#define vec vector
void solve(){
int n, x;
cin >> n >> x;
vec<int> a(n);
for(int i = 0; i < n; i++){
cin >> a[i];
}
unordered_map<int, int> mp, mp2;
sort(a.begin(), a.end());
for(int num : a){
mp[num]++;
}
for(int i = 0; i <= n; i++){
if(mp[i] > 1){
mp2[i % x] += mp[i] - 1;
}
if(!mp[i] && mp2[i % x]){
mp[i]++;
mp2[i % x]--;
}
}
//cout << mp[1];
for(int i = 0; i <= n; i++){
if(!mp[i]){
cout << i << '\n';
return;
}
}
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t=1;
cin >> t;
while(t--){
solve();
}
return 0;
}