题意:给一个n,然后给出n个范围在1到n之间的数,问最少改变几个数字才能得到一个完整的从1到n的序列,然后输出这个序列并保证这个序列的字典序最小。
分析:首先n小于2e5,那么直接暴力就可以了……
AC代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+7;
int a[maxn],pre[maxn],mis[maxn],vis[maxn];
int main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int n;
int ans=0;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
pre[a[i]]++;
}
int j=1;
for(int i=1;i<=n;i++){
if(!pre[i]){
mis[j]=i;
j++;
}
}
cout<<j-1<<endl;
int k=1;
for(int i=1;i<=n;i++){
if(pre[a[i]]==1 && !vis[a[i]]){
cout<<a[i]<<" ";
vis[a[i]]++;
pre[a[i]]--;
}
if(pre[a[i]]>1){
if(mis[k]<a[i] && mis[k]){
cout<<mis[k]<<" ";
k++;
vis[mis[k]]++;
pre[a[i]]--;
}
else if(!vis[a[i]]){
cout<<a[i]<<" ";
vis[a[i]]++;
}
else{
cout<<mis[k]<<" ";
vis[mis[k]]++;
k++;
pre[a[i]]--;
}
}
}
return 0;
}