Submission #469811

#TimeUsernameProblemLanguageResultExecution timeMemory
469811aZvezdaLamps (JOI19_lamps)C++14
47 / 100
19 ms15776 KiB
#include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4")
#define endl "\n"
typedef long long ll;
template<class T, class T2> inline ostream &operator <<(ostream &out, const pair<T, T2> &x) { out << x.first << " " << x.second; return out;}
template<class T, class T2> inline istream &operator >>(istream &in, pair<T, T2> &x) { in >> x.first >> x.second; return in;}
template<class T, class T2> inline bool chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; }
template<class T, class T2> inline bool chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; }
const ll mod = 1e9 + 7;
#define out(x) "{" << (#x) << ": " << x << "} "

const int MAX_N = 1e5 + 10;
ll dp[MAX_N][3][2];
string a, b;
int n;

signed main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    cin >> n >> a >> b;
    a = "x" + a;
    b = "x" + b;

    for(int i = 0; i < MAX_N; i ++) {
        for(int j = 0; j < 3; j ++) {
            for(int k = 0; k < 2; k ++) {
                dp[i][j][k] = mod;
            }
        }
    }

    dp[0][2][0] = 0;

    for(int i = 1; i <= n; i ++) {
        for(int j = 0; j < 2; j ++) {
            for(int k = 0; k < 2; k ++) {

                int curr = j;
                if(k) {curr = (1 - curr);}
                if(curr != b[i] - '0') {continue;}

                for(int prevj = 0; prevj < 3; prevj ++) {
                    for(int prevk = 0; prevk < 2; prevk ++) {

                        int cost;
                        if(prevj != j) {
                            cost = 1;
                        } else {
                            cost = 0;
                        }

                        if(k == 1 && prevk != k) {
                            cost ++;
                        }

                        chkmin(dp[i][j][k], dp[i - 1][prevj][prevk] + cost);
                    }
                }

            }
        }

        if(a[i] == b[i]) {
            for(int j = 0; j < 3; j ++) {
                for(int k = 0; k < 2; k ++) {
                    chkmin(dp[i][2][0], dp[i - 1][j][k]);
                }
            }
        } else {
            for(int j = 0; j < 3; j ++) {
                for(int k = 0; k < 2; k ++) {
                    chkmin(dp[i][2][1], dp[i - 1][j][k] + (1 - k));
                }
            }
        }
    }

    ll ret = mod;
    for(int i = 0; i < 3; i ++) {
        for(int j = 0; j < 2; j ++) {
            chkmin(ret, dp[n][i][j]);
        }
    }
    cout << ret << endl;
    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...