constructive algorithms *1300

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#define ll long long
#define str string
#define ld long double
#define run_vasya_run ios_base::sync_with_stdio(0) , cin.tie(0) ,cout.tie(0)
#define ull unsigned long long
using namespace std;
const ll mod = 1000000007;
ll _ , n ,a[300000] , c[300000] , b[300000] , ans , pos;
map<ll,ll>used;
vector<ll>v , v1;
ll binpow(ll a , ll n , ll mod)
{
    if(n == 0)
    {
        return 1ll;
    }
    if(n % 2 == 1)
    {
        return a * binpow(a , n - 1 , mod) % mod;
    }
    return (binpow(a , n / 2 , mod) * binpow(a , n / 2 , mod)) % mod;
}
int main()
{
    run_vasya_run;
    _ = 1;
    while(_ --)
    {
        cin >> n;
        for(int j = 1;j <= n;j ++)
        {
            cin >> a[j];
        }
        for(int j = 1;j <= n;j ++)
        {
            cin >> b[j];
            if(a[j] != b[j])
            {
                ans ++;
            }
            else
            {
                c[j] = a[j];
                used[a[j]] ++;
            }
        }
        if(ans == 1){
            for(int j = 1;j <= n;j ++)
            {
                if(a[j] != b[j])
                {
                    used[a[j]] ++;
                    used[b[j]] ++;
                    pos = j;
                }
            }
            for(int i = 1;i <= n;i ++)
            {
                if(used[i] == 0)
                {
                    c[pos] = i;
                }
            }
        }
        else
        {
            ans = 1;
            for(int j = 1;j <= n;++ j)
            {
                if(used[j] == 0)
                {
                    v.push_back(j);
                }
            }
            ans = 1;
            for(int i = 1;i <= n;++ i)
            {
                if(a[i] != b[i])
                {
                    v1.push_back(i);
                }
            }
            c[v1[0]] = v[0];
            c[v1[1]] = v[1];
            if((a[v1[0]] != v[0]) + (a[v1[1]] != v[1]) == 1 && (b[v1[0]] != v[0]) + (b[v1[1]] != v[1]) == 1)
            {

            }
            else
            {
                swap(c[v1[0]] , c[v1[1]]);
            }
        }
        for(int j = 1;j <= n;j ++)
        {
            cout << c[j] << ' ';
        }
    }
}


Comments

Submit
0 Comments
More Questions

1356. Sort Integers by The Number of 1 Bits
922. Sort Array By Parity II
344. Reverse String
1047. Remove All Adjacent Duplicates In String
977. Squares of a Sorted Array
852. Peak Index in a Mountain Array
461. Hamming Distance
1748. Sum of Unique Elements
897. Increasing Order Search Tree
905. Sort Array By Parity
1351. Count Negative Numbers in a Sorted Matrix
617. Merge Two Binary Trees
1450. Number of Students Doing Homework at a Given Time
700. Search in a Binary Search Tree
590. N-ary Tree Postorder Traversal
589. N-ary Tree Preorder Traversal
1299. Replace Elements with Greatest Element on Right Side
1768. Merge Strings Alternately
561. Array Partition I
1374. Generate a String With Characters That Have Odd Counts
1822. Sign of the Product of an Array
1464. Maximum Product of Two Elements in an Array
1323. Maximum 69 Number
832. Flipping an Image
1295. Find Numbers with Even Number of Digits
1704. Determine if String Halves Are Alike
1732. Find the Highest Altitude
709. To Lower Case
1688. Count of Matches in Tournament
1684. Count the Number of Consistent Strings