Submission #747049

#TimeUsernameProblemLanguageResultExecution timeMemory
747049stevancvLamps (JOI19_lamps)C++14
100 / 100
71 ms35808 KiB
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 1e6 + 2;
const int inf = 1e9;
int dp[N][2][3], a[N], b[N];
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n; cin >> n;
    string A, B;
    cin >> A >> B;
    for (int i = 0; i < n; i++) {
        a[i + 1] = A[i] - '0';
        b[i + 1] = B[i] - '0';
    }
    for (int i = 0; i <= n; i++) for (int j = 0; j < 6; j++) dp[i][j / 3][j % 3] = inf;
    dp[0][0][0] = 0;
    for (int i = 1; i <= n; i++) {
        if (a[i] == b[i]) for (int j = 0; j < 6; j++) smin(dp[i][0][0], dp[i - 1][j / 3][j % 3]);
        if (b[i] == 0) for (int j = 0; j < 6; j++) smin(dp[i][0][1], dp[i - 1][j / 3][j % 3] + (j % 3 != 1));
        if (b[i] == 1) for (int j = 0; j < 6; j++) smin(dp[i][0][2], dp[i - 1][j / 3][j % 3] + (j % 3 != 2));
        if (a[i] != b[i]) for (int j = 0; j < 6; j++) smin(dp[i][1][0], dp[i - 1][j / 3][j % 3] + (j < 3));
        if (b[i] == 1) for (int j = 0; j < 6; j++) smin(dp[i][1][1], dp[i - 1][j / 3][j % 3] + (j % 3 != 1) + (j < 3));
        if (b[i] == 0) for (int j = 0; j < 6; j++) smin(dp[i][1][2], dp[i - 1][j / 3][j % 3] + (j % 3 != 2) + (j < 3));
    }
    int ans = inf;
    for (int j = 0; j < 6; j++) smin(ans, dp[n][j / 3][j % 3]);
    cout << ans << en;
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...