Submission #1029834

# Submission time Handle Problem Language Result Execution time Memory
1029834 2024-07-21T11:46:33 Z mat_jur Tug of War (BOI15_tug) C++17
100 / 100
73 ms 10620 KB
#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
auto&operator<<(auto &o, pair<auto, auto> p) {o << "(" << p.first << ", " << p.second << ")"; return o;}
auto operator<<(auto &o, auto x)->decltype(x.end(), o) {o<<"{"; for(auto e : x) o<<e<<", "; return o<<"}";}
#define debug(X) cerr << "["#X"]: " << X << '\n';
#else 
#define cerr if(0)cout
#define debug(X) ;
#endif
using ll = long long;
#define all(v) (v).begin(), (v).end()
#define ssize(x) int(x.size())
#define fi first
#define se second
#define mp make_pair
#define eb emplace_back

int main() {
	ios_base::sync_with_stdio(false); cin.tie(nullptr);

	int n, k;
	cin >> n >> k;

	vector<vector<pair<int, int>>> g(2*n);
	vector<int> s(2*n);

	int S = 0;
	for (int i = 0; i < 2*n; ++i) {
		int l, r;
		cin >> l >> r >> s[i];
		S += s[i];
		--l;
		r += n-1;
		g[l].eb(mp(r, i));
		g[r].eb(mp(l, i));
	}

	vector<int> last(2*n), st(2*n);
	for (int i = 0; i < 2*n; ++i) last[i] = ssize(g[i])-1;
	for (int i = 0; i < 2*n; ++i) st[i] = ssize(g[i]);

	vector<bool> off(2*n);

	int sum = 0;
	for (int i = 0; i < 2*n; ++i) {
		int j = i;
		while (st[j] == 1) {
			while (off[g[j][last[j]].se]) --last[j];
			auto [v, idx] = g[j][last[j]];
			off[idx] = true;
			sum += s[idx] * (j < n ? 1 : -1);
			--st[j]; --st[v];
			j = v;
		}
	}

	for (int i = 0; i < 2*n; ++i) {
		sort(all(g[i]), [&](pair<int, int> a, pair<int, int> b) {
			return off[a.se] < off[b.se];
		});
		auto l = g[i].begin();
		while (l != g[i].end() && !off[(*l).se]) l++;
		g[i].erase(l, g[i].end());
	}
	
	for (int i = 0; i < 2*n; ++i) {
		if (ssize(g[i]) != 2 && ssize(g[i]) != 0) {cout << "NO \n"; return 0;}
	}

	vector<bool> odw(2*n);
	vector<int> cnt(40 * n + 2);

	for (int i = 0; i < 2*n; ++i) {
		if (ssize(g[i]) == 0) continue;
		int j = i;
		int prev = -1;
		int x = 0;
		while (!odw[j]) {
			odw[j] = true;
			pair<int, int> nxt = (prev == g[j][0].fi ? g[j][1] : g[j][0]);
			x += s[nxt.se] * (j < n ? 1 : -1);	
			prev = j;
			j = nxt.fi;
		}
		if (prev != -1) {
			x = abs(x);
			cnt[2*x]++;
			sum -= x;
		}
	}

#ifndef DEBUG
	constexpr int SIZE = 1200005;
#else
	constexpr int SIZE = 20;
#endif

	debug(sum);

	bitset<SIZE> b;
	debug(cnt);

	b[0] = true;
	for (int i = 1; i < ssize(cnt); ++i) {
		while (cnt[i] >= 3 && 2*i < ssize(cnt)) {
			cnt[2*i]++;
			cnt[i] -= 2;
		}

		for (int j = 1; j <= cnt[i]; ++j) b |= (b<<i);
	}

	debug(b);
	for (int x = 0; x < SIZE; ++x) {
		if (b[x] && abs(sum+x) <= k) {
			cout << "YES \n";
			return 0;
		}
	}

	cout << "NO \n";

	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 860 KB Output is correct
2 Correct 1 ms 860 KB Output is correct
3 Correct 1 ms 856 KB Output is correct
4 Correct 1 ms 860 KB Output is correct
5 Correct 2 ms 860 KB Output is correct
6 Correct 2 ms 860 KB Output is correct
7 Correct 1 ms 856 KB Output is correct
8 Correct 1 ms 860 KB Output is correct
9 Correct 1 ms 860 KB Output is correct
10 Correct 3 ms 860 KB Output is correct
11 Correct 1 ms 856 KB Output is correct
12 Correct 1 ms 860 KB Output is correct
13 Correct 1 ms 860 KB Output is correct
14 Correct 1 ms 860 KB Output is correct
15 Correct 1 ms 860 KB Output is correct
16 Correct 1 ms 860 KB Output is correct
17 Correct 1 ms 860 KB Output is correct
18 Correct 1 ms 860 KB Output is correct
19 Correct 1 ms 860 KB Output is correct
20 Correct 1 ms 856 KB Output is correct
21 Correct 1 ms 860 KB Output is correct
22 Correct 1 ms 860 KB Output is correct
23 Correct 2 ms 860 KB Output is correct
24 Correct 1 ms 860 KB Output is correct
25 Correct 2 ms 860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 860 KB Output is correct
2 Correct 1 ms 860 KB Output is correct
3 Correct 1 ms 856 KB Output is correct
4 Correct 1 ms 860 KB Output is correct
5 Correct 2 ms 860 KB Output is correct
6 Correct 2 ms 860 KB Output is correct
7 Correct 1 ms 856 KB Output is correct
8 Correct 1 ms 860 KB Output is correct
9 Correct 1 ms 860 KB Output is correct
10 Correct 3 ms 860 KB Output is correct
11 Correct 1 ms 856 KB Output is correct
12 Correct 1 ms 860 KB Output is correct
13 Correct 1 ms 860 KB Output is correct
14 Correct 1 ms 860 KB Output is correct
15 Correct 1 ms 860 KB Output is correct
16 Correct 1 ms 860 KB Output is correct
17 Correct 1 ms 860 KB Output is correct
18 Correct 1 ms 860 KB Output is correct
19 Correct 1 ms 860 KB Output is correct
20 Correct 1 ms 856 KB Output is correct
21 Correct 1 ms 860 KB Output is correct
22 Correct 1 ms 860 KB Output is correct
23 Correct 2 ms 860 KB Output is correct
24 Correct 1 ms 860 KB Output is correct
25 Correct 2 ms 860 KB Output is correct
26 Correct 6 ms 1372 KB Output is correct
27 Correct 11 ms 1372 KB Output is correct
28 Correct 5 ms 1372 KB Output is correct
29 Correct 11 ms 1380 KB Output is correct
30 Correct 5 ms 1372 KB Output is correct
31 Correct 11 ms 1372 KB Output is correct
32 Correct 5 ms 1372 KB Output is correct
33 Correct 11 ms 1372 KB Output is correct
34 Correct 3 ms 1372 KB Output is correct
35 Correct 11 ms 1372 KB Output is correct
36 Correct 5 ms 1372 KB Output is correct
37 Correct 11 ms 1372 KB Output is correct
38 Correct 6 ms 1372 KB Output is correct
39 Correct 12 ms 1372 KB Output is correct
40 Correct 5 ms 1372 KB Output is correct
41 Correct 11 ms 1372 KB Output is correct
42 Correct 5 ms 1372 KB Output is correct
43 Correct 11 ms 1372 KB Output is correct
44 Correct 5 ms 1372 KB Output is correct
45 Correct 11 ms 1352 KB Output is correct
46 Correct 5 ms 1372 KB Output is correct
47 Correct 1 ms 1116 KB Output is correct
48 Correct 4 ms 1372 KB Output is correct
49 Correct 5 ms 1372 KB Output is correct
50 Correct 3 ms 1372 KB Output is correct
51 Correct 4 ms 1372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 3932 KB Output is correct
2 Correct 6 ms 2396 KB Output is correct
3 Correct 8 ms 3932 KB Output is correct
4 Correct 6 ms 2488 KB Output is correct
5 Correct 6 ms 3932 KB Output is correct
6 Correct 7 ms 2560 KB Output is correct
7 Correct 7 ms 3932 KB Output is correct
8 Correct 6 ms 2396 KB Output is correct
9 Correct 7 ms 3932 KB Output is correct
10 Correct 7 ms 2396 KB Output is correct
11 Correct 7 ms 3932 KB Output is correct
12 Correct 6 ms 2576 KB Output is correct
13 Correct 8 ms 3932 KB Output is correct
14 Correct 6 ms 3796 KB Output is correct
15 Correct 10 ms 2396 KB Output is correct
16 Correct 7 ms 3932 KB Output is correct
17 Correct 7 ms 2396 KB Output is correct
18 Correct 7 ms 3820 KB Output is correct
19 Correct 6 ms 2396 KB Output is correct
20 Correct 7 ms 3772 KB Output is correct
21 Correct 6 ms 2396 KB Output is correct
22 Correct 8 ms 4128 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 860 KB Output is correct
2 Correct 1 ms 860 KB Output is correct
3 Correct 1 ms 856 KB Output is correct
4 Correct 1 ms 860 KB Output is correct
5 Correct 2 ms 860 KB Output is correct
6 Correct 2 ms 860 KB Output is correct
7 Correct 1 ms 856 KB Output is correct
8 Correct 1 ms 860 KB Output is correct
9 Correct 1 ms 860 KB Output is correct
10 Correct 3 ms 860 KB Output is correct
11 Correct 1 ms 856 KB Output is correct
12 Correct 1 ms 860 KB Output is correct
13 Correct 1 ms 860 KB Output is correct
14 Correct 1 ms 860 KB Output is correct
15 Correct 1 ms 860 KB Output is correct
16 Correct 1 ms 860 KB Output is correct
17 Correct 1 ms 860 KB Output is correct
18 Correct 1 ms 860 KB Output is correct
19 Correct 1 ms 860 KB Output is correct
20 Correct 1 ms 856 KB Output is correct
21 Correct 1 ms 860 KB Output is correct
22 Correct 1 ms 860 KB Output is correct
23 Correct 2 ms 860 KB Output is correct
24 Correct 1 ms 860 KB Output is correct
25 Correct 2 ms 860 KB Output is correct
26 Correct 6 ms 1372 KB Output is correct
27 Correct 11 ms 1372 KB Output is correct
28 Correct 5 ms 1372 KB Output is correct
29 Correct 11 ms 1380 KB Output is correct
30 Correct 5 ms 1372 KB Output is correct
31 Correct 11 ms 1372 KB Output is correct
32 Correct 5 ms 1372 KB Output is correct
33 Correct 11 ms 1372 KB Output is correct
34 Correct 3 ms 1372 KB Output is correct
35 Correct 11 ms 1372 KB Output is correct
36 Correct 5 ms 1372 KB Output is correct
37 Correct 11 ms 1372 KB Output is correct
38 Correct 6 ms 1372 KB Output is correct
39 Correct 12 ms 1372 KB Output is correct
40 Correct 5 ms 1372 KB Output is correct
41 Correct 11 ms 1372 KB Output is correct
42 Correct 5 ms 1372 KB Output is correct
43 Correct 11 ms 1372 KB Output is correct
44 Correct 5 ms 1372 KB Output is correct
45 Correct 11 ms 1352 KB Output is correct
46 Correct 5 ms 1372 KB Output is correct
47 Correct 1 ms 1116 KB Output is correct
48 Correct 4 ms 1372 KB Output is correct
49 Correct 5 ms 1372 KB Output is correct
50 Correct 3 ms 1372 KB Output is correct
51 Correct 4 ms 1372 KB Output is correct
52 Correct 7 ms 3932 KB Output is correct
53 Correct 6 ms 2396 KB Output is correct
54 Correct 8 ms 3932 KB Output is correct
55 Correct 6 ms 2488 KB Output is correct
56 Correct 6 ms 3932 KB Output is correct
57 Correct 7 ms 2560 KB Output is correct
58 Correct 7 ms 3932 KB Output is correct
59 Correct 6 ms 2396 KB Output is correct
60 Correct 7 ms 3932 KB Output is correct
61 Correct 7 ms 2396 KB Output is correct
62 Correct 7 ms 3932 KB Output is correct
63 Correct 6 ms 2576 KB Output is correct
64 Correct 8 ms 3932 KB Output is correct
65 Correct 6 ms 3796 KB Output is correct
66 Correct 10 ms 2396 KB Output is correct
67 Correct 7 ms 3932 KB Output is correct
68 Correct 7 ms 2396 KB Output is correct
69 Correct 7 ms 3820 KB Output is correct
70 Correct 6 ms 2396 KB Output is correct
71 Correct 7 ms 3772 KB Output is correct
72 Correct 6 ms 2396 KB Output is correct
73 Correct 8 ms 4128 KB Output is correct
74 Correct 28 ms 10332 KB Output is correct
75 Correct 57 ms 10332 KB Output is correct
76 Correct 26 ms 10332 KB Output is correct
77 Correct 57 ms 10208 KB Output is correct
78 Correct 24 ms 10332 KB Output is correct
79 Correct 24 ms 10588 KB Output is correct
80 Correct 59 ms 10324 KB Output is correct
81 Correct 23 ms 10332 KB Output is correct
82 Correct 19 ms 10204 KB Output is correct
83 Correct 27 ms 10324 KB Output is correct
84 Correct 56 ms 10332 KB Output is correct
85 Correct 25 ms 10332 KB Output is correct
86 Correct 57 ms 10332 KB Output is correct
87 Correct 26 ms 10332 KB Output is correct
88 Correct 72 ms 10332 KB Output is correct
89 Correct 37 ms 10348 KB Output is correct
90 Correct 57 ms 10376 KB Output is correct
91 Correct 25 ms 10212 KB Output is correct
92 Correct 73 ms 10324 KB Output is correct
93 Correct 25 ms 10332 KB Output is correct
94 Correct 18 ms 5724 KB Output is correct
95 Correct 27 ms 10540 KB Output is correct
96 Correct 26 ms 10620 KB Output is correct
97 Correct 24 ms 10332 KB Output is correct
98 Correct 24 ms 10332 KB Output is correct