1766C - Hamiltonian Wall - CodeForces Solution


dp implementation *1300

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

template <class T>
using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#ifdef ONLINE_JUDGE
#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline","O3")
#endif // ONLINE_JUDGE

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define uni(x) (x).erase(unique(all(x)), (x).end())
#define rnk(x, y) upper_bound(all((x)), (y)) - (x).begin()

template <class T = int>
using ii = pair<T, T>;
template <class T = int>
using tri = tuple<T, T, T>;

typedef long double ld;
typedef long long ll;
// typedef __int128 LL;

mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

static int rnd(int lo, int hi)
{
    return uniform_int_distribution<int>(lo, hi)(rng);
}

int Get(int k, vector<int> &p)
{
    return p[k] == k ? k : p[k] = Get(p[k], p);
}

typedef vector<vector<int>> mat;

// global variables

const ld eps = 1e-9;
const ll oo = 2e18 + 100;

const int MAX = 2e5+10;
const ll mod = 1e9+7;

int iterate(vector<string> &s, int posx, int posy)
{
    vector<vector<bool>> band(2, vector<bool> (s[0].size()));
    band[posy][posx] = true;
    int cont = 1;
    while(true)
    {
        if(s[(posy+1)%2][posx] == 'B' && !band[(posy+1)%2][posx]){
            posy = (posy+1)%2;
            band[posy][posx] = true;
            cont++;
            continue;   
        }
        if(posx<s[0].size()&& !band[posy][posx+1] && s[posy][posx+1] == 'B'){
            posx = posx+1;
            band[posy][posx] = true;
            cont++;
            continue;
        }
        return cont;
    }
}

void solve()
{
    int n; cin >> n;
    vector<string> s(2);
    int cont = 0;
    for(int i=0;i<2;i++){
        cin >> s[i];
        for(int j=0;j<n;j++){
            if(s[i][j] == 'B'){
                cont++;
            }
        }
    }
    int ans = 0;
    for(int i=0;i<n;i++){
        if(s[0][i] == 'B'){
            ans = max(ans, iterate(s, i, 0));
        }
        if(s[1][i] == 'B'){
            ans = max(ans, iterate(s, i, 1));
        }
        if(s[0][i] == 'B' || s[1][i] == 'B'){
            break;
        }
    }
    if(ans == cont) {
        cout << "YES\n";
        return;
    }
    cout << "NO\n";
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);

#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
#endif // ONLINE_JUDGE

    int t = 1; cin >> t;
    for (int i = 1; i <= t; i++){
        solve();
    }

    return 0;
}


Comments

Submit
0 Comments
More Questions

1671D - Insert a Progression
1671A - String Building
1671B - Consecutive Points Segment
1671C - Dolce Vita
1669G - Fall Down
4D - Mysterious Present
1316B - String Modification
1204A - BowWow and the Timetable
508B - Anton and currency you all know
1672A - Log Chopping
300A - Array
48D - Permutations
677C - Vanya and Label
1583B - Omkar and Heavenly Tree
1703C - Cypher
1511C - Yet Another Card Deck
1698A - XOR Mixup
1702E - Split Into Two Sets
1703B - ICPC Balloons
1702F - Equate Multisets
1700A - Optimal Path
665C - Simple Strings
1708A - Difference Operations
1703E - Mirror Grid
1042A - Benches
1676B - Equal Candies
1705B - Mark the Dust Sweeper
1711A - Perfect Permutation
1701B - Permutation
1692A - Marathon