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
  • 相关阅读:
    Python --链接MYSQL数据库与简单操作 含SSH链接
    Jmeter 后置处理器--jp@gc
    Jmeter 接口测试 响应结果中文是Unicode转为中文
    Jmeter JDBC请求---把数据库结果参数化传递到其他请求
    MySQL根据某字段部分内容分组计数
    Linux 服务器命令,持续更新……
    APP网络测试要点和弱网模拟
    Jmeter JDBC Request 查询语句中有汉字查询结果为空的解决方法
    Jmeter连接Redis,获取Redis数据集
    Jmeter获取接口返回数组的长度
  • 原文地址:https://www.cnblogs.com/lyzuikeai/p/7594819.html
Copyright © 2011-2022 走看看