Codeforces Round #436 D

Make a Permutation!

思路:记录一共有多少种数字tot,那么一定需要修改n-tot次,cnt记录当前没出现过的最小的数字,每种数字只有一个不需要修改,如果当前数字比cnt大,那么直接修改,如果当前数字比cnt小,那么修改后面的,注意每种数只能有一个是不修改的,xjb模拟了

AC代码:

#include "iostream"
#include "iomanip"
#include "string.h"
#include "stack"
#include "queue"
#include "string"
#include "vector"
#include "set"
#include "map"
#include "algorithm"
#include "stdio.h"
#include "math.h"
#pragma comment(linker, "/STACK:102400000,102400000")
#define bug(x) cout<<x<<" "<<"UUUUU"<<endl;
#define mem(a,x) memset(a,x,sizeof(a))
#define step(x) fixed<< setprecision(x)<<
#define mp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define ll long long
#define endl ("\n")
#define ft first
#define sd second
#define lrt (rt<<1)
#define rrt (rt<<1|1)
using namespace std;
const ll mod=1e9+7;
const ll INF = 1e18+1LL;
const int inf = 1e9+1e8;
const double PI=acos(-1.0);
const int N=1e5+100;

int M[N<<1],vis[N<<1];
int a[N<<1],tot;
int main(){
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    int n; cin>>n;
    for(int i=1; i<=n; ++i){
        cin>>a[i];
        if(!M[a[i]]){
            tot++;
        }
         M[a[i]]++;
    }
    int t=n-tot;
    int cnt=1;
    for(int i=1; i<=n; ++i){
        if(M[a[i]]!=1){
            while(M[cnt]>0){
                cnt++;
            }
            if(a[i]<cnt && vis[a[i]]==0){
                vis[a[i]]=1; continue;
            }
            M[a[i]]--, a[i]=cnt, M[cnt]++;
        }
    }
    cout<<t<<endl;
    for(int i=1; i<=n; ++i){
        cout<<a[i]<<" ";
    }
    return 0;
}

 

posted on 2017-09-25 21:42  lazzzy  阅读(121)  评论(0编辑  收藏  举报

导航