1283C - Friends and Gifts - CodeForces Solution


constructive algorithms data structures math *1500

Please click on ads to support us..

Python Code:


q = int(input())
w = list(map(int, input().split()))
e = [0] * q
r = []
t = []
for i in range(q):
    if w[i]==0:
                r.append(i)
    else:
                e[w[i]-1]=1
for i in range(q):
    if e[i]==0:
                t.append(i)
for i in range(len(r)):
    if r[i] == t[i]:
                if i == 0:
                        t[i], t[1] = t[1], t[i]
        else:
                        t[i], t[0] = t[0], t[i]
for i in range(len(r)):
    w[r[i]] = t[i] + 1
print(*w)
    

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define AM17 ios_base::sync_with_stdio(false),cin.tie(NULL);
#define yes cout<<"YES\n";
#define no cout<<"NO\n";

#define lenSorts(s) sort(s.begin(), s.end(), [&] (const string &s, const string &t) { return s.size() < t.size();});
int dx4[4] = { -1, 0, 1, 0 };
int dy4[4] = { 0, 1, 0, -1 };
int dx8[] = {+0, +0, -1, +1, +1, +1, -1, -1};
int dy8[] = {-1, +1, +0, +0, +1, -1, +1, -1};
string Dir = "URDL";
template <class T>
istream & operator>> (istream&is , vector<T> &v )
{
    for (auto &i:v)
        is>> i ;
    return is ;
}
template <class T>
ostream & operator<< (ostream&os ,const vector<T> &v )
{
    for (auto &i:v)
        os << i << " " ;
    os << '\n' ;
    return os ;
}
ll MOD;
ll fastpow(ll a,ll p)
{
    if(p==0)
        return 1ll;

    if(p&1)
        return ((a%MOD) * fastpow(a,p-1))%MOD;
    else
    {
        ll ret=(fastpow(a,p/2));
        return ((ret%MOD) * (ret%MOD))%MOD;
    }
}
//
////ll w[N];
const int N=2e5+10;

//int parent[N];
//struct DSU{
////    vector<ll>cost;
////    ll mx=0;
//    DSU(int n)
//    {
////        sz.resize(N);
//        for (int i = 0; i <= n ; ++i) {
//            parent[i]=i;
////            sz[i]=1;
//        }
//    }
//    int find(int node)
//    {
//        return parent[node];
////        if(parent[node]==node)
////            return parent[node];
////        return parent[node]= find(parent[node]);
//    }
//    bool add(int a,int b)
//    {
////        a= find(a);
////        b= find(b);
////        if(a==b)
////            return false;
////        if(sz[a]<sz[b])
////        { swap(a,b);}
////        parent[b]=a;
////        sz[a]+=sz[b];
//        parent[a]=parent[b];
//        return true;
//    }
//};
//
int main()
{
    int loop=1;
//    cin>>loop;
    while (loop--)
    {
        int n;
        cin>>n;
        vector<pair<int,int>>v(n+1);
        vector<int>send,rec;
        vector<int>ans(n+1);
        for (int i = 1; i <= n; ++i) {
            int f;
            cin>>f;
            ans[i]=f;
            if(f)
            {
                v[i].first=f;
                v[f].second=i;
            }
            else
            {
                send.push_back(i);
            }
        }
        for (int i = 1; i <=n ; ++i) {
            if(!v[i].second)
                rec.push_back(i);
        }
        for (int i = 0; i < send.size(); ++i) {
            if(send[i]==rec[i])
            {
                swap(rec[i],rec[(i+1)%(send.size())]);
            }
        }
        int l=0;
        for (int i = 1; i <=n ; ++i) {
            cout<<(ans[i]?ans[i]:rec[l++])<<" ";
        }



    }
}


Comments

Submit
0 Comments
More Questions

136. Single Number
169. Majority Element
119. Pascal's Triangle II
409. Longest Palindrome
1574A - Regular Bracket Sequences
1574B - Combinatorics Homework
1567A - Domino Disaster
1593A - Elections
1607A - Linear Keyboard
EQUALCOIN Equal Coins
XOREQN Xor Equation
MAKEPAL Weird Palindrome Making
HILLSEQ Hill Sequence
MAXBRIDGE Maximise the bridges
WLDRPL Wildcard Replacement
1221. Split a String in Balanced Strings
1002. Find Common Characters
1602A - Two Subsequences
1555A - PizzaForces
1607B - Odd Grasshopper
1084A - The Fair Nut and Elevator
1440B - Sum of Medians
1032A - Kitchen Utensils
1501B - Napoleon Cake
1584B - Coloring Rectangles
1562B - Scenes From a Memory
1521A - Nastia and Nearly Good Numbers
208. Implement Trie
1605B - Reverse Sort
1607C - Minimum Extraction