You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.4 KiB
C++

#define _CRT_SECURE_NO_WARNINGS
#define _SILENCE_CXX17_C_HEADER_DEPRECATION_WARNING
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 50;
int a[2][N];
int vis[2][N];
pair<int, int> que[N];
int rmost(int x)
{
int h = 0, t = 0, ans = x;
for (que[t++] = {!a[0][x], x}; h ^ t; h++)
{
auto x = que[h];
ans = max(ans, x.second);
if (a[x.first][x.second + 1]) que[t++] = {x.first, x.second + 1}, vis[x.first][x.second + 1] = true;
if (a[!x.first][x.second] && !vis[!x.first][x.second]) que[t++] = {!x.first, x.second}, vis[!x.first][x.second] = true;
}
return ans;
}
int main()
{
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%d", &a[0][i]);
for (int i = 0; i < n; i++)
scanf("%d", &a[1][i]);
int l = 0, r = n;
while (a[0][l] == 0 && a[1][l] == 0) l++;
while (a[0][r] == 0 && a[1][r] == 0) r--;
int ans = 0;
while (l < r)
{
int t = rmost(l), t2;
if (t == r) break;
for (t2 = t + 1; t2; t2++)
if (a[0][t2] | a[1][t2])
break;
if ((a[0][t] == 1 && a[0][t2] == 1) || (a[1][t] == 1 && a[1][t2] == 1))
ans += t2 - t - 1;
else
{
ans += t2 - t;
a[0][t2] = a[1][t2] = 1;
}
l = t2;
}
printf("%d", ans);
return 0;
}