1. 程式人生 > 實用技巧 >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 arennfriends who want to give gifts for the New Year to each other. Each friend should giveexactlyone gift and receiveexactlyone gift. The friendcannotgive the gift to himself.

For each friend the valuefifiis known: it is eitherfi=0fi=0if theii-th friend doesn't know whom he wants to give the gift to or

1fin1≤fi≤nif theii-th friend wants to give the gift to the friendfifi.

You want to fill in the unknown values (fi=0fi=0) in such a way that each friend givesexactlyone gift and receivesexactlyone gift and there isnofriend 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 integernn(2n21052≤n≤2⋅105) — the number of friends.

The second line of the input containsnnintegersf1,f2,,fnf1,f2,…,fn(0fin0≤fi≤n,fiifi≠i, allfi0fi≠0are distinct), wherefifiis the eitherfi=0fi=0if thei

i-th friend doesn't know whom he wants to give the gift to or1fin1≤fi≤nif theii-th friend wants to give the gift to the friendfifi. It is also guaranteed that there isat least twovaluesfi=0fi=0.

Output

Printnnintegersnf1,nf2,,nfnnf1,nf2,…,nfn, wherenfinfishould be equal tofifiiffi0fi≠0or the number of friend whom theii-th friend wants to give the gift to. All valuesnfinfishould be distinct,nfinficannot be equal toii. Each friend givesexactlyone gift and receivesexactlyone gift and there isnofriend 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 }