답안 #416368

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
416368 2021-06-02T10:38:39 Z model_code Long puzzle (innopolis2021_final_D) C++17
80 / 100
3000 ms 10880 KB
// Created by Nikolay Budin

#ifdef DEBUG
#  define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define ff first
#define ss second
#define szof(x) ((int)x.size())
#ifndef LOCAL
#  define cerr __get_ce
#endif

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef unsigned long long ull;

using namespace __gnu_pbds;
template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename K, typename V> using ordered_map = tree<K, V, less<K>, rb_tree_tag, tree_order_statistics_node_update>;

int const INF = (int)1e9 + 1e3;
ll const INFL = (ll)1e18 + 1e6;
#ifdef LOCAL
	mt19937 tw(9450189);
#else
	mt19937 tw(chrono::high_resolution_clock::now().time_since_epoch().count());
#endif
uniform_int_distribution<ll> ll_distr;
ll rnd(ll a, ll b) { return ll_distr(tw) % (b - a + 1) + a; }

const int MOD = 1'000'000'007;

void add(int& a, int b) {
	a += b;
	if (a >= MOD) {
		a -= MOD;
	}
}

ll encode(int len, int deg1, int deg2, int mask_have, vector<int> const& color) {
	ll ret = len;
	ret <<= 11;
	ret |= deg1 + 1000;
	ret <<= 11;
	ret |= deg2 + 1000;
	ret <<= 4;
	ret |= mask_have;
	for (int i = 0; i < 4; ++i) {
		ret <<= 2;
		ret |= color[i];
	}
	return ret;
}

void decode(ll val, int& len, int& deg1, int& deg2, int& mask_have, vector<int>& color) {
	for (int i = 3; i >= 0; --i) {
		color[i] = val & 3;
		val >>= 2;
	}
	mask_have = val & 15;
	val >>= 4;
	deg2 = (val & 2047) - 1000;
	val >>= 11;
	deg1 = (val & 2047) - 1000;
	val >>= 11;
	len = val;
}

void solve() {
	int n, l;
	cin >> n >> l;
	// len, deg_in[1] - deg_out[1], deg_in[2] - deg_out[2], mask_have, color_0 .. color_3

	vector<pair<ll, int>> vars, next;
	vars.push_back({encode(0, 0, 0, 0, {0, 1, 2, 3}), 1});

	int len, deg1, deg2, mask_have;
	vector<int> color(4);


	for (int it = 0; it < n; ++it) {
		int a;
		string b, c;
		cin >> a >> b >> c;
		int vl, vr;
		if (b == "none") {
			vl = 0;
		} else if (b == "in") {
			vl = 1;
		} else {
			vl = 2;
		}
		if (c == "none") {
			vr = 3;
		} else if (c == "in") {
			vr = 2;
		} else {
			vr = 1;
		}

		// cerr << szof(vars) << endl;

		next.clear();

		for (auto p : vars) {
			decode(p.ff, len, deg1, deg2, mask_have, color);
			// cerr << len << " " << deg1 << " " << deg2 << " " << mask_have << endl;
			next.push_back(p);

			if (len + a > l || (vl == 0 && (mask_have & 1)) || (vr == 3 && (mask_have & 8))) {
				continue;
			}

			len += a;

			mask_have |= 1 << vl;
			mask_have |= 1 << vr;
			if (vl == 1) {
				--deg1;
			}
			if (vl == 2) {
				--deg2;
			}
			if (vr == 1) {
				++deg1;
			}
			if (vr == 2) {
				++deg2;
			}

			if (color[vl] != color[vr]) {
				int old_color = color[vr];
				replace(color.begin(), color.end(), old_color, color[vl]);
				int color_cnt = 0;
				for (int i = 0; i < 4; ++i) {
					if (find(color.begin(), color.begin() + i, color[i]) == color.begin() + i) {
						if (color[i] != color_cnt) {
							old_color = color[i];
							replace(color.begin(), color.end(), old_color, color_cnt);
						}
						++color_cnt;
					}
				}
			}

			next.push_back({encode(len, deg1, deg2, mask_have, color), p.ss});
		}

		sort(next.begin(), next.end());
		vars.clear();
		for (auto p : next) {
			if (!szof(vars) || vars.back().ff != p.ff) {
				vars.push_back(p);
			} else {
				add(vars.back().ss, p.ss);
			}
		}
	}

	int ans = 0;
	for (auto p : vars) {
		decode(p.ff, len, deg1, deg2, mask_have, color);
		if (len == l && deg1 == 0 && deg2 == 0 && (mask_have & 1) && (mask_have & 8) 
			&& color[3] == 0 && (!(mask_have & 2) || color[1] == 0) && (!(mask_have & 4) || color[2] == 0)) {
			add(ans, p.ss);
		}
	}

	cout << ans << "\n";
}


