1504C - Balance the Bits - CodeForces Solution


brute force constructive algorithms greedy *1600

Please click on ads to support us..

Python Code:

t=int(input())
for i in range(t):
    n=int(input())
    s=input()
    a=''
    b=''
    c1=s.count("1")
    c0=s.count("0")
    if c1%2!=0 or c0%2!=0 or s[0]!="1" or s[-1]!="1" :
        print("NO")
    else:
        print("YES")
        one=0
        zer=0
        for j in range(n):
            if s[j]=="1":
                one+=1
                if one<=c1//2:
                    a+="("
                    b+="("
                else:
                    a+=")"
                    b+=")"
            else:
                zer += 1
                if zer%2!=0:
                    a+="("
                    b+=")"
                else:
                    a+=")"
                    b+="("
        print(a)
        print(b)

C++ Code:

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<vector<string>> vvs;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vpii;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repd(i, a, b) for (int i = a; i >= b; i--)
#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define yes cout << "YES \n";
#define no cout << "NO \n";
#define enter cout << "\n";
int mod = 1e9 + 7;
void solve()
{

    int n;
    cin >> n;

    string s;
    cin >> s;

    int a = 0, b = 0;
    int e = 0, f = 0;
    int r = n / 2;

    string c(n, '('), d(n, '(');
    int alt = 0;
    rep(i, 0, n)
    {
        if (s[i] == '0')
        {

            if (alt)
            {
                a++;
                d[i] = ')';
                alt = 0;
            }
            else
            {
                c[i] = ')';
                b++;

                alt = 1;
            }
        }
    }
    rep(i, 0, n)
    {
        if (s[i] == '1')
        {
            if (a < r)
            {
                a++;
            }
            else
            {
                c[i] = ')';
            }
            if (b < r)
            {
                b++;
            }
            else
            {
                d[i] = ')';
            }
            if(c[i]!=d[i]){
                no
                return;
            }
        }
    }
    a = 0;
    for (auto i : c)
    {
        if (i == '(')
        {
            a++;
        }
        else
            a--;
        if (a < 0)
        {
            no return;
        }
    }
    if (a != 0)
    {
        no return;
    }
    for (auto i : d)
    {
        if (i == '(')
        {
            a++;
        }
        else
            a--;
        if (a < 0)
        {
            no return;
        }
    }
    if (a != 0)
    {
        no return;
    }
    yes
            cout
        << c;
    enter
            cout
        << d;
    enter
}
void inc()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif // ONLINE_JUDGE
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    inc();
    int t = 1;
    cin >> t;
    // creatSieveforPM();

    while (t--)
    {

        solve();
    }

    return 0;
}


Comments

Submit
0 Comments
More Questions

510A - Fox And Snake
1520B - Ordinary Numbers
1624A - Plus One on the Subset
350A - TL
1487A - Arena
1520D - Same Differences
376A - Lever
1305A - Kuroni and the Gifts
1609A - Divide and Multiply
149B - Martian Clock
205A - Little Elephant and Rozdil
1609B - William the Vigilant
978B - File Name
1426B - Symmetric Matrix
732B - Cormen --- The Best Friend Of a Man
1369A - FashionabLee
1474B - Different Divisors
1632B - Roof Construction
388A - Fox and Box Accumulation
451A - Game With Sticks
768A - Oath of the Night's Watch
156C - Cipher
545D - Queue
459B - Pashmak and Flowers
1538A - Stone Game
1454C - Sequence Transformation
165B - Burning Midnight Oil
17A - Noldbach problem
1350A - Orac and Factors
1373A - Donut Shops