Submission #428042

#TimeUsernameProblemLanguageResultExecution timeMemory
428042tengiz05Lamps (JOI19_lamps)C++17
4 / 100
10 ms15952 KiB
#include <bits/stdc++.h> int dp[2000][2000]; int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int n; std::cin >> n; if (n > 500) { std::string a, b; std::cin >> a >> b; int ans = 0; for (int i = 0; i < n; i++) { if (b[i] != '1') { continue; } while (i + 1 < n && b[i + 1] == '1') { i++; } ans++; } std::cout << ans << "\n"; } else { std::string a[2], b; std::cin >> a[0] >> b; a[1] = a[0]; for (int i = 0; i < n; i++) { a[1][i] ^= 1; } auto cost = [&](char c, int l, int r) { int ans = 0; for (int i = l; i <= r; i++) { if (c != b[i]) { while (i + 1 <= r && b[i + 1] != c) { i++; } ans++; } } return ans; }; auto xorcost = [&](int l, int r, int f) { int ans = 0; for (int i = l; i <= r; i++) { if (a[f][i] != b[i]) { while (i + 1 <= r && b[i + 1] != a[f][i + 1]) { i++; } ans++; } } return ans; }; memset(dp, -1, sizeof dp); std::function<int(int, int)> calc = [&](int l, int r) { if (l == r) { return int(a[0][l] != b[l]); } int &ret = dp[l][r]; if (~ret) { return ret; } ret = 1000000; ret = std::min(ret, cost('0', l, r) + bool(std::count(a[0].begin() + l, a[0].begin() + r + 1, '1'))); ret = std::min(ret, cost('1', l, r) + bool(std::count(a[0].begin() + l, a[0].begin() + r + 1, '0'))); ret = std::min(ret, xorcost(l, r, 0)); ret = std::min(ret, xorcost(l, r, 1) + 1); for (int i = l; i < r; i++) { ret = std::min(ret, calc(l, i) + calc(i + 1, r)); } return ret; }; std::cout << calc(0, n - 1) << "\n"; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...