Submission #758329

# Submission time Handle Problem Language Result Execution time Memory
758329 2023-06-14T12:49:54 Z SanguineChameleon Archery (IOI09_archery) C++17
100 / 100
256 ms 12532 KB
#include <bits/stdc++.h>
using namespace std;

void just_do_it();

int main() {
	#ifdef KAMIRULEZ
		freopen("kamirulez.inp", "r", stdin);
		freopen("kamirulez.out", "w", stdout);
	#endif
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	just_do_it();
	return 0;
}

const int WHITE = 0;
const int GRAY = 1;
const int BLACK = 2;

const int maxN = 2e5 + 20;
int a[maxN * 2];
pair<int, int> memo[maxN];
int N, R, K;

int get_color(int X) {
	if (X > K) {
		return WHITE;
	}
	else if (X < K) {
		return BLACK;
	}
	else {
		return GRAY;
	}
}

namespace sub1 {
	int cnt[maxN * 4][3];
	int sum[3];

	pair<int, int> simulate(int start) {
		for (int i = 0; i < maxN * 4; i++) {
			for (int j = 0; j < 3; j++) {
				cnt[i][j] = 0;
			}
		}
		for (int i = 0; i < 3; i++) {
			sum[i] = 0;
		}
		int color1 = -1;
		int color2 = -1;
		for (int i = 1; i <= start * 2 - 1; i++) {
			int color = get_color(a[i]);
			int pos = (i + 1) / 2;
			if (pos == 1) {
				if (color1 == -1) {
					color1 = color;
				}
				else {
					color2 = color;
				}
			}
			else {
				cnt[pos][color]++;
			}
		}
		if (start == 1) {
			if (color1 == -1) {
				color1 = GRAY;
			}
			else {
				color2 = GRAY;
			}
		}
		else {
			cnt[start][GRAY]++;
		}
		for (int i = start * 2; i <= N * 2 - 1; i++) {
			int color = get_color(a[i]);
			int pos = i / 2 + 1;
			if (pos == 1) {
				if (color1 == -1) {
					color1 = color;
				}
				else {
					color2 = color;
				}
			}
			else {
				cnt[pos][color]++;
			}
		}
		int first_gray = -1;
		int wraps = 0;
		for (int round = 1; round <= N * 3; round++) {
			if (round > N * 2 && (color1 == GRAY || color2 == GRAY)) {
				first_gray = round - 1;
				break;
			}
			if (color1 < color2) {
				swap(color1, color2);
			}
			if (color2 == GRAY) {
				wraps++;
			}
			cnt[round + N][color2]++;
			for (int i = 0; i < 3; i++) {
				sum[i] += cnt[round + 1][i];
			}
			for (int i = 2; i >= 0; i--) {
				if (sum[i] > 0) {
					color2 = i;
					sum[i]--;
					break;
				}
			}
		}
		if (R > first_gray) {
			wraps++;
		}
		int pos = (first_gray + N - R) % N + 1;
		return make_pair(pos, wraps);
	}
}


namespace sub2 {
	int cnt[maxN][2];
	int sum[3];

	pair<int, int> simulate(int start) {
		for (int i = 0; i < maxN; i++) {
			for (int j = 0; j < 3; j++) {
				cnt[i][j] = 0;
			}
		}
		for (int i = 0; i < 3; i++) {
			sum[i] = 0;
		}
		for (int i = 1; i <= start * 2 - 1; i++) {
			int color = get_color(a[i]);
			int pos = (i + 1) / 2;
			if (color != BLACK) {
				cnt[pos][color]++;
			}
		}
		cnt[start][GRAY]++;
		for (int i = start * 2; i <= N * 2 - 1; i++) {
			int color = get_color(a[i]);
			int pos = i / 2 + 1;
			if (color != BLACK) {
				cnt[pos][color]++;
			}
		}
		int pos = N;
		int wraps = 0;
		for (int iter = 0; iter < N * 3; iter++) {
			sum[WHITE] += cnt[pos][WHITE];
			sum[GRAY] += cnt[pos][GRAY];
			cnt[pos][WHITE] = 0;
			cnt[pos][GRAY] = 0;
			if (sum[WHITE] + sum[GRAY] >= 2) {
				if (pos == 1) {
					if (sum[GRAY] == 1) {
						cnt[pos][GRAY]++;
						sum[GRAY]--;
					}
					else {
						cnt[pos][WHITE]++;
						sum[WHITE]--;
					}
				}
				else {
					cnt[pos][WHITE]++;
					sum[WHITE]--;
				}
			}
			else if (sum[WHITE] + sum[GRAY] == 1) {
				if (pos == 1) {
					if (sum[GRAY] == 1) {
						wraps++;
					}
				}
				else {
					if (sum[WHITE] == 1) {
						cnt[pos][WHITE]++;
						sum[WHITE]--;
					}
					else {
						cnt[pos][GRAY]++;
						sum[GRAY]--;
					}
				}
			}
			if (pos == 1) {
				pos = N;
			}
			else {
				pos--;
			}
		}
		pos = -1;
		for (int i = 1; i <= N; i++) {
			if (cnt[i][GRAY] == 1) {
				pos = i;
				break;
			}
		}
		return make_pair(pos, wraps);
	}
}

