Submission #1369696

#TimeUsernameProblemLanguageResultExecution timeMemory
1369696gelastropodJust Long Neckties 2 (JOI25_ho_t4)C++20
100 / 100
2178 ms121884 KiB
#include <vector>
#include <iostream>
using namespace std;
#define int long long

#define SIZE 21LL

int calc(int i, int a) {
	i += (1LL << a);
	for (int j = a - 1; j >= 0; j--) {
		if (i & (1LL << j)) {
			i -= (1LL << j);
			return i;
		}
	}
	return i;
}

signed main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	int N, a; cin >> N;
	vector<int> A, vals(1LL << SIZE, -1), work;
	vector<vector<int>> next(SIZE * SIZE, vector<int>((N + SIZE * SIZE - 2) / (SIZE * SIZE), N));
	for (int i = 0; i < N; i++) {
		cin >> a; a--;
		A.push_back(a);
	}
	for (int i = N - 2; i >= 0; i--) next[A[i] * 21 + A[i + 1]][i / (SIZE * SIZE)] = i;
	for (int i = next[0].size() - 2; i >= 0; i--) for (int j = 0; j < SIZE * SIZE; j++) next[j][i] = min(next[j][i], next[j][i + 1]);
	for (int i = 0; i < (1LL << SIZE); i++) {
		int crnt = vals[i], res = N;
		for (; crnt < N - 2 && crnt < (vals[i] / (SIZE * SIZE) + 1) * SIZE * SIZE - 1 && ((i & (1LL << A[crnt + 1])) || (i & (1LL << A[crnt + 2]))); crnt++);
		if (crnt >= N - 2) {
			work.push_back(i);
			continue;
		}
		if (!(i & (1LL << A[crnt + 1])) && !(i & (1LL << A[crnt + 2]))) {
			vals[calc(i, A[crnt + 1])] = max(vals[calc(i, A[crnt + 1])], crnt + 1);
			vals[calc(i, A[crnt + 2])] = max(vals[calc(i, A[crnt + 2])], crnt + 2);
			continue;
		}
		for (int j = 0; j < SIZE * SIZE; j++) {
			if ((i & (1LL << (j / SIZE))) || (i & (1LL << (j % SIZE)))) continue;
			res = min(res, next[j][(crnt + 1) / (SIZE * SIZE)] - 1);
		}
		if (res >= N - 2) {
			work.push_back(i);
			continue;
		}
		vals[calc(i, A[res + 1])] = max(vals[calc(i, A[res + 1])], res + 1);
		vals[calc(i, A[res + 2])] = max(vals[calc(i, A[res + 2])], res + 2);
	}
	int ans = SIZE;
	for (int i : work) {
		int crnt = 0;
		for (int j = 0; j < SIZE; j++) crnt += (bool)(i & (1LL << j));
		ans = min(ans, crnt);
	}
	cout << ans << '\n';
}
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...