J - J CodeForces - 569B(标记技巧)

J - J CodeForces - 569B(标记技巧)

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 a 1, a 2, …, a n (1 ≤ a i ≤ 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 

思路

这一题正解应该是,表计每个数字出现的次数,然后对序列排序,对于重复的数字我们只保留第一个

最优代码

#include<bits/stdc++.h>
#include<iostream>
using namespace std;
//给出一个数字n,接下来是n个数,
//把其中的重复的数或者大于n的数进行替换,
//使得整个数列是由1~n来组成的,可能会有
//多种答案,输出其中任意一种。
const int maxn = 1e5 + 5;
int a[maxn],book[maxn];
int res[maxn];
int main(){
	int n;
	cin >> n ;
	memset(res,-1,sizeof(res));
	memset(book,0,sizeof(book));
	for(int i = 1 ; i <= n ; i++){
		cin >> a[i];
		if(!book[a[i]] && a[i] <= n){
			res[i] = a[i];
			book[a[i]]++;
		}
	}
//	for(int i = 1 ; i <= n ; i ++)
//		cout << res[i] << endl;
	int minpos = 1;
	for(int i = 1 ; i <= n ; i ++){
		if(res[i] == -1){
			for(int j  = minpos ;j <= n ; j ++){
				if(!book[j]){
					minpos = j+1;
					res[i] = j;
					book[j] ++;
					break;
				}
			}
		}
	}
	for(int i = 1 ; i <= n ; i ++ )
		cout << res[i] << " ";
	cout << endl;
	return 0;	
}

暴力代码

#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
void fre() { freopen("A.txt","r",stdin), freopen("Ans.txt","w",stdout); }
#define ll long long 
const int mxn = 2e5;
const int INF = 0x3f3f3f3f;
char ar[mxn];
struct Iterm
{
    int a, b;       //a当前物品的编号、b为物品的位置
} it[mxn];

bool cmpa(Iterm x, Iterm y)
{
    return x.a < y.a;
}
bool cmpb(Iterm x, Iterm y)
{
    return x.b < y.b;

}
int bar[mxn];

int main()
{
    /* fre(); */
    int n;
    scanf("%d", &n);
    for(int i = 1; i <= n; i ++)
    {
        scanf("%d", &it[i].a);
        if(bar[it[i].a])
            it[i].a = INF;
        else
            bar[it[i].a] = 1;
        it[i].b = i;
    }
    sort(it + 1, it + 1 + n, cmpa);
    int last = 1;
    for(int i = 1; i <= n; i ++)
    {
        if(it[i].a == INF)
        {
            while(bar[last])
                last ++;
            it[i].a = last ++;
        }
    }
    sort(it + 1, it + 1 + n, cmpa);
    int tem = n;
    for(int i = 1, j = 1; j <= tem; i ++)
    {
        if(it[j].a != i)
        {
            it[tem].a = i;
            tem --;
        }
        else
            j ++;
    }
    sort(it + 1, it + 1 + n, cmpb);
    for(int i = 1; i <= n; i ++)
        printf("%d ", it[i].a);
    
    printf("\n");

    return 0;
}
原创文章 220 获赞 292 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qq_34261446/article/details/105652619
J