pair<int, int> get_final(int start) {
	if (memo[start].first != -1) {
		return memo[start];
	}
	if (K <= N + 1) {
		return (memo[start] = sub1::simulate(start));
	}
	else {
		return (memo[start] = sub2::simulate(start));
	}
}

pair<int, int> solve(int lt, int rt) {
	pair<int, int> final_lt = get_final(lt);
	pair<int, int> final_rt = get_final(rt);
	if (final_lt.second > final_rt.second) {
		int tmp_lt = lt;
		int tmp_rt = rt;
		int cut = -1;
		while (lt <= rt) {
			int mt = (lt + rt) / 2;
			pair<int, int> final_mt = get_final(mt);
			if (final_mt.second == final_lt.second) {
				cut = mt;
				lt = mt + 1;
			}
			else {
				rt = mt - 1;
			}
		}
		lt = tmp_lt;
		rt = tmp_rt;
		pair<int, int> res1 = solve(lt, cut);
		pair<int, int> res2 = solve(cut + 1, rt);
		if (res1.first < res2.first) {
			return res1;
		}
		else {
			return res2;
		}
	}
	else {
		pair<int, int> res = make_pair(-1, -1);
		while (lt <= rt) {
			int mt = (lt + rt) / 2;
			pair<int, int> final_mt = get_final(mt);
			if (final_mt.first == final_lt.first) {
				res = make_pair(final_mt.first, mt);
				lt = mt + 1;
			}
			else {
				rt = mt - 1;
			}
		}
		return res;
	}
}

void just_do_it() {
	cin >> N >> R >> K;
	R = N * 2 + R % N;
	for (int i = 1; i <= N * 2 - 1; i++) {
		cin >> a[i];
	}
	if (N == 1) {
		cout << 1;
		return;
	}
	if (K == 1) {
		cout << N;
		return;
	}
	if (K == N * 2) {
		cout << 2;
		return;
	}
	for (int i = 1; i <= N; i++) {
		memo[i] = make_pair(-1, -1);
	}
	auto res = solve(1, N);
	cout << res.second;
}

Compilation message

archery.cpp: In function 'std::pair<int, int> sub2::simulate(int)':
archery.cpp:135:15: warning: iteration 2 invokes undefined behavior [-Waggressive-loop-optimizations]
  135 |     cnt[i][j] = 0;
      |     ~~~~~~~~~~^~~
archery.cpp:134:22: note: within this loop
  134 |    for (int j = 0; j < 3; j++) {
      |                    ~~^~~
archery.cpp:135:13: warning: array subscript 2 is above array bounds of 'int [2]' [-Warray-bounds]
  135 |     cnt[i][j] = 0;
      |     ~~~~~~~~^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 22 ms 9728 KB Output is correct
4 Correct 39 ms 9684 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 9732 KB Output is correct
2 Correct 31 ms 9716 KB Output is correct
3 Correct 38 ms 9684 KB Output is correct
4 Correct 71 ms 9948 KB Output is correct
5 Correct 256 ms 12376 KB Output is correct
6 Correct 31 ms 9712 KB Output is correct
7 Correct 15 ms 9684 KB Output is correct
8 Correct 24 ms 9940 KB Output is correct
9 Correct 63 ms 10044 KB Output is correct
10 Correct 20 ms 9684 KB Output is correct
11 Correct 65 ms 10068 KB Output is correct
12 Correct 45 ms 9684 KB Output is correct
13 Correct 161 ms 11644 KB Output is correct
14 Correct 56 ms 9792 KB Output is correct
15 Correct 73 ms 10216 KB Output is correct
16 Correct 21 ms 9684 KB Output is correct
17 Correct 41 ms 9732 KB Output is correct
18 Correct 48 ms 9684 KB Output is correct
19 Correct 47 ms 9768 KB Output is correct
20 Correct 50 ms 9812 KB Output is correct
21 Correct 75 ms 10048 KB Output is correct
22 Correct 68 ms 10196 KB Output is correct
23 Correct 210 ms 12532 KB Output is correct
24 Correct 6 ms 1876 KB Output is correct
25 Correct 8 ms 1876 KB Output is correct
26 Correct 10 ms 1876 KB Output is correct
27 Correct 24 ms 2132 KB Output is correct
28 Correct 144 ms 3924 KB Output is correct
29 Correct 7 ms 1876 KB Output is correct
30 Correct 10 ms 1876 KB Output is correct
31 Correct 18 ms 2220 KB Output is correct
32 Correct 101 ms 4616 KB Output is correct
33 Correct 5 ms 1876 KB Output is correct
34 Correct 4 ms 1876 KB Output is correct
35 Correct 7 ms 1876 KB Output is correct
36 Correct 8 ms 1876 KB Output is correct
37 Correct 15 ms 2172 KB Output is correct
38 Correct 22 ms 2260 KB Output is correct
39 Correct 6 ms 1876 KB Output is correct
40 Correct 6 ms 1876 KB Output is correct
41 Correct 8 ms 1940 KB Output is correct
42 Correct 8 ms 1876 KB Output is correct
43 Correct 12 ms 1876 KB Output is correct
44 Correct 17 ms 2104 KB Output is correct
45 Correct 19 ms 2132 KB Output is correct
46 Correct 19 ms 2248 KB Output is correct
47 Correct 106 ms 4960 KB Output is correct