Submission #235252

#TimeUsernameProblemLanguageResultExecution timeMemory
235252Nodir_BobievLamps (JOI19_lamps)C++17
47 / 100
1096 ms74696 KiB
    /*
    +----------------------------------------------------------------+
    | In the name of Allah, the most Gracious and the most Merciful. |
    +----------------------------------------------------------------+
    Our hearts quake with fear of you
    Our hearts wake with love for you
    And to Islam we must be true
    In everything we think and do.
    --------------------------------
    When your heart is breaking
    And your pain makes you fall
    Remember just remember
    Allah Sees it all.
    */
    # include <bits/stdc++.h>
    # define FILE
    using namespace std;
     
    const int N = 1e6 + 100;
    const int inf = 1e9;
    string A,B;
    int n;
    vector < vector < int > > dp[N];
     
    char op(int x, char c){
        if( x == 0 ) return c;
        if( x == 1 ) return '0';
        if( x == 2 ) return '1';
        if( c == '1' ) return '0';
        return '1';
    }
     
    int main(){
        # ifdef FILEs
            freopen( "input.txt", "r", stdin );
            freopen( "output.txt", "w", stdout );
        # endif
        ios_base::sync_with_stdio(false);
        cin >> n >> A >> B;
        A = '0' + A;
        B = '0' + B;
     
        dp[0].push_back({0,0,0,0});
     
        for( int i = 1; i <= n; i ++ ){
            for( int a = 0; a < 4; a ++ ){
                for( int b = 0; b < 4; b ++ ){
                    for( int c = 0; c < 4; c ++ ){
                        if( op(c,op(b,op(a,A[i]))) != B[i] )
                            continue;
                        int mn = inf;
                        for( auto vc: dp[i-1] ){
                            int cost = vc[3] + (vc[0]!=a && a) + 
                                               (vc[1]!=b && b) + 
                                               (vc[2]!=c && c);
                            mn = min(mn, cost);
                        }
                        if( mn < inf )
                            dp[i].push_back({a,b,c,mn});
                    }
                }
            }
        }
        int ans = inf;
        for( auto vc: dp[n] )ans = min( ans, vc[3] );
        cout << ans;
    }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...