Submission #377766

# Submission time Handle Problem Language Result Execution time Memory
377766 2021-03-15T00:45:11 Z ecnerwala Exam (eJOI20_exam) C++17
100 / 100
66 ms 8464 KB
#include <bits/stdc++.h>

int main() {
	using namespace std;
	ios_base::sync_with_stdio(false), cin.tie(nullptr);

	int N; cin >> N;
	vector<int> A(N); for (auto& a : A) cin >> a;
	vector<int> B(N); for (auto& b : B) cin >> b;
	vector<array<int, 2>> match(N, array<int, 2>{-1, -1});

	for (int z = 0; z < 2; z++) {
		unordered_map<int, int> in_stack;
		vector<int> stk; stk.reserve(N);
		for (int j = 0; j < N; j++) {
			int i = z ? N-1-j : j;
			while (!stk.empty() && stk.back() <= A[i]) {
				in_stack.erase(stk.back());
				stk.pop_back();
			}
			stk.push_back(A[i]);
			in_stack[A[i]] = i;

			if (in_stack.count(B[i])) {
				match[i][z] = in_stack[B[i]];
			}
		}
	}

	vector<int> V; V.reserve(2*N);
	for (int i = 0; i < N; i++) {
		// match[i][0] is to the left, match[i][1] is to the right
		// We can just process match[i][1] first and the match[i][0]
		if (match[i][1] != -1) {
			V.push_back(match[i][1]);
		}
		if (match[i][0] != -1 && match[i][0] != match[i][1]) {
			V.push_back(match[i][0]);
		}
	}

	// find the maximum (weakly) increasing subsequence of V
	vector<int> best; best.reserve(V.size());
	for (int v : V) {
		int mi = -1;
		int ma = int(best.size());
		while (ma - mi > 1) {
			int md = (mi + ma) / 2;
			if (v >= best[md]) mi = md;
			else ma = md;
		}
		if (ma == int(best.size())) best.push_back(v);
		else best[ma] = v;
	}

	cout << best.size() << '\n';

	return 0;
}

// 1. We can do just ops with 2 people at a time.
//
// 2. People can only increase, so we should do smaller values first.
//
// 3. If we consider the smallest B which passes, it has to come from someone's
//   original score; that original score has to "flow" from the original place to
//   the final place, without crossing anyone bigger in the middle.
//   Afterwards, no bigger ops can cross this B, which means that we can solve
//   the left and right halves separately.
//
//   This gives O(N^3) dp: for each interval dp[interval] = max_{choice of smallest B} dp[left half] + dp[right half] + 1
//
//
// Consider the maximum A; that value never decreases, so smaller values never
// cross; Then, we should do something on the left and something on the right
// then expand the A some amount left and right; there's an interval of the
// smaller stuff that's left exposed
//
//
// Each B either comes from the leftmost equal A, comes from the rightmost equal A, or is impossible
// What you need, as you sweep from left to right
//   A1 <= A2
# 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 492 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 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 12 ms 1280 KB Output is correct
3 Correct 34 ms 6928 KB Output is correct
4 Correct 42 ms 3548 KB Output is correct
5 Correct 55 ms 4844 KB Output is correct
6 Correct 30 ms 2588 KB Output is correct
7 Correct 52 ms 3560 KB Output is correct
8 Correct 43 ms 3820 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 2 ms 492 KB Output is correct
4 Correct 3 ms 748 KB Output is correct
5 Correct 3 ms 748 KB Output is correct
6 Correct 3 ms 748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 492 KB Output is correct
2 Correct 18 ms 1772 KB Output is correct
3 Correct 37 ms 3052 KB Output is correct
4 Correct 43 ms 3864 KB Output is correct
5 Correct 66 ms 8464 KB Output is correct
6 Correct 54 ms 5580 KB Output is correct
7 Correct 58 ms 6348 KB Output is correct
8 Correct 36 ms 3052 KB Output is correct
9 Correct 44 ms 3600 KB Output is correct
10 Correct 49 ms 5368 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 492 KB Output is correct
4 Correct 1 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 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 492 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 492 KB Output is correct
4 Correct 1 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 2 ms 492 KB Output is correct
10 Correct 3 ms 748 KB Output is correct
11 Correct 3 ms 748 KB Output is correct
12 Correct 3 ms 748 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 492 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 3 ms 492 KB Output is correct
24 Correct 3 ms 492 KB Output is correct
25 Correct 3 ms 492 KB Output is correct
26 Correct 2 ms 492 KB Output is correct
27 Correct 2 ms 492 KB Output is correct
28 Correct 3 ms 492 KB Output is correct
29 Correct 3 ms 620 KB Output is correct
30 Correct 4 ms 620 KB Output is correct
31 Correct 3 ms 620 KB Output is correct
32 Correct 3 ms 620 KB Output is correct