1408A - Circle Coloring - CodeForces Solution


constructive algorithms *800

Please click on ads to support us..

Python Code:

for _ in [0]*int(input()):
 n=int(input())
 a=[*map(int,input().split())]
 b=[*map(int,input().split())]
 c=[*map(int,input().split())]
 p=[a[0]]
 for i in range(1,n):
  if p[-1]==a[i]:p.append(b[i])
  else:p.append(a[i])
 if p[0]==p[-1]:p[-1]=a[-1]
 if p[0]==p[-1] or p[-2]==p[-1]:p[-1]=b[-1]
 if p[0]==p[-1] or p[-2]==p[-1]:p[-1]=c[-1]
 print(*p)

C++ Code:

#include <bits/stdc++.h>
using namespace std;
 
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    
    int t,n;
    cin >> t;
    while(t--) {
        cin >> n;
        int a[3][n],prev=0;
        for(int i=0; i<3; ++i) for(int j=0; j<n; ++j) cin >> a[i][j];
        for(int i=0; i<n; ++i)
            for(int j=0; j<3; ++j)
                if(a[j][i]!=prev && (i<n-1 || i==n-1 && a[j][i]!=a[0][0])) {
                    prev=a[j][i];
                    cout << prev << ' ';
                    break;
                }
        cout << '\n';
    }
}


Comments

Submit
0 Comments
More Questions

977D - Divide by three multiply by two
1654B - Prefix Removals
1654A - Maximum Cake Tastiness
1649A - Game
139A - Petr and Book
1612A - Distance
520A - Pangram
124A - The number of positions
1041A - Heist
901A - Hashing Trees
1283A - Minutes Before the New Year
1654D - Potion Brewing Class
1107B - Digital root
25A - IQ test
785A - Anton and Polyhedrons
1542B - Plus and Multiply
306A - Candies
1651C - Fault-tolerant Network
870A - Search for Pretty Integers
1174A - Ehab Fails to Be Thanos
1169A - Circle Metro
780C - Andryusha and Colored Balloons
1153A - Serval and Bus
1487C - Minimum Ties
1136A - Nastya Is Reading a Book
1353B - Two Arrays And Swaps
1490E - Accidental Victory
1335A - Candies and Two Sisters
96B - Lucky Numbers (easy)
1151B - Dima and a Bad XOR