이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
int n;
string a, b;
const int maxn = 1000010;
int dp[maxn][5];
//0 is nothing on me
//1 is a '0' on me
//2 is a '1' on me
//3 is a '0' on top of a '1'
//4 is a '1' on top of a '0'
const int cost[5][5] =
{{0, 1, 1, 2, 2},
{0, 0, 1, 1, 2},
{0, 1, 0, 2, 1},
{0, 0, 0, 0, 1},
{0, 0, 0, 1, 0}};
bool issame(int u, int tp) {
if (u == 0) return true;
if (tp == 0) {
return a[u] == b[u];
}
if (tp == 2 || tp == 4) {
return b[u] == '1';
}
if (tp == 1 || tp == 3) {
return b[u] == '0';
}
return false;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
cin >> a >> b;
a = " " + a;
b = " " + b;
for (int i = 1; i < 5; i++) {
dp[0][i] = n;
}
for (int i = 1; i <= n; i++) {
for (int j = 0; j < 5; j++) {
dp[i][j] = n;
for (int k = 0; k < 5; k++) {
//get to j from k
dp[i][j] = min(dp[i][j],
dp[i-1][k] + cost[k][j] +
((!issame(i, j) && issame(i-1, k)) ? 1 : 0) );
}
}
// cout << i << " : " ;
// for (int j = 0; j < 5; j++) {
// cout << dp[i][j] << " ";
// }
// cout << endl;
}
// cout << "v : " << issame(1, 0) << endl;
int res = n;
for (int i = 0; i < 5; i++) {
res = min(res, dp[n][i]);
}
cout << res << endl;
}
# | 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... |