#include <bits/stdc++.h>
using namespace std;
void setup()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int n, f[1000000][3], a[1000000], b[1000000];
char c;
int main()
{
setup();
cin >> n;
for (int i = 0; i < n; ++i)
{
cin >> c;
a[i] = c - '0';
}
for (int i = 0; i < n; ++i)
{
cin >> c;
b[i] = c - '0';
}
f[0][0] = 1 + (b[0] == 1);
f[0][1] = 1 + (b[0] == 0);
f[0][2] = (a[0] != b[0]);
/* the key is if the color to paint of this cell different from it's requirement
and the previous cell have not been inversed then this cell have to be inversed*/
for (int i = 1; i < n; ++i)
{
f[i][0] = min({f[i - 1][0] + (b[i] == 1 && b[i - 1] == 0),
f[i - 1][1] + 1 + (b[i] == 1 && b[i - 1] == 1),
f[i - 1][2] + 1 + (b[i] == 1 && b[i - 1] == a[i - 1])});
f[i][1] = min({f[i - 1][0] + 1 + (b[i] == 0 && b[i - 1] == 0),
f[i - 1][1] + (b[i] == 0 && b[i - 1] == 1),
f[i - 1][2] + 1 + (b[i] == 0 && b[i - 1] == a[i - 1])});
f[i][2] = min({f[i - 1][0] + (a[i] != b[i] && b[i - 1] == 0),
f[i - 1][1] + (a[i] != b[i] && b[i - 1] == 1),
f[i - 1][2] + (a[i] != b[i] && a[i - 1] == b[i - 1])});
}
cout << min({f[n - 1][0], f[n - 1][1], f[n - 1][2]});
return 0;
}
# | 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... |