Codeforces 569 B. Inventory

22237 단어 CF
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 코드는 비교적 번거로워서 멀티셋을 사용했다.ac 코드:
#include 
#include 
#include 
#include 
#include 
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
"
,a[n]); return 0; }

다음에 큰 놈의 코드를 첨부한다(남이 하는 것이 묘하고 i와 a[i]의 관계를 충분히 이용했다).
#include 
#include 
#include 
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
"
,data[m]); return 0; } --------------------- :ITAK :CSDN :https://blog.csdn.net/qingshui23/article/details/47810385 : , !

좋은 웹페이지 즐겨찾기