Codeforces 1283 C. Friends and Gifts

There are n 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 fi is known: it is either fi=0 if the i-th friend doesn’t know whom he wants to give the gift to or 1≤fi≤n if the i-th friend wants to give the gift to the friend fi.

You want to fill in the unknown values (fi=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 n (2≤n≤2⋅105) — the number of friends.

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

Output
Print n integers nf1,nf2,…,nfn, where nfi should be equal to fi if fi≠0 or the number of friend whom the i-th friend wants to give the gift to. All values nfi should be distinct, nfi cannot be equal to i. 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
5
5 0 0 2 4
output
5 3 1 2 4
input
7
7 0 0 1 4 0 6
output
7 3 2 1 4 5 6
input
7
7 4 0 3 0 5 1
output
7 4 2 3 6 5 1
input
5
2 1 0 0 0
output
2 1 4 5 3

题意
给你一个序列,将没有出现的数字填入0的位子,注意第i位不能多于i。

思路
将没有出现的数字储存在一个数组,进行随机化,直到符合要求。

代码

#include <bits/stdc++.h>
typedef long long ll;
const ll mod = 9999999967;
using namespace std;
namespace fastIO {
    inline void input(int& res) {
        char c = getchar();res = 0;int f = 1;
        while (!isdigit(c)) { f ^= c == '-'; c = getchar(); }
        while (isdigit(c)) { res = (res << 3) + (res << 1) + (c ^ 48);c = getchar(); }
        res = f ? res : -res;
    }
    inline ll qpow(ll a, ll b) {
        ll ans = 1, base = a;
        while (b) {
            if (b & 1) ans = (ans * base % mod +mod )%mod;
            base = (base * base % mod + mod)%mod;
            b >>= 1;
        }
        return ans;
    }
}
using namespace fastIO;
const int N = 2e5 + 5;
int Case,n,k;
int a[N],b[N],ans[N],c[N];
bool check(){
	for(int i=1;i<=n;i++){
		//cout<<ans[i]<<endl;
		if(ans[i]==i||ans[i]==0)
			return false;
	}
	return true;
}
int main(){
	Case=1;
	//input(Case);
	int cnt = 0;
	while(Case--){
		input(n);
		for(int i=1;i<=n;i++){
			input(a[i]);
			if(a[i]) c[a[i]]=1;
		}
		for(int i=1;i<=n;i++){
			if(!c[i]) b[++cnt] = i;
		}
		while(!check()){
			random_shuffle(b+1,b+cnt+1);
			int cc = 0;
			for(int i=1;i<=n;i++){
				if(a[i])
					ans[i]=a[i];
				else
					ans[i]=b[++cc];
			}
		}
		for(int i=1;i<=n;i++) printf("%d ",ans[i]);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值