zoukankan      html  css  js  c++  java
  • codeforces contest 864 problemD

    Ivan has an array consisting of n elements. Each of the elements is an integer from 1 to n.

    Recently Ivan learned about permutations and their lexicographical order. Now he wants to change (replace) minimum number of elements in his array in such a way that his array becomes a permutation (i.e. each of the integers from 1 to n was encountered in his array exactly once). If there are multiple ways to do it he wants to find the lexicographically minimal permutation among them.

    Thus minimizing the number of changes has the first priority, lexicographical minimizing has the second priority.

    In order to determine which of the two permutations is lexicographically smaller, we compare their first elements. If they are equal — compare the second, and so on. If we have two permutations x and y, then x is lexicographically smaller if xi < yi, where i is the first index in which the permutations x and y differ.

    Determine the array Ivan will obtain after performing all the changes.

    Input

    The first line contains an single integer n (2 ≤ n ≤ 200 000) — the number of elements in Ivan's array.

    The second line contains a sequence of integers a1, a2, ..., an (1 ≤ ai ≤ n) — the description of Ivan's array.

    Output

    In the first line print q — the minimum number of elements that need to be changed in Ivan's array in order to make his array a permutation. In the second line, print the lexicographically minimal permutation which can be obtained from array with q changes.

    Examples
    input
    4
    3 2 2 3
    output
    2
    1 2 4 3
    input
    6
    4 5 6 3 2 1
    output
    0
    4 5 6 3 2 1
    input
    10
    6 8 4 6 7 1 6 3 4 5
    output
    3
    2 8 4 6 7 1 9 3 10 5
    Note

    In the first example Ivan needs to replace number three in position 1 with number one, and number two in position 3 with number four. Then he will get a permutation [1, 2, 4, 3] with only two changed numbers — this permutation is lexicographically minimal among all suitable.

    In the second example Ivan does not need to change anything because his array already is a permutation.

    ————————————————————————————————————

    这道题呢就是叫你用最少的修改次数使得序列变成一个n的排列 在这个前提下使得字典序最小

    我们可以记录一下每个数出现的次数 把没出现的从小到大扔进队列里面 然后扫一边原序列

    如果当前数字出现的次数大于1我们可以考虑是否修改 如果他这个值之前已经出现过并且没有修改

    那么这个位置就必须修改了 不然的话 如果他的值比当前队列的值要小 那就不修改 留着之后修改这样字典序一定最小

    打个标记就可以辣

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    const int M=250007;
    int read(){
        int ans=0,f=1,c=getchar();
        while(c<'0'||c>'9'){if(c=='-') f=-1; c=getchar();}
        while(c>='0'&&c<='9'){ans=ans*10+(c-'0'); c=getchar();}
        return ans*f;
    }
    int n,k,ansh;
    int f[M],q[M],cnt;
    int sum,ans[M],last[M];
    struct pos{int v,x;}e[M];
    bool cmp(pos a,pos b){return a.x<b.x;}
    int main(){
        n=read();
        for(int i=1;i<=n;i++){k=read(); f[k]++; ans[i]=k;}
        for(int i=1;i<=n;i++) if(!f[i]) q[++cnt]=i;
        int k=1;
        for(int i=1;i<=n;i++){
            if(f[ans[i]]>1){
                if(last[ans[i]]||q[k]<ans[i]) f[ans[i]]--,ans[i]=q[k++],ansh++;
                else last[ans[i]]=1;
            }
        }
        printf("%d
    ",ansh);
        for(int i=1;i<=n;i++) printf("%d ",ans[i]);
        return 0;
    }
    View Code
  • 相关阅读:
    五十:数据库之Flask-Script详解
    四十九:数据库之Flask-SQLAlchemy下alembic的配置
    四十八:数据库之alembic常用命令和经典错误的解决办法
    四十七:数据库之alembic数据库迁移工具的基本使用
    四十六:数据库之Flask-SQLAlchemy的使用
    四十五:数据库之SQLAlchemy之subquery实现复杂查询
    四十四:数据库之SQLAlchemy之join实现复杂查询
    四十三:数据库之SQLAlchemy之group_by和having子句
    四十二:数据库之SQLAlchemy之数据查询懒加载技术
    四十一:数据库之SQLAlchemy之limlt、、slice、offset及切片
  • 原文地址:https://www.cnblogs.com/lyzuikeai/p/7594819.html
Copyright © 2011-2022 走看看