Submission #365514

# Submission time Handle Problem Language Result Execution time Memory
365514 2021-02-11T19:20:41 Z kostia244 parentrises (BOI18_parentrises) C++17
100 / 100
154 ms 65344 KB
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2")
#include<bits/stdc++.h>
#define all(x) begin(x), end(x)
using namespace std;
using ll = long long;
vector<vector<int>> g;
string solve(string s) {
	g.assign(s.size(), {});
	vector<int> z[2];
	auto check = [&](string s) {
		int ok = 1, a = 0, b = 0, c = count(all(s), '('), d = count(all(s), ')');
		for(auto i : s) {
			ok &= b <= 2*a;
			ok &= c <= 2*d;
			if(i == ')') swap(a, b), swap(c, d);
			a++, c--;
			if(i == ')') swap(a, b), swap(c, d);
			ok &= b <= 2*a;
			ok &= c <= 2*d;
		}
		return ok;
	};
	if(!check(s)) return "impossible";
	for(auto &i : s) {
		int pos = &i-&s[0];
		if(i == '(') {
			z[0].push_back(pos);
		} else {
			if(z[0].empty()) {
				if(z[1].empty()) return "wimpossible";
				g[pos].push_back(z[1].back());
				g[z[1].back()].push_back(pos);
				z[1].pop_back();
			} else {
				g[pos].push_back(z[0].back());
				g[z[0].back()].push_back(pos);
				z[1].push_back(z[0].back());
				z[0].pop_back();
			}
		}/*
		cout << pos << ": ";
		for(auto i : z[0]) cout << i << " "; cout << " | ";
		for(auto i : z[1]) cout << i << " "; cout << endl;*/
	}
	reverse(all(s));
	z[0].clear();
	for(auto &i : s) {
		int pos = &i-&s[0];
		pos = s.size()-1-pos;
		if(i == ')') {
			if(g[pos].size() < 2) z[0].push_back(pos);
		} else if(g[pos].empty()) {
			if(z[0].empty()) return "wimpossible";
			g[pos].push_back(z[0].back());
			g[z[0].back()].push_back(pos);
			z[0].pop_back();
		}
	}
	reverse(all(s));
	vector<int> col(s.size());
	for(int i = 0; i < s.size(); i++) {
		if(g[i].size() == 2) {
			int c = 1;
			for(auto v : g[i]) col[v] = c++;
		}
	}
	s = "";
	for(auto i : col) {
		if(i == 0) s += "G";
		if(i == 1) s += "B";
		if(i == 2) s += "R";
	}
	return s;
}
int F[301];
int dp[302][301];
const int mod = 1e9 + 7;
void add(int &a, int b) {
	a = a+b>=mod?a+b-mod:a+b;
}
int f(int n) {
	if(F[n] != -1) return F[n];
	F[n] = 0;
	for(int c0 = 0; c0 <= n; c0++) {
		int c1 = n-c0;
		memset(dp, 0, sizeof dp);
		dp[0][0] = 1;
		for(int i = 0; i <= n; i++) {
			for(int z = 0; z <= i; z++) {
				int o = i-z;
				dp[i][z] *= (z <= 2*o && (c1-o) <= 2*(c0-z));
				//if(dp[i][z]) cout << i << " " << z << " " << dp[i][z] << " " << F[n] << endl;
				add(dp[i+1][z], dp[i][z]);
				add(dp[i+1][z+1], dp[i][z]);
				if(i == n && z == c0) {
					add(F[n], dp[i][z]);
					//cout << i << " "<< z << " | " << c0 << " " << dp[i][z] << endl;
				}
			}
		}
	}
	return F[n];
}
void calc() {
	for(int i = 0; i < 301; i++) F[i] = -1;
}
vector<int> magic = {837843372, 418921686, 953901352, 896789267, 948394637, 897003764, 612177559, 806088783, 934621064, 350224822, 175112411, 62075416, 328419278, 164209639, 891751001, 947791358, 473895679, 95673275, 769893770, 384946885, 290375226, 843739278, 421869639, 629844702, 488506332, 244253166, 301383510, 176300243, 588150125, 111861393, 722583423, 861291715, 526477156, 415292185, 707646096, 912447619, 784770329, 892385168, 553096598, 945192936, 472596468, 733717711, 405272028, 202636014, 286602731, 81313192, 40656596, 788176043, 880119188, 440059594, 103642988, 466033432, 233016716, 434823692, 347612879, 673806443, 624000951, 938705286, 469352643, 550581443, 546342054, 273171027, 476000768, 892175009, 946087508, 864267253, 949949322, 474974661, 763528731, 605633083, 802816545, 267785589, 928992770, 464496385, 991939394, 878832258, 439416129, 849451750, 965257035, 982628521, 360033502, 113787442, 56893721, 859847032, 921239537, 960619772, 858231094, 670140532, 335070266, 102336119, 609264082, 304632041, 304323137, 905618323, 952809165, 95772722, 489482444, 244741222, 369499245, 152029355, 576014681, 482869402, 431829094, 215914547, 231777149, 644744581, 822372294, 31838831, 507189630, 253594815, 702061186, 774010790, 387005395, 635491203, 535977949, 767988978, 474482700, 719738429, 859869218, 557503950, 15970429, 507985218, 596258099, 881995017, 940997512, 322809267, 983846694, 491923347, 54775313, 760978462, 380489231, 833650616, 645533691, 822766849, 151215664, 130660778, 65330389, 93415445, 781784246, 390892123, 338047842, 335267721, 667633864, 524137426, 31661245, 515830626, 406717000, 596375546, 298187773, 626109777, 265666673, 632833340, 697370162, 604797590, 302398795, 641256163, 779072480, 389536240, 52648990, 263250105, 631625056, 714603167, 465313672, 232656836, 124734442, 56680793, 528340400, 250532053, 754795604, 377397802, 408729101, 555421034, 277710517, 922600280, 403016113, 701508060, 203965075, 475613767, 737806887, 433079969, 755693087, 877846547, 39283492, 527872108, 263936054, 666019185, 575131420, 287565710, 520941027, 933113189, 966556598, 262223145, 28892134, 14446067, 765671696, 622380158, 311190079, 825442333, 641951166, 320975583, 959772055, 176372730, 88186365, 982308804, 698141717, 849070862, 973819030, 368248343, 684124175, 43060831, 593377428, 296688714, 648783808, 665001374, 332500687, 169754380, 211897966, 105948983, 764861307, 298703075, 649351541, 166780006, 874863062, 437431531, 342317187, 612904993, 806452500, 578770998, 155450740, 77725370, 177655189, 150798450, 75399225, 697441626, 164420328, 82210164, 391328273, 142385694, 71192847, 448451120, 333407575, 666703791, 96196738, 970274345, 985137176, 421349166, 841698915, 920849461, 360752402, 210918582, 105459291, 578936427, 118222693, 559111350, 287719520, 268615347, 634307677, 204521844, 925027435, 962513721, 551498904, 576580024, 288290012, 72895660, 86514938, 43257469, 509707998, 263268639, 631634323, 313801154, 158331292, 79165646, 39237478, 19865414, 9932707, 4906266, 2496396, 1248198, 613440, 314472, 157236, 76680, 39768, 19884, 9578, 5062, 2531, 1194, 652, 326, 148, 86, 43, 18, 12, 6, 2, 2, 1, 0};

