其他分享
首页 > 其他分享> > C - Friends and Gifts-CodeForces - 1283C-ZUT周赛

C - Friends and Gifts-CodeForces - 1283C-ZUT周赛

作者:互联网

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
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 


第一次的想法:
  在读入数据后把没有收到礼物的人的编号保存在stack中,再输出送出礼物的对象,如果对象为零
则从栈中弹出一个输出,如果对象与栈顶元素相同则弹出栈顶第二个元素输出
  具体代码:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<stack>
 4 using namespace std;
 5 typedef long long ll;
 6 ll n;
 7 ll to[200005];
 8 ll is[200005];
 9 stack<ll> s;
10 ll go(ll x){
11     if(s.top()!=x){
12         ll k=s.top();s.pop();
13         return k;
14     }else{
15         ll a=s.top();s.pop();
16         ll k=s.top();s.pop();
17         s.push(a);
18         return k;
19     }
20 }
21 int main(){
22     cin>>n;
23     for(ll i=1;i<=n;i++){
24         cin>>to[i];
25         if(to[i]==0){
26             
27         }
28         is[to[i]]=1;
29     }
30     for(ll i=1;i<=n;i++){
31         if(!is[i]){
32             s.push(i);
33         }
34     }
35     for(ll i=1;i<=n;i++){
36         if(to[i]==0){
37             cout<<go(i)<<" ";
38         }else{
39             cout<<to[i]<<" ";
40         }
41     }
42     return 0;
43 }
这是错误的code
至今无法找出WA原因5555

正解?:
 开两个vector一个存没有送出的人的下标,另一个存没有收到礼物的人的下标,然后判两个vector的相同位置的下标是否相同
如果相同则与后面的人的下标互换一下值以避免自己给自己送礼物的情况
  具体代码:
 1 #include<iostream>
 2 #include<cmath>
 3 #include<vector>
 4 #define ll long long
 5 using namespace std;
 6 ll to[200005];
 7 ll is[200005];
 8 vector<ll> a;
 9 vector<ll> b;
10 int main(){
11     ll n;
12     cin>>n;
13     for(ll i=1;i<=n;i++){
14         cin>>to[i];
15         is[to[i]]=1;
16         if(to[i]==0){
17             a.push_back(i);
18         }
19     }
20     for(ll i=1;i<=n;i++){
21         if(is[i]==0){
22             b.push_back(i);
23         }
24     }
25     ll len=a.size();
26     for(ll i=0;i<len;i++){
27         if(a[i]==b[i]){
28             ll x=i,y=(i+1)%len;
29             swap(b[x],b[y]);
30         }
31     }
32     for(int i=0;i<len;i++){
33         to[a[i]]=b[i];
34     }
35     for(ll i=1;i<=n;i++) cout<<to[i]<<" ";
36     return 0;
37     
38 }

 

 

标签:1283C,周赛,ZUT,gift,give,ll,input,fi,friend
来源: https://www.cnblogs.com/diara/p/13907476.html