Submission #393529

# Submission time Handle Problem Language Result Execution time Memory
393529 2021-04-23T18:26:52 Z JimmyZJX The Big Prize (IOI17_prize) C++14
100 / 100
39 ms 1864 KB
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cstring>
#include <climits>
#include <cassert>
#include <tuple>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <unordered_set>
#include <unordered_map>
#include <numeric>

using namespace std;

typedef long long LL;
typedef vector<int> Vi;
typedef vector<LL> VL;
typedef vector<bool> Vb;
typedef vector<vector<int>> Vii;

#define forR(i, n) for (int i = 0; i < (n); i++)

Vi ask(int i);

int cntV = 0, N, cntNotV;

int ask_cache[200003][2];
Vi askC(int i) {
	if (ask_cache[i][0] >= 0) {
		return Vi{ ask_cache[i][0], ask_cache[i][1] };
	}
	Vi rs = ask(i);
	ask_cache[i][0] = rs[0]; ask_cache[i][1] = rs[1];
	return rs;
}

int getLeftNonVCnt(int i) {
	int c = 0;
	while (true) {
		if (i >= N) return cntNotV - c;
		Vi rs = askC(i);
		if (rs[0] + rs[1] == cntNotV) {
			// is a IV node
			return rs[0] - c;
		} else {
			i++;
			c++;
		}
	}
}

int find_best(int n) {
	N = n;
	memset(ask_cache, -1, sizeof(ask_cache));

	if (n <= 5000) {
		forR(i, n) {
			Vi rs = askC(i);
			if (rs[0] == 0 && rs[1] == 0)
				return i;
		}
	}


	cntV = INT_MAX;
	forR(i, 500) {
		Vi rs = askC(i);
		if (rs[0] == 0 && rs[1] == 0) return i;
		cntV = min(cntV, n - rs[0] - rs[1]);
	}

	cntNotV = n - cntV;

	auto getTth = [n](int t) {
		// goal: get the t-th non-V node
		int l = 0, r = n - 1;

		while (l < r) {
			int mid = (l + r) / 2;
			if (getLeftNonVCnt(mid + 1) >= t) {
				r = mid;
			} else {
				l = mid + 1;
			}
		}
		return l;
	};

	int cntIV = INT_MAX; // cnt_ge_IV
	for (int t = 1; t <= 30; t++) {
		int node = getTth(t);
		// l is the t-th non-V node
		assert(getLeftNonVCnt(node) == t - 1);
		Vi rs = askC(node);
		assert(rs[0] + rs[1] < cntNotV);
		if (rs[0] == 0 && rs[1] == 0) return node;
		cntIV = min(cntIV, n - rs[0] - rs[1]);
	}

	int cntLtIV = n - cntIV;

	auto getLeftNonVICnt = [&getTth, cntLtIV](int i_cntV) {
		int c = 0;
		while (true) {
			if (i_cntV > cntNotV) return cntLtIV - c;
			Vi rs = askC(getTth(i_cntV));
			assert(rs[0] + rs[1] <= cntLtIV);
			if (rs[0] + rs[1] == cntLtIV) {
				// is a IIV node
				return rs[0] - c;
			} else {
				i_cntV++;
				c++;
			}
		}
	};

	for (int t = 1; t <= cntLtIV; t++) {
		// goal: get the t-th non-IV node
		int l = 1, r = cntNotV;
		while (l < r) {
			int mid = (l + r) / 2;
			if (getLeftNonVICnt(mid + 1) >= t) {
				r = mid;
			} else {
				l = mid + 1;
			}
		}
		// l is the t-th non-IV node
		int node = getTth(l);
		Vi rs = askC(node);
		assert(rs[0] + rs[1] < cntLtIV);
		if (rs[0] == 0 && rs[1] == 0) return node;
	}

	return -1;

	//int l = 0, r = n - 1;

	//while (l < r) {
	//	int mid = (l + r) / 2;
	//	Vi rs = ask(mid);
	//	if (rs[0] == rs[1]) return mid;
	//	if (rs[0] == 1) {
	//		r = mid - 1;
	//	} else {
	//		l = mid + 1;
	//	}
	//}

	//return l;
}

#ifdef TEST_LOCAL
Vi _answer{ 2,2,2,2,2,2,2,2,2,2,2,1,2,2,2,2,2,2,2,2,2 };
Vi ask(int i) {
	int n = _answer.size();
	assert(i >= 0 && i < n);
	int l = 0, r = 0;
	for (int j = 0; j < n; j++) {
		if (_answer[j] < _answer[i]) {
			if (j < i) l++; else r++;
		}
	}
	return Vi{ l, r };
}

int main() {
	_answer = Vi(6000, 4);
	_answer[3] = 3;
	forR(i, 100) {
		_answer[1000 + i * 10] = 3;
	}
	_answer[5997] = 2;
	_answer[5998] = 2;
	_answer[5999] = 1;

	auto r = find_best(_answer.size());

	return 0;
}
#endif

