题目描述:
题目分析:
这个题我的做法是用一个map与并查集,因为对于每个位置的答案要求:找到大于等于原编号的且未被使用的一个数,所以可以用并查集来维护;代码如下:需要注意的是需要用ios加速,会卡时间的。
#include <bits/stdc++.h>
using namespace std;
#define int long long
unordered_map<int,int> f;
int find(int x){
if(f.count(x) == 0)
return x;
f[x] = find(f[x]);
return f[x];
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for(int i=0;i<n;i++){
int x;
cin >> x;
int fx = find(x);
cout << fx << " ";
f[fx] = fx + 1;
}
return 0;
}