Codeforces 569 B. Inventory
Companies always have a lot of equipment, furniture and other things. All of them should be tracked. To do this, there is an inventory number assigned with each item. It is much easier to create a database by using those numbers and keep the track of everything.
During an audit, you were surprised to find out that the items are not numbered sequentially, and some items even share the same inventory number! There is an urgent need to fix it. You have chosen to make the numbers of the items sequential, starting with 1. Changing a number is quite a time-consuming process, and you would like to make maximum use of the current numbering.
You have been given information on current inventory numbers for n items in the company. Renumber items so that their inventory numbers form a permutation of numbers from 1 to n by changing the number of as few items as possible. Let us remind you that a set of n numbers forms a permutation if all the numbers are in the range from 1 to n, and no two numbers are equal.
Input
The first line contains a single integer n — the number of items (1 ≤ n ≤ 105).
The second line contains n numbers a1, a2, …, an (1 ≤ ai ≤ 105) — the initial inventory numbers of the items.
Output
Print n numbers — the final inventory numbers of the items in the order they occur in the input. If there are multiple possible answers, you may print any of them.
Examples
Input
3
1 3 2
Output
1 3 2
Input
4
2 2 3 3
Output
2 1 3 4
Input
1
2
Output
1
Note
In the first test the numeration is already a permutation, so there is no need to change anything.
In the second test there are two pairs of equal numbers, in each pair you need to replace one number.
In the third test you need to replace 2 by 1, as the numbering should start from one.
一开始读错题意没有考虑大于n的数的处理,英语不好是真难受。。。
题意就是n个数,只能是1-n,一个数字一个。。。。。。
我的ac代码比较麻烦,用了multiset。。
ac代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <set>
#include <vector>
using namespace std;
int a[100005];
int b[100005],c[100005]={0},d[100005]={0};
int main()
{
int n;
cin>>n;
set<int> se1;//储存a[i]中出现的数字
multiset<int>se2;//储存重复出现的数字和大于n的数字
for (int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
se1.insert(a[i]);
b[i]=a[i];
}
if (n==1)
{
cout<<1<<endl;
return 0;
}
sort(b+1,b+n+1);
int k=0;
for (int i=1;i<n;i++)
if (b[i]==b[i+1])
se2.insert(b[i]);
else if (b[i]>n)
se2.insert(b[i]);
if (b[n]>n)
se2.insert(b[n]);
for (int i=1;i<=n;i++)
{
if (se1.find(i)==se1.end())
c[k++]=i;//储存a[i]中没有的数字
}
k=0;
for (int i=1;i<=n;i++)
{
if (se2.find(a[i])!=se2.end())//如果找到了a[i],就把a[i]换成c[i]
se2.erase(se2.find(a[i]));//把a[i]从se2中删除
a[i]=c[k++];
}
for (int i=1;i<n;i++)
printf("%d ",a[i]);
printf("%d\n",a[n]);
return 0;
}
转下面附上大佬的代码(人家做的就是妙,充分的利用了i和a[i]的关系):
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 1e5+5;
bool f[maxn];//判断是不是符合条件
int data[maxn];
int fac[maxn];//把不符合的改为符合的除0外
int main()
{
int m;
scanf("%d",&m);
memset(f, 0, sizeof(f));
int cnt = 1;//记录不符合的个数,从1开始啊。。。
for(int i=1; i<=m; i++)
{
scanf("%d",&data[i]);
if(!f[data[i]] && data[i]<=m)
f[data[i]] = 1;
else
{
fac[cnt] = i;
cnt++;
}
}
cnt--;
for(int i=m; i>0; i--)
{
if(!f[i])
{
data[fac[cnt]] = i;
cnt--;
}
}
for(int i=1; i<m; i++)
printf("%d ",data[i]);
printf("%d\n",data[m]);
return 0;
}
---------------------
作者:ITAK
来源:CSDN
原文:https://blog.csdn.net/qingshui23/article/details/47810385
版权声明:本文为博主原创文章,转载请附上博文链接!