int main() {
	cin.tie(0)->sync_with_stdio(0);
	magic.push_back(0);
	reverse(all(magic));
	int t, x;
	string s;
	cin >> t;
	if(t == 1) {
		cin >> t;
		while(t--) {
			cin >> s;
			cout << solve(s) << '\n';
		}
	} else {
		cin >> t;
		while(t--) cin >> x, cout << magic[x] << endl;
	}
}

Compilation message

parentrises.cpp: In function 'std::string solve(std::string)':
parentrises.cpp:62:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   62 |  for(int i = 0; i < s.size(); i++) {
      |                 ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 0 ms 364 KB Output is correct
5 Correct 0 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 0 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 0 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 0 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 2 ms 748 KB Output is correct
12 Correct 2 ms 1004 KB Output is correct
13 Correct 1 ms 492 KB Output is correct
14 Correct 2 ms 748 KB Output is correct
15 Correct 2 ms 1004 KB Output is correct
16 Correct 7 ms 492 KB Output is correct
17 Correct 14 ms 6892 KB Output is correct
18 Correct 7 ms 1132 KB Output is correct
19 Correct 11 ms 3564 KB Output is correct
20 Correct 15 ms 6900 KB Output is correct
21 Correct 59 ms 1132 KB Output is correct
22 Correct 144 ms 65084 KB Output is correct
23 Correct 81 ms 7656 KB Output is correct
24 Correct 109 ms 32744 KB Output is correct
25 Correct 154 ms 65344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct