This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
//#pragma GCC optimize("Ofast")
#define fi first
#define se second
#define ll long long
#define dl double long
using namespace std;
const int NN = 1e9 + 7;
const int N = 1e5 + 7;
const int M = 10;
const ll mod = 1e9 + 7;
const int inf = 1e9 + 7;
const dl rf = 1e-14;
const int B = sqrt(N);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int n;
string s,b;
int f[N][4];
int d[N][M][M];
map < pair < pair < int , int > , int > , int > m;
void solve1()
{
cin >> n >> s >> b;
s = '#' + s;
b = '#' + b;
for( int i = 0; i <= n; i++ ){
for( int j = 0; j < M; j++ ){
for( int h = 0; h < M; h++ ){
d[i][j][h] = 1e9;
}
}
}
for( int i = 1; i <= n; i++ ){
f[i][1] = f[i - 1][1] + (b[i] == '0');
f[i][2] = f[i - 1][2] + (b[i] == '1');
f[i][3] = f[i - 1][3] + (b[i] != s[i]);
}
d[0][0][0] = 0;
//m[{ {1 , 2} , 3 }] = 1;
//m[{ {2 , 1} , 3 }] = 1;
m[{ {3 , 1} , 2 }] = 1;
m[{ {3 , 2} , 1 }] = 1;
for( int i = 1; i <= n; i++ ){
for( int j = 0; j <= 3; j++ ){
if( b[i] == '0' ){
int l = 1 , r = i;
while( l < r ){
int m = (l + r) / 2;
if( f[i][1] - f[m - 1][1] == i - m + 1 )r = m;
else l = m + 1;
}
for( int h = 0; h <= 3; h++ ){
d[i][j][1] = min( d[i][j][1] , d[l - 1][h][j] + 1 - m[{{ h , j } , 1}] );
}
}
if( s[i] == b[i] ){
for( int h = 0; h <= 3; h++ ){
d[i][j][0] = min( d[i][j][0] , d[i - 1][h][j] );
}
}else{
int l = 1 , r = i;
while( l < r ){
int m = (l + r) / 2;
if( f[i][3] - f[m - 1][3] == i - m + 1 )r = m;
else l = m + 1;
}
for( int h = 0; h <= 3; h++ ){
d[i][j][3] = min( d[i][j][3] , d[l - 1][h][j] + 1 - m[{{ h , j } , 3}] );
if( i == 8 && j == 1 ){
//cout << l << " " << h << " " << j << " " << 3 << " " << d[l - 1][h][j] << "\n";
}
}
}
if( b[i] == '1' ){
int l = 1 , r = i;
while( l < r ){
int m = (l + r) / 2;
if( f[i][2] - f[m - 1][2] == i - m + 1 )r = m;
else l = m + 1;
}
for( int h = 0; h <= 3; h++ ){
d[i][j][2] = min( d[i][j][2] , d[l - 1][h][j] + 1 - m[{{ h , j } , 2}] );
}
}
// cout << i << " " << j << " " << d[i][j][0] << " " << d[i][j][1] << " " << d[i][j][2] << " "
// << d[i][j][3] << "\n";
}
}
int ans = 1e9;
for( int i = 0; i <= 3; i++ ){
for( int j = 0; j <= 3; j++ ){
ans = min( ans , d[n][i][j] );
}
}
cout << ans;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
//freopen( "input.txt" , "r" , stdin );
//freopen( "output.txt" , "w" , stdout );
int cghf = 1;//cin >> cghf;
while( cghf-- ){
solve1();
}
}
# | 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... |