#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 = 1e6 + 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] );
}
}
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 }] = 0;
m[{ {3 , 2} , 1 }] = 0;
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";
}
}
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();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Runtime error |
185 ms |
262148 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |