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>
#define pb push_back
#define all(a) a.begin(), a.end()
#define sz(a) (int)a.size()
#define x first
#define y second
#define debug(...) cout << "[" << #__VA_ARGS__ << ": " << __VA_ARGS__ << "]\n"
#define rd() abs((int)rng())
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int>pii;
const int maxn = 1e6 + 100;
const int mod = 1e9 + 7;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
int n, dp[maxn][3][2]; /// 0 - nothing, 1 - set 1, 2 - set 0
string a, b;
int main()
{
ios_base::sync_with_stdio(false), cin.tie(0);
cin >> n;
cin >> a >> b;
a = '.' + a;
b = '.' + b;
for(int i = 0; i < maxn; i++)
{
for(int j = 0; j < 3; j++)
{
for(int k = 0; k < 2; k++)
dp[i][j][k] = mod;
}
}
dp[0][0][0] = 0;
for(int i = 1; i <= n; i++)
{
for(int op = 0; op < 3; op++)
{
for(int flip = 0; flip < 2; flip++)
{
if(a[i] == b[i])
{
dp[i][0][0] = min(dp[i][0][0], dp[i - 1][op][flip]);
if(a[i] == '1')
{
dp[i][2][1] = min(dp[i][2][1], dp[i - 1][op][flip] + (2 != op) + (1 != flip));
dp[i][1][0] = min(dp[i][1][0], dp[i - 1][op][flip] + (1 != op));
}
else
{
dp[i][1][1] = min(dp[i][1][1], dp[i - 1][op][flip] + (1 != op) + (1 != flip));
dp[i][2][0] = min(dp[i][2][0], dp[i - 1][op][flip] + (2 != op));
}
}
else
{
if(a[i] == '1')
{
dp[i][2][0] = min(dp[i][2][0], dp[i - 1][op][flip] + (2 != op));
dp[i][1][1] = min(dp[i][1][1], dp[i - 1][op][flip] + (1 != op) + (1 != flip));
}
else
{
dp[i][1][0] = min(dp[i][1][0], dp[i - 1][op][flip] + (1 != op));
dp[i][2][1] = min(dp[i][2][1], dp[i - 1][op][flip] + (2 != op) + (1 != flip));
}
dp[i][0][1] = min(dp[i][0][1], dp[i - 1][op][flip] + (1 != flip));
}
}
}
}
int ans = mod;
for(int op = 0; op < 3; op++)
for(int flip = 0; flip < 2; flip++)
{
//cout << dp[n][op][flip] << endl;
ans = min(ans, dp[n][op][flip]);
}
cout << ans << "\n";
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... |