# Verdict Execution time Memory Grader output
1 Correct 7 ms 1736 KB Output is correct
2 Correct 6 ms 1736 KB Output is correct
3 Correct 8 ms 1736 KB Output is correct
4 Correct 8 ms 1864 KB Output is correct
5 Correct 8 ms 1832 KB Output is correct
6 Correct 1 ms 1828 KB Output is correct
7 Correct 8 ms 1736 KB Output is correct
8 Correct 8 ms 1736 KB Output is correct
9 Correct 8 ms 1828 KB Output is correct
10 Correct 7 ms 1864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 1864 KB Output is correct
2 Correct 4 ms 1864 KB Output is correct
3 Correct 5 ms 1856 KB Output is correct
4 Correct 5 ms 1736 KB Output is correct
5 Correct 9 ms 1864 KB Output is correct
6 Correct 1 ms 1864 KB Output is correct
7 Correct 6 ms 1824 KB Output is correct
8 Correct 4 ms 1864 KB Output is correct
9 Correct 8 ms 1736 KB Output is correct
10 Correct 4 ms 1864 KB Output is correct
11 Correct 11 ms 1860 KB Output is correct
12 Correct 6 ms 1736 KB Output is correct
13 Correct 10 ms 1824 KB Output is correct
14 Correct 10 ms 1864 KB Output is correct
15 Correct 12 ms 1832 KB Output is correct
16 Correct 18 ms 1736 KB Output is correct
17 Correct 5 ms 1736 KB Output is correct
18 Correct 17 ms 1824 KB Output is correct
19 Correct 4 ms 1832 KB Output is correct
20 Correct 15 ms 1828 KB Output is correct
21 Correct 11 ms 1736 KB Output is correct
22 Correct 8 ms 1740 KB Output is correct
23 Correct 8 ms 1736 KB Output is correct
24 Correct 4 ms 1824 KB Output is correct
25 Correct 19 ms 1828 KB Output is correct
26 Correct 19 ms 1728 KB Output is correct
27 Correct 4 ms 1820 KB Output is correct
28 Correct 20 ms 1736 KB Output is correct
29 Correct 16 ms 1832 KB Output is correct
30 Correct 22 ms 1836 KB Output is correct
31 Correct 8 ms 1736 KB Output is correct
32 Correct 8 ms 1736 KB Output is correct
33 Correct 1 ms 1736 KB Output is correct
34 Correct 7 ms 1856 KB Output is correct
35 Correct 4 ms 1832 KB Output is correct
36 Correct 6 ms 1836 KB Output is correct
37 Correct 10 ms 1728 KB Output is correct
38 Correct 8 ms 1728 KB Output is correct
39 Correct 8 ms 1816 KB Output is correct
40 Correct 13 ms 1736 KB Output is correct
41 Correct 15 ms 1808 KB Output is correct
42 Correct 21 ms 1728 KB Output is correct
43 Correct 19 ms 1736 KB Output is correct
44 Correct 13 ms 1864 KB Output is correct
45 Correct 15 ms 1812 KB Output is correct
46 Correct 5 ms 1736 KB Output is correct
47 Correct 12 ms 1728 KB Output is correct
48 Correct 23 ms 1736 KB Output is correct
49 Correct 5 ms 1864 KB Output is correct
50 Correct 26 ms 1736 KB Output is correct
51 Correct 16 ms 1728 KB Output is correct
52 Correct 6 ms 1736 KB Output is correct
53 Correct 10 ms 1736 KB Output is correct
54 Correct 13 ms 1736 KB Output is correct
55 Correct 3 ms 1828 KB Output is correct
56 Correct 11 ms 1816 KB Output is correct
57 Correct 17 ms 1824 KB Output is correct
58 Correct 25 ms 1824 KB Output is correct
59 Correct 22 ms 1736 KB Output is correct
60 Correct 15 ms 1832 KB Output is correct
61 Correct 9 ms 1828 KB Output is correct
62 Correct 6 ms 1832 KB Output is correct
63 Correct 7 ms 1736 KB Output is correct
64 Correct 7 ms 1728 KB Output is correct
65 Correct 8 ms 1736 KB Output is correct
66 Correct 39 ms 1736 KB Output is correct
67 Correct 6 ms 1864 KB Output is correct
68 Correct 6 ms 1824 KB Output is correct
69 Correct 21 ms 1824 KB Output is correct
70 Correct 10 ms 1736 KB Output is correct
71 Correct 33 ms 1736 KB Output is correct
72 Correct 10 ms 1736 KB Output is correct
73 Correct 25 ms 1736 KB Output is correct
74 Correct 26 ms 1736 KB Output is correct
75 Correct 9 ms 1736 KB Output is correct
76 Correct 26 ms 1736 KB Output is correct
77 Correct 30 ms 1736 KB Output is correct
78 Correct 13 ms 1736 KB Output is correct
79 Correct 17 ms 1864 KB Output is correct
80 Correct 28 ms 1736 KB Output is correct
81 Correct 18 ms 1740 KB Output is correct
82 Correct 27 ms 1736 KB Output is correct
83 Correct 6 ms 1864 KB Output is correct
84 Correct 23 ms 1736 KB Output is correct
85 Correct 29 ms 1736 KB Output is correct
86 Correct 18 ms 1736 KB Output is correct
87 Correct 8 ms 1736 KB Output is correct
88 Correct 11 ms 1832 KB Output is correct
89 Correct 14 ms 1736 KB Output is correct
90 Correct 7 ms 1736 KB Output is correct
91 Correct 11 ms 1736 KB Output is correct
92 Correct 10 ms 1736 KB Output is correct
93 Correct 19 ms 1864 KB Output is correct
94 Correct 16 ms 1736 KB Output is correct
95 Correct 14 ms 1736 KB Output is correct
96 Correct 15 ms 1736 KB Output is correct
97 Correct 6 ms 1736 KB Output is correct