int main() {
#ifdef LOCAL
	auto start_time = clock();
	cerr << setprecision(3) << fixed;
#endif
	cout << setprecision(15) << fixed;
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int test_count = 1;
	// cin >> test_count;
	for (int test = 1; test <= test_count; ++test) {
		solve();
	}

#ifdef LOCAL
	auto end_time = clock();
	cerr << "Execution time: " << (end_time - start_time) * (int)1e3 / CLOCKS_PER_SEC << " ms\n";
#endif
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 2 ms 460 KB Output is correct
3 Correct 4 ms 588 KB Output is correct
4 Correct 2 ms 460 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 4 ms 716 KB Output is correct
9 Correct 3 ms 528 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 400 KB Output is correct
2 Correct 27 ms 448 KB Output is correct
3 Correct 23 ms 456 KB Output is correct
4 Correct 6 ms 332 KB Output is correct
5 Correct 27 ms 448 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 3 ms 332 KB Output is correct
8 Correct 4 ms 332 KB Output is correct
9 Correct 24 ms 332 KB Output is correct
10 Correct 28 ms 428 KB Output is correct
11 Correct 24 ms 332 KB Output is correct
12 Correct 27 ms 340 KB Output is correct
13 Correct 18 ms 404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 4 ms 332 KB Output is correct
3 Correct 4 ms 460 KB Output is correct
4 Correct 3 ms 332 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 2 ms 344 KB Output is correct
8 Correct 9 ms 588 KB Output is correct
9 Correct 8 ms 588 KB Output is correct
10 Correct 9 ms 588 KB Output is correct
11 Correct 7 ms 460 KB Output is correct
12 Correct 6 ms 332 KB Output is correct
13 Correct 2 ms 332 KB Output is correct
14 Correct 16 ms 732 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 4 ms 332 KB Output is correct
3 Correct 4 ms 460 KB Output is correct
4 Correct 3 ms 332 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 2 ms 344 KB Output is correct
8 Correct 9 ms 588 KB Output is correct
9 Correct 8 ms 588 KB Output is correct
10 Correct 9 ms 588 KB Output is correct
11 Correct 7 ms 460 KB Output is correct
12 Correct 6 ms 332 KB Output is correct
13 Correct 2 ms 332 KB Output is correct
14 Correct 16 ms 732 KB Output is correct
15 Correct 14 ms 624 KB Output is correct
16 Correct 26 ms 744 KB Output is correct
17 Correct 26 ms 952 KB Output is correct
18 Correct 21 ms 716 KB Output is correct
19 Correct 3 ms 332 KB Output is correct
20 Correct 5 ms 332 KB Output is correct
21 Correct 12 ms 456 KB Output is correct
22 Correct 87 ms 1132 KB Output is correct
23 Correct 80 ms 1140 KB Output is correct
24 Correct 58 ms 1204 KB Output is correct
25 Correct 72 ms 1156 KB Output is correct
26 Correct 57 ms 816 KB Output is correct
27 Correct 49 ms 796 KB Output is correct
28 Correct 27 ms 632 KB Output is correct
29 Correct 11 ms 488 KB Output is correct
30 Correct 147 ms 1772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 2 ms 460 KB Output is correct
3 Correct 4 ms 588 KB Output is correct
4 Correct 2 ms 460 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 4 ms 716 KB Output is correct
9 Correct 3 ms 528 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 26 ms 400 KB Output is correct
14 Correct 27 ms 448 KB Output is correct
15 Correct 23 ms 456 KB Output is correct
16 Correct 6 ms 332 KB Output is correct
17 Correct 27 ms 448 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 3 ms 332 KB Output is correct
20 Correct 4 ms 332 KB Output is correct
21 Correct 24 ms 332 KB Output is correct
22 Correct 28 ms 428 KB Output is correct
23 Correct 24 ms 332 KB Output is correct
24 Correct 27 ms 340 KB Output is correct
25 Correct 18 ms 404 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 4 ms 332 KB Output is correct
28 Correct 4 ms 460 KB Output is correct
29 Correct 3 ms 332 KB Output is correct
30 Correct 1 ms 204 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 2 ms 344 KB Output is correct
33 Correct 9 ms 588 KB Output is correct
34 Correct 8 ms 588 KB Output is correct
35 Correct 9 ms 588 KB Output is correct
36 Correct 7 ms 460 KB Output is correct
37 Correct 6 ms 332 KB Output is correct
38 Correct 2 ms 332 KB Output is correct
39 Correct 16 ms 732 KB Output is correct
40 Correct 14 ms 624 KB Output is correct
41 Correct 26 ms 744 KB Output is correct
42 Correct 26 ms 952 KB Output is correct
43 Correct 21 ms 716 KB Output is correct
44 Correct 3 ms 332 KB Output is correct
45 Correct 5 ms 332 KB Output is correct
46 Correct 12 ms 456 KB Output is correct
47 Correct 87 ms 1132 KB Output is correct
48 Correct 80 ms 1140 KB Output is correct
49 Correct 58 ms 1204 KB Output is correct
50 Correct 72 ms 1156 KB Output is correct
51 Correct 57 ms 816 KB Output is correct
52 Correct 49 ms 796 KB Output is correct
53 Correct 27 ms 632 KB Output is correct
54 Correct 11 ms 488 KB Output is correct
55 Correct 147 ms 1772 KB Output is correct
56 Correct 0 ms 204 KB Output is correct
57 Correct 1 ms 204 KB Output is correct
58 Correct 344 ms 1780 KB Output is correct
59 Correct 465 ms 1828 KB Output is correct
60 Correct 716 ms 6748 KB Output is correct
61 Correct 442 ms 1748 KB Output is correct
62 Correct 46 ms 460 KB Output is correct
63 Correct 127 ms 804 KB Output is correct
64 Correct 223 ms 1176 KB Output is correct
65 Correct 2032 ms 10768 KB Output is correct
66 Correct 2063 ms 5720 KB Output is correct
67 Correct 2267 ms 5684 KB Output is correct
68 Correct 1978 ms 5804 KB Output is correct
69 Correct 1556 ms 3356 KB Output is correct
70 Correct 656 ms 1716 KB Output is correct
71 Correct 368 ms 1144 KB Output is correct
72 Correct 143 ms 884 KB Output is correct
73 Execution timed out 3045 ms 10880 KB Time limit exceeded