#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
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++){
| ~~^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
308 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
316 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
312 KB |
Output is correct |
13 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
308 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
316 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
312 KB |
Output is correct |
13 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
139 ms |
66960 KB |
Output is correct |
8 |
Correct |
175 ms |
66920 KB |
Output is correct |
9 |
Correct |
164 ms |
67064 KB |
Output is correct |
10 |
Correct |
167 ms |
66880 KB |
Output is correct |
11 |
Correct |
168 ms |
66960 KB |
Output is correct |
12 |
Correct |
193 ms |
67024 KB |
Output is correct |
13 |
Correct |
172 ms |
66912 KB |
Output is correct |
14 |
Correct |
160 ms |
66964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
308 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
316 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
312 KB |
Output is correct |
13 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |