Submission #235274

#TimeUsernameProblemLanguageResultExecution timeMemory
235274Nodir_BobievLamps (JOI19_lamps)C++17
100 / 100
572 ms105116 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, gettt[30][30]; char precount[2][30]; vector < pair < int, int > > dp[N]; vector < vector < int > > oper= { {}, {1}, {2}, {3}, {1,2}, {1,3}, {2,1}, {2,3}, {3,1}, {3,2}, {1,2,3}, {1,3,2}, {2,1,3}, {2,3,1}, {3,1,2}, {3,2,1} }; inline 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'; } inline int get( vector < int >&v1, vector < int >&v2, int i=0, int j=0 ){ if( i == (int)v1.size() || j == (int)v2.size())return 0; int mx = 0; if( v1[i] == v2[j] ) mx = get(v1, v2, i+1, j+1) + 1; mx = max({mx, get(v1, v2, i+1, j), get(v1, v2, i, j+1)}); return mx; } 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; for( size_t i = 0; i < oper.size(); i ++ ){ for( size_t j = 0; j < oper.size(); j ++ ){ gettt[i][j] = get(oper[i], oper[j]); } } for( int i = 0; i < 2; i ++ ){ for( size_t j = 0; j < oper.size(); j ++ ){ precount[i][j] = '0'+i; for( auto c: oper[j] ){ precount[i][j] = op(c, precount[i][j]); } } } dp[0].push_back({0, 0}); for( int i = 1; i <= n; i ++ ){ for( size_t j = 0; j < oper.size(); j ++){ if( precount[A[i]-48][j] != B[i] ) continue; int res = inf; for( auto v2: dp[i-1] ){ res = min( res, v2.second + (int)oper[j].size() - gettt[j][v2.first] ); } dp[i].push_back({j,res}); } } int ans = inf; for( auto vc: dp[n] )ans = min( ans, vc.second ); 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...