Submission #206675

# Submission time Handle Problem Language Result Execution time Memory
206675 2020-03-04T12:29:30 Z opukittpceno_hhr Rope (JOI17_rope) C++17
0 / 100
2500 ms 256 KB
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")

#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <fstream>
#include <cassert>
#include <cstring>
#include <unordered_set>
#include <unordered_map>
#include <numeric>
#include <ctime>
#include <bitset>
#include <complex>
#include <random>
#include <functional>

using namespace std;

const int N = 15;
const int NONE = -1;

int eq(int a, int b) {
	if (a == NONE || b == NONE) {
		return 1;
	} else {
		return a == b;
	}
}

int dp[N][N + 1];
int sdp[N + 1];
int pdp[N + 1];

int ts[N];
int s[N + 1];

int cans(int n) {
	for (int i = 0; i < N; i++) {
		for (int j = 0; j <= N; j++) {
			dp[i][j] = -1;
		}
	}
	function<int(int, int)> can = [&](int i, int len) {
		if (len % 2 == 1) {
			return 0;
		}
		if (len == 0) {
			return 1;
		}
		if (dp[i][len] == -1) {
			dp[i][len] = can(i + 1, len - 2) & ((ts[i] == ts[i + len - 1]));
		}
		return dp[i][len];
	};
	fill(pdp, pdp + N + 1, 0);
	fill(sdp, sdp + N + 1, 0);
	pdp[0] = 1;
	sdp[0] = 1;
	for (int i = 0; i < n; i++) {
		for (int len = 2; i + len <= n; len++) {
			pdp[i + len / 2] |= (pdp[i] & can(i, len));
		}
	}
	reverse(ts, ts + n);
	for (int i = 0; i < N; i++) {
		for (int j = 0; j <= N; j++) {
			dp[i][j] = -1;
		}
	}
	for (int i = 0; i < n; i++) {
		for (int len = 2; i + len <= n; len++) {
			sdp[i + len / 2] |= (sdp[i] & can(i, len));
		}
	}
	int ans = n;
	for (int i = 0; i <= n; i++) {
		if (n - 2 - i >= 0) {
			ans = min(ans, pdp[i] + sdp[n - 2 - i]);
		}
	}
	return ans == 0;
}


int solve(int n, int a, int b) {
	int ans = n;
	for (int i = 0; i < (1 << n); i++) {
		for (int j = 0; j < n; j++) {
			if ((i >> j) & 1) {
				ts[j] = a;
			} else {
				ts[j] = b;
			}
		}
		if (cans(n)) {
			int c = 0;
			for (int j = 0; j < n; j++) {
				c += !eq(s[j], ts[j]);
			}
			ans = min(ans, c);
		}
	}
	for (auto t : s) {
		ans += (t == NONE);
	}
	return ans;
}

signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);

	int n, m;
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		cin >> s[i];
	}
	vector<int> ans(m, n);
	for (int i = 0; i < m; i++) {
		for (int j = i; j < m; j++) {
			int v = solve(n, i, j);
			ans[i] = min(ans[i], v);
			ans[j] = min(ans[j], v);
		}
	}
	for (auto t : ans) {
		cout << t << '\n';
	}
}
# Verdict Execution time Memory Grader output
1 Execution timed out 2573 ms 256 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2573 ms 256 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2573 ms 256 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2573 ms 256 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2573 ms 256 KB Time limit exceeded
2 Halted 0 ms 0 KB -