이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double lf;
typedef long double Lf;
typedef pair <int,int> pii;
typedef pair <ll, ll> pll;
#define TRACE(x) cerr << #x << " " << x << endl
#define FOR(i, a, b) for (int i = (a); i < int(b); i++)
#define REP(i, n) FOR(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define _ << " " <<
#define fi first
#define sec second
#define mp make_pair
#define pb push_back
const int MAXN = 1000010;
const int inf = 2000000;
int n;
char s[MAXN], p[MAXN];
int dp[MAXN][4][4][4];
vector <pii> tmp;
void normal(vector <int> &v) {
tmp.clear();
REP(i, 3) tmp.pb({v[i], i});
sort(all(tmp));
int cnt = 1;
REP(i, 3) {
if (tmp[i].fi) v[tmp[i].sec] = cnt++;
}
}
int rek(int i, vector <int> v) {
normal(v);
int a = v[0], b = v[1], c = v[2];
if (i == n + 1) return 0;
if (dp[i][a][b][c] != -1) return dp[i][a][b][c];
int ret = inf;
int t = s[i];
if (b && b > c && b > a) t = 0;
if (c && c > b && c > a) t = 1;
if (a && a > b && a > c) {
if (b && b > c) t = 1;
if (c && c > b) t = 0;
if (!b && !c) t = s[i] ^ 1;
}
if (a && b && c && t != p[i]) return dp[i][a][b][c] = inf;
if (!a) {
vector <int> perm = {b, c, 3};
sort(all(perm));
do {
if (!perm[0]) continue;
if (perm[1] && !b) continue;
if (perm[2] && !c) continue;
if (!perm[1] && b) continue;
if (!perm[2] && c) continue;
ret = min(ret, rek(i, {perm[0], perm[1], perm[2]}) + 1);
} while(next_permutation(all(perm)));
}
if (!b) {
vector <int> perm = {a, c, 3};
sort(all(perm));
do {
if (!perm[1]) continue;
if (perm[0] && !a) continue;
if (perm[2] && !c) continue;
if (!perm[0] && a) continue;
if (!perm[2] && c) continue;
ret = min(ret, rek(i, {perm[0], perm[1], perm[2]}) + 1);
} while(next_permutation(all(perm)));
}
if (!c) {
vector <int> perm = {b, a, 3};
sort(all(perm));
do {
if (!perm[2]) continue;
if (perm[1] && !b) continue;
if (perm[0] && !a) continue;
if (!perm[1] && b) continue;
if (!perm[0] && a) continue;
ret = min(ret, rek(i, {perm[0], perm[1], perm[2]}) + 1);
} while(next_permutation(all(perm)));
}
if (t == p[i]) {
ret = min(ret, rek(i + 1, {a, b, c}));
ret = min(ret, rek(i + 1, {0, b, c}));
ret = min(ret, rek(i + 1, {0, 0, c}));
ret = min(ret, rek(i + 1, {0, 0, 0}));
ret = min(ret, rek(i + 1, {a, 0, c}));
ret = min(ret, rek(i + 1, {a, b, 0}));
ret = min(ret, rek(i + 1, {a, 0, 0}));
ret = min(ret, rek(i + 1, {0, b, 0}));
}
return dp[i][a][b][c] = ret;
}
int main() {
cin >> n;
cin >> s >> p;
REP(i, n) {
s[i] -= '0';
p[i] -= '0';
}
for (int i = n; i > 0; i--) {
s[i] = s[i - 1];
p[i] = p[i - 1];
}
memset(dp, -1, sizeof dp);
cout << rek(1, {0, 0, 0}) << endl;
//FOR(i, 1, n + 1) TRACE(i _ rek(i, {0, 0, 0}));
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... |