Submission #61229

#TimeUsernameProblemLanguageResultExecution timeMemory
61229SpaimaCarpatilorparentrises (BOI18_parentrises)C++17
50 / 100
112 ms3628 KiB
#include<bits/stdc++.h>

using namespace std;

int N;
char sir[1000009], ans[1000009];
const int mod = 1e9 + 7;

void adto (int &x, int y) {x += y; if (x >= mod) x -= mod;}

void addGreens (int l, int r)
{
    while (1)
    {
        while (sir[l] == ')' && l <= N) l ++;
        while (sir[r] == '(' && r >= 1) r --;
        if (l == N + 1 || r == 0) break;
        if (l < r) ans[l] = ans[r] = 'G', l ++, r --;
        else break;
    }
}

void fillOutRB (bool &ok)
{
    int j = 0, k = 0;
    for (int i=1; i<=N; i++)
    {
        if (ans[i] == 'G')
        {
            if (sir[i] == '(') j ++, k ++;
            else j --, k --;
            if (j < 0 || k < 0) ok = 0;
            continue;
        }
        if (sir[i] == '(')
        {
            if (j > k) k ++, ans[i] = 'B';
            else j ++, ans[i] = 'R';
            continue;
        }
        if (j < k) k --, ans[i] = 'B';
        else j --, ans[i] = 'R';
        if (j < 0) ok = 0;
        assert (j <= k);
    }
    if (!ok) assert (j + k == 0);
}

namespace generator {
    const int N = 13;
    bool canDo = 0;
    void back (int pos, int i, int j)
    {
        if (canDo) return ;
        if (i < 0 || j < 0) return ;
        if (pos == N + 1)
        {
            if (i + j == 0)
                canDo = 1;
            return ;
        }
        back (pos + 1, i + sir[pos], j + sir[pos]);
        back (pos + 1, i + sir[pos], j);
        back (pos + 1, i, j + sir[pos]);
    }

    void genTests ()
    {
        vector < string > input;
        for (int msk = 0; msk < (1 << N); msk ++)
        {
            for (int i=0; i<N; i++)
                sir[i + 1] = (((msk & (1 << i)) > 0) ? +1 : -1);
            canDo = 0, back (1, 0, 0);
            if (canDo)
            {
                string curr;
                for (int i=1; i<=N; i++)
                    if (sir[i] == +1) curr.push_back ('(');
                    else curr.push_back (')');
                input.push_back (curr);
            }
        }
        printf ("%d\n", input.size ());
        for (auto s : input)
            printf ("%s\n", s.c_str ());
        exit (0);
    }
}

int main ()
{
//freopen ("input", "r", stdin);
//freopen ("output", "w", stdout);

//generator::genTests ();

int type;
scanf ("%d", &type);
if (type == 1)
{
    int tests;
    scanf ("%d\n", &tests);
    while (tests --)
    {
        scanf ("%s\n", sir + 1), N = strlen (sir + 1);
        int balance = 0;
        for (int i=1; i<=N; i++)
            if (sir[i] == '(') balance ++;
            else balance --;
        for (int i=1; i<=N + 1; i++)
            ans[i] = 0;
        bool ok = 1;
        if (balance > 0)
        {
            ///I'll use first X + balance open brackets and last X closed ones where X is maximal so that all the closed brackets come after the open ones
            int l = 1, r = N;
            while (balance > 0)
            {
                while (sir[r] == '(' && r >= 1) r --;
                if (r == 0)
                {
                    ok = 0;
                    break;
                }
                ans[r] = 'G', r --;
                balance --;
            }
            if (ok)
                addGreens (l, r);
        }
        else
        {
            balance = -balance;
            ///same as above
            int l = 1, r = N;
            while (balance > 0)
            {
                while (sir[l] == ')' && l <= N) l ++;
                if (l == N + 1)
                {
                    ok = 0;
                    break;
                }
                ans[l] = 'G', l ++;
                balance --;
            }
            if (ok)
                addGreens (l, r);
        }
        if (ok)
            fillOutRB (ok);
        if (!ok) printf ("impossible\n");
        else
        {
            for (int i=1; i<=N; i++)
                printf ("%c", ans[i]);
            printf ("\n");
        }
    }
    return 0;
}
/*dp[0][0][0] = 1;
for (int i=0; i<=N; i++)
    for (int j=0; j<=i; j++)
        for (int k=0; k<=i; k++)
            if (dp[i][j][k] > 0)
            {
                adto (dp[i + 1][j + 1][k + 1], dp[i][j][k]);
                int nj = j, nk = k;
                if (k > j) nk --;
                else nj --;
                if (nj >= 0)
                    adto (dp[i + 1][nj][nk], dp[i][j][k]);
            }
int tests;
scanf ("%d", &tests);
while (tests --)
{
    scanf ("%d", &N);
    int ans = 0;
    for (int i=0; i<=N; i++)
        for (int j=0; j<=N; j++)
            ans += dp[i][j];
}*/
return 0;
}

Compilation message (stderr)

parentrises.cpp: In function 'void generator::genTests()':
parentrises.cpp:84:38: warning: format '%d' expects argument of type 'int', but argument 2 has type 'std::vector<std::__cxx11::basic_string<char> >::size_type {aka long unsigned int}' [-Wformat=]
         printf ("%d\n", input.size ());
                         ~~~~~~~~~~~~~^
parentrises.cpp: In function 'int main()':
parentrises.cpp:99:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 scanf ("%d", &type);
 ~~~~~~^~~~~~~~~~~~~
parentrises.cpp:103:11: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf ("%d\n", &tests);
     ~~~~~~^~~~~~~~~~~~~~~~
parentrises.cpp:106:32: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf ("%s\n", sir + 1), N = strlen (sir + 1);
         ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...