[Codeforces Round #611 (Div. 3)] C. Friends and Gifts (随机大法好)

[Codeforces Round #611 (Div. 3)] C. Friends and Gifts (随机大法好)

C. Friends and Gifts

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

There are nn friends who want to give gifts for the New Year to each other. Each friend should give exactly one gift and receive exactly one gift. The friend cannot give the gift to himself.

For each friend the value fifi is known: it is either fi=0fi=0 if the ii-th friend doesn't know whom he wants to give the gift to or 1≤fi≤n1≤fi≤n if the ii-th friend wants to give the gift to the friend fifi.

You want to fill in the unknown values (fi=0fi=0) in such a way that each friend gives exactly one gift and receives exactly one gift and there is no friend who gives the gift to himself. It is guaranteed that the initial information isn't contradictory.

If there are several answers, you can print any.

Input

The first line of the input contains one integer nn (2≤n≤2⋅1052≤n≤2⋅105) — the number of friends.

The second line of the input contains nn integers f1,f2,…,fnf1,f2,…,fn (0≤fi≤n0≤fi≤n, fi≠ifi≠i, all fi≠0fi≠0 are distinct), where fifi is the either fi=0fi=0 if the ii-th friend doesn't know whom he wants to give the gift to or 1≤fi≤n1≤fi≤n if the ii-th friend wants to give the gift to the friend fifi. It is also guaranteed that there is at least two values fi=0fi=0.

Output

Print nn integers nf1,nf2,…,nfnnf1,nf2,…,nfn, where nfinfi should be equal to fifi if fi≠0fi≠0 or the number of friend whom the ii-th friend wants to give the gift to. All values nfinfi should be distinct, nfinfi cannot be equal to ii. Each friend gives exactly one gift and receives exactly one gift and there is no friend who gives the gift to himself.

If there are several answers, you can print any.

Examples

input

Copy

5
5 0 0 2 4

output

Copy

5 3 1 2 4 

input

Copy

7
7 0 0 1 4 0 6

output

Copy

7 3 2 1 4 5 6 

input

Copy

7
7 4 0 3 0 5 1

output

Copy

7 4 2 3 6 5 1 

input

Copy

5
2 1 0 0 0

output

Copy

2 1 4 5 3 

题意:

给定一个整数n,以及一个1~n的全排列p,

p中至少有2个数字被抹掉了(抹掉后p[i]=0),让我们补充哪些被抹掉的部分,

使其满足:

是一个1~n的全排列,以及p[i]不等于i

思路:

本题可以抖个机灵用随机数做。

我们将被抹掉的数字加入到一个vector中,

用mt随机数 (mt19937 generator(time(0))) 作为引擎,

调用 shuffle 函数进行随机打乱vector的顺序,直至满足将其填入p中不存在p[i]=i即可

代码:

#include <bits/stdc++.h>
#include <ctime>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) { if (a == 0ll) {return 0ll;} a %= MOD; ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("
");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("
");}}}
inline long long readll() {long long tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') fh = -1; c = getchar();} while (c >= '0' && c <= '9') tmp = tmp * 10 + c - 48, c = getchar(); return tmp * fh;}
inline int readint() {int tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') fh = -1; c = getchar();} while (c >= '0' && c <= '9') tmp = tmp * 10 + c - 48, c = getchar(); return tmp * fh;}
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int n;
int a[maxn];
int b[maxn];
std::vector<int> v, v2;
int main()
{
    //freopen("D:\code\text\input.txt","r",stdin);
    //freopen("D:\code\text\output.txt","w",stdout);
    n = readint();
    srand ( (unsigned int) time (NULL) );
    repd(i, 1, n)
    {
        a[i] = readint();
        b[a[i]] = 1;
        if (a[i] == 0)
        {
            v2.push_back(i);
        }
    }
    repd(i, 1, n)
    {
        if (!b[i])
        {
            v.push_back(i);
        }
    }
    mt19937 generator(time(0));
    repd(rp, 1, 20)
    {
        shuffle(ALL(v), generator);
        int len = sz(v2);
        int isok = 1;
        for (int i = 0; i < len; ++i)
        {
            if (v[i] == v2[i])
            {
                isok = 0;
                break;
            }
        }
        if (isok)
        {
            break;
        }
    }
    int id = 0;
    repd(i, 1, n)
    {
        printf("%d%c", a[i] == 0 ? v[id++] : a[i], i == n ? '
' : ' ');
    }

    return 0;
}



本博客为本人原创,如需转载,请必须声明博客的源地址。 本人博客地址为:www.cnblogs.com/qieqiemin/ 希望所写的文章对您有帮助。
原文地址:https://www.cnblogs.com/qieqiemin/p/12253971.html