This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
/*
0 = nothing.
1 = toggle
2 = set 0s
3 = set 1s
0: 0 0 0
1: 0 0 1
2: 0 0 2
3: 0 0 3
4: 0 1 2
5: 0 1 3
6: 0 2 1
7: 0 2 3
8: 0 3 1
9: 0 3 2
10: 1 2 3
11: 1 3 2
12: 2 1 3
13: 2 3 1
14: 3 1 2
15: 3 2 1
*/
const int maxN = 1e6 + 5, inf = 1e9;
int dp[maxN][16];
bool res[16][2] = {{0, 1}, {1, 0}, {1, 1}, {0, 0}, {0, 0}, {1, 1}, {1, 1}, {1, 1}, {0, 0}, {0, 0}, {1, 1}, {0, 0}, {1, 1}, {0, 0}, {0, 0}, {1, 1}};
vector<vector<int> > ops = {{}, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 1}, {2, 3}, {3, 1}, {3, 2}, {1, 2, 3}, {1, 3, 2}, {2, 1, 3}, {2, 3, 1}, {3, 1, 2}, {3, 2, 1}};
int ops_needed[16][16];
int lcs(vector<int> X, vector<int> Y)
{
int m = X.size();
int n = Y.size();
int L[m + 1][n + 1];
for (int i = 0; i <= m; i++) {
for (int j = 0; j <= n; j++) {
if (i == 0 || j == 0)
L[i][j] = 0;
else if (X[i - 1] == Y[j - 1])
L[i][j] = L[i - 1][j - 1] + 1;
else
L[i][j] = max(L[i - 1][j], L[i][j - 1]);
}
}
return L[m][n];
}
int main(){
for (int i = 0; i < 16; i++){
for (int j = 0; j < 16; j++){
ops_needed[i][j] = ops[j].size() - lcs(ops[i], ops[j]);
}
}
int n;
cin >> n;
string a, b;
cin >> a >> b;
dp[0][0] = 0;
for (int i = 1; i < 16; i++) dp[0][i] = inf;
for (int i = 1; i <= n; i++){
bool a_bit = a[i-1] - '0', b_bit = b[i-1] - '0';
for (int j = 0; j < 16; j++){
dp[i][j] = inf;
if (res[a_bit][j] != b_bit) continue;
for (int k = 0; k < 16; k++) dp[i][j] = min(dp[i][j], dp[i-1][k] + ops_needed[k][j]);
}
}
int ans = inf;
for (int i = 0; i < 16; i++) ans = min(ans, dp[n][i]);
cout << ans << endl;
}
Compilation message (stderr)
lamp.cpp: In function 'int main()':
lamp.cpp:79:20: warning: iteration 2 invokes undefined behavior [-Waggressive-loop-optimizations]
79 | if (res[a_bit][j] != b_bit) continue;
| ~~~~~~~~~~~~^
lamp.cpp:77:21: note: within this loop
77 | for (int j = 0; j < 16; j++){
| ~~^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |