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 

=========================================================================

很自然的思路是把没送礼的和没收礼的放在队列里进行一一配对,并且相同的不能配对,如果仅仅是这样的话由于加入队列顺序的原因,很可能造成我们最终还是配对了相同的。所以我们可以在发现配对相同的时候,交换和上一个的答案,这样就完成了完美的匹配

# include<iostream>
# include<vector>
# include<cstring>
# include<queue>
# include<algorithm>
using namespace std;

int a[200000+10];
bool book1[200000+10],book2[200000+10];
queue<int>v1,v2;
int main ()
{

    int n;

    cin>>n;

    for(int i=1; i<=n; i++)
    {
        cin>>a[i];
        if(a[i])
        {
            book1[i]=1;
            book2[a[i]]=1;
        }


    }


    // book1是已经送  book2是已经收礼

    for(int i=1; i<=n; i++)
    {
        if(!book1[i])
        {
            v1.push(i);
        }

        if(!book2[i])
        {
            v2.push(i);
        }
    }


    int pre;

    while(!v1.empty())
    {
        int now=v1.front();

        v1.pop();

        while(!v2.empty())
        {
            int temp=v2.front();

            v2.pop();

            if(temp==now)
            {
                if(v2.size())
                {


                    int temp2=v2.front();

                    v2.pop();

                    a[now]=temp2;

                    v2.push(temp);

                    break;
                }
                else
                {
                    int temp3=a[pre];

                    a[pre]=now;

                    a[now]=temp3;


                }

                break;
            }
            a[now]=temp;

            break;


        }

        pre=now;
    }

    for(int i=1; i<=n; i++)
    {
        cout<<a[i]<<" ";
    }

    return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qinsanma and Code

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值