Submission #68502

# Submission time Handle Problem Language Result Execution time Memory
68502 2018-08-17T08:29:44 Z aome parentrises (BOI18_parentrises) C++17
100 / 100
214 ms 116708 KB
#include <bits/stdc++.h>

using namespace std;

namespace Task1 {
	char color[1000005];

	void solve() {
		int q; cin >> q;
		while (q--) {
			string s; cin >> s;
			stack<int> st;
			stack<int> st2;
			stack<int> st3;
			bool fail = 0;
			for (int i = 0; i < s.size(); ++i) {
				if (s[i] == '(') st.push(i), color[i] = 'G';
				else {
					if (st.size()) {
						st2.push(i), color[i] = 'G', st3.push(st.top()), st.pop();
					}
					else {
						if (!st2.size()) { fail = 1; break; }
						color[i] = 'R', color[st2.top()] = 'B', st2.pop(), st3.pop();
					}
				}
			}
			while (st.size() && st3.size()) {
				color[st.top()] = 'R', color[st3.top()] = 'B';
				st.pop(), st3.pop();
			}
			while (st.size()) st.pop();
			while (st2.size()) st2.pop();
			for (int i = 0; i < s.size(); ++i) {
				if (s[i] == '(') {
					if (color[i] == 'G' || color[i] == 'R') st.push(i);
					if (color[i] == 'G' || color[i] == 'B') st2.push(i);
				}
				else {
					if (color[i] == 'G' || color[i] == 'R') {
						if (!st.size()) { fail = 1; break; }
						st.pop();
					}
					if (color[i] == 'G' || color[i] == 'B') {
						if (!st2.size()) { fail = 1; break; }
						st2.pop();
					}
				}
			}
			if (st.size() || st2.size()) fail = 1;
			if (fail) cout << "impossible\n";
			else {
				for (int i = 0; i < s.size(); ++i) cout << color[i]; cout << '\n';
			}
		}
	}
}

namespace Task2 {
	const int N = 305;
	const int mod = 1e9 + 7;

	int f[N][N][N][2], g[N][2], h[N];

	void add(int &x, int y) { x += x + y >= mod ? y - mod : y; }

	void solve() {
		f[0][0][0][0] = 1;
		for (int i = 0; i <= 300; ++i) {
			for (int j = 0; j <= i; ++j) {
				for (int k = 0; k <= i; ++k) {
					for (int l = 0; l <= 1; ++l) {
						add(f[i + 1][j + 1][k][l], f[i][j][k][l]);
						if (j) add(f[i + 1][j - 1][k + 1][1], f[i][j][k][l]);
						else if (k) add(f[i + 1][j][k - 1][0], f[i][j][k][l]);
					}
				}
			}
			for (int j = 0; j <= i; ++j) {
				add(g[i][0], f[i][0][j][0]);
				add(g[i][1], f[i][0][j][1]);
			}
		}
		for (int i = 1; i <= 300; ++i) {
			for (int j = 0; j <= i; ++j) {
				add(h[i], 1LL * (g[j][0] + g[j][1]) * g[i - j][0] % mod);
			}
		}
		int q; cin >> q;
		while (q--) {
			int x; cin >> x, cout << h[x] << '\n';
		}
	}
}

int main() {
	ios::sync_with_stdio(false);
	int P; cin >> P;
	if (P == 1) Task1::solve();
	else Task2::solve();
}

Compilation message

parentrises.cpp: In function 'void Task1::solve()':
parentrises.cpp:16:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for (int i = 0; i < s.size(); ++i) {
                    ~~^~~~~~~~~~
parentrises.cpp:34:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for (int i = 0; i < s.size(); ++i) {
                    ~~^~~~~~~~~~
parentrises.cpp:53:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < s.size(); ++i) cout << color[i]; cout << '\n';
                     ~~^~~~~~~~~~
parentrises.cpp:53:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
     for (int i = 0; i < s.size(); ++i) cout << color[i]; cout << '\n';
     ^~~
parentrises.cpp:53:58: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
     for (int i = 0; i < s.size(); ++i) cout << color[i]; cout << '\n';
                                                          ^~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 3 ms 548 KB Output is correct
4 Correct 2 ms 548 KB Output is correct
5 Correct 3 ms 548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 548 KB Output is correct
2 Correct 2 ms 564 KB Output is correct
3 Correct 2 ms 640 KB Output is correct
4 Correct 3 ms 644 KB Output is correct
5 Correct 3 ms 648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 548 KB Output is correct
2 Correct 2 ms 564 KB Output is correct
3 Correct 2 ms 640 KB Output is correct
4 Correct 3 ms 644 KB Output is correct
5 Correct 3 ms 648 KB Output is correct
6 Correct 3 ms 648 KB Output is correct
7 Correct 3 ms 648 KB Output is correct
8 Correct 2 ms 652 KB Output is correct
9 Correct 3 ms 652 KB Output is correct
10 Correct 3 ms 652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 548 KB Output is correct
2 Correct 2 ms 564 KB Output is correct
3 Correct 2 ms 640 KB Output is correct
4 Correct 3 ms 644 KB Output is correct
5 Correct 3 ms 648 KB Output is correct
6 Correct 3 ms 648 KB Output is correct
7 Correct 3 ms 648 KB Output is correct
8 Correct 2 ms 652 KB Output is correct
9 Correct 3 ms 652 KB Output is correct
10 Correct 3 ms 652 KB Output is correct
11 Correct 5 ms 656 KB Output is correct
12 Correct 3 ms 668 KB Output is correct
13 Correct 3 ms 824 KB Output is correct
14 Correct 3 ms 824 KB Output is correct
15 Correct 4 ms 824 KB Output is correct
16 Correct 24 ms 1004 KB Output is correct
17 Correct 8 ms 1640 KB Output is correct
18 Correct 6 ms 1640 KB Output is correct
19 Correct 8 ms 1640 KB Output is correct
20 Correct 9 ms 1956 KB Output is correct
21 Correct 153 ms 3032 KB Output is correct
22 Correct 99 ms 10280 KB Output is correct
23 Correct 66 ms 10280 KB Output is correct
24 Correct 79 ms 10280 KB Output is correct
25 Correct 91 ms 13292 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 193 ms 116564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 193 ms 116564 KB Output is correct
2 Correct 214 ms 116708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 193 ms 116564 KB Output is correct
2 Correct 214 ms 116708 KB Output is correct
3 Correct 214 ms 116708 KB Output is correct