This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// oj.uz JOI19_lamps
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int mod = 1000 * 1000 * 1000 + 7;
const int INF = 1000 * 1000 * 1000;
const ll LINF = (ll)INF * INF;
#ifdef DEBUG
#define dbg(x) cout << #x << " = " << (x) << endl << flush;
#define dbgr(s, f) { cout << #s << ": "; for (auto _ = (s); _ != (f); _++) cout << *_ << ' '; cout << endl << flush; }
#else
#define dbg(x) ;
#define dbgr(s, f) ;
#endif
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define fast_io ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define msub(a, b) ((mod + ((ll)(a) - (b)) % mod) % mod)
#define mdiv(a, b) ((ll)(a) * poww((b), mod - 2) % mod)
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define endl '\n'
#define MAXN 1000100
int n;
int a[MAXN], b[MAXN];
int dp[MAXN][2][3];
int32_t main(void)
{
fast_io;
string sa, sb;
cin >> n >> sa >> sb;
FOR(i, 0, MAXN)
fill(dp[i][0], dp[i][0] + 3, INF), fill(dp[i][1], dp[i][1] + 3, INF);
FOR(i, 0, n)
{
a[i] = sa[i] - '0';
b[i] = sb[i] - '0';
}
// dp base
dp[0][(a[0] != b[0])][0] = (a[0] != b[0]);
dp[0][(b[0] == 1)][1] = (int)(b[0] == 1) + 1;
dp[0][(b[0] == 0)][2] = (int)(b[0] == 0) + 1;
FOR(i, 1, n)
{
if (a[i] == b[i])
dp[i][0][0] = min({dp[i - 1][0][0], dp[i - 1][0][1], dp[i - 1][0][2], dp[i - 1][1][0], dp[i - 1][1][1], dp[i - 1][1][2]});
else
dp[i][1][0] = min({dp[i - 1][1][0], dp[i - 1][1][1], dp[i - 1][1][2], dp[i - 1][0][0] + 1, dp[i - 1][0][1] + 1, dp[i - 1][0][2] + 1});
if (b[i] == 0)
{
dp[i][0][1] = min({dp[i - 1][0][1], dp[i - 1][1][1], dp[i - 1][0][0] + 1, dp[i - 1][1][0] + 1});
dp[i][1][2] = min({dp[i - 1][0][0] + 2, dp[i - 1][0][2] + 1, dp[i - 1][1][0] + 1, dp[i - 1][1][2]});
}
else
{
dp[i][1][1] = min({dp[i - 1][0][0] + 2, dp[i - 1][0][1] + 1, dp[i - 1][1][0] + 1, dp[i - 1][1][1]});
dp[i][0][2] = min({dp[i - 1][0][0] + 1, dp[i - 1][1][0] + 1, dp[i - 1][0][2], dp[i - 1][1][2]});
}
}
int ans = INF;
FOR(i, 0, 2)
FOR(j, 0, 3)
ans = min(ans, dp[n - 1][i][j]);
cout << ans << endl;
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... |