Codeforces Round #315 (Div. 2B) 569B Inventory 贪心

时间:2023-03-08 22:17:42

题目:Click here

题意:给你n,然后n个数,n个数中可能重复,可能不是1到n中的数。然后你用最少的改变数,让这个序列包含1到n所有数,并输出最后的序列。

分析:贪心。

 #include <bits/stdc++.h>
using namespace std;
const int M = 1e5+; int n;
int a[M]; // 给定序列
int mark[M]; // mark[i] 表示i在给定序列中的出现次数
int notap[M]; // 给的序列中没有出现的元素
bool firstout; void print( int x ) { // 输出格式控制 ps:在CF上测试了一下,不管这个格式也能AC
if( firstout ) {
printf("%d", x );
firstout = false;
}
else
printf(" %d", x );
} int main() {
while( ~scanf("%d", &n ) ) {
memset( mark, , sizeof(mark) );
memset( notap, , sizeof(notap) );
for( int i=; i<=n; i++ ) {
scanf("%d", a+i );
mark[a[i]]++;
}
int cnt = ;
for( int i=; i<=n; i++ )
if( !mark[i] ) {
notap[cnt] = i;
cnt++;
}
firstout = true;
int top = ;
for( int i=; i<=n; i++ )
if( mark[a[i]] == && a[i] <= n ) // 给定序列中有这个数并且范围合法
print( a[i] );
else {
print( notap[top] ); // 不然从没有的序列中弹出一个数
top++;
mark[a[i]]--;
}
printf("\n");
}
return ;
}