#include <bits/stdc++.h>
using namespace std;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#define flash ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define debug(x) cerr << " - " << #x << ": " << x << endl;
#define debugs(x, y) cerr << " - " << #x << ": " << x << " " << #y << ": " << y << endl;
#define all(x) (x).begin(),(x).end()
#define sz(x) (ll)x.size()
#define ll long long
#define INF 1000000000
#define MOD 1000000007
#define pb push_back
#define ve vector<ll>
#define dos pair<ll,ll>
#define vedos vector<dos>
#define rand mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
struct greateri
{
template<class T>
bool operator()(T const &a, T const &b) const { return a > b; }
};
string kal,kal1;
ll n;
ll dp[1000001][3][5];
ll on[1000001],off[1000001],tog[1000001],noth[1000001];
ll on1[1000001],off1[1000001],tog1[1000001],noth1[1000001];
bool check(ll x)
{
if(x!=-1)return true;
return false;
}
ll solve(ll index,ll state,ll last)
{
ll ans=INF;
if(index>=n)return 0;
if(dp[index][state][last]!=-1)return dp[index][state][last];
if(check(on[index]))
{
if(last==1)ans=min(ans,solve(index+1,1,1));
else ans=min(ans,1+solve(index+1,1,1));
}
if(check(noth[index]))
{
ans=min(ans,solve(index+1,1,2));
}
if(check(off[index]))
{
if(last==3)ans=min(ans,solve(index+1,1,3));
else ans=min(ans,1+solve(index+1,1,3));
}
if(check(tog[index]))
{
if(last==4)ans=min(ans,solve(index+1,1,4));
else ans=min(ans,1+solve(index+1,1,4));
}
if(state==1)
{
if(check(on1[index]))
{
if(last==1)ans=min(ans,1+solve(index+1,2,1));
else ans=min(ans,2+solve(index+1,2,1));
}
if(check(noth1[index]))
{
ans=min(ans,1+solve(index+1,2,2));
}
if(check(off1[index]))
{
if(last==3)ans=min(ans,1+solve(index+1,2,3));
else ans=min(ans,2+solve(index+1,2,3));
}
if(check(tog1[index]))
{
if(last==4)ans=min(ans,1+solve(index+1,2,4));
else ans=min(ans,2+solve(index+1,2,4));
}
}
if(state==2)
{
if(check(on1[index]))
{
if(last==1)ans=min(ans,solve(index+1,2,1));
else ans=min(ans,1+solve(index+1,2,1));
}
if(check(noth1[index]))
{
ans=min(ans,solve(index+1,2,2));
}
if(check(off1[index]))
{
if(last==3)ans=min(ans,solve(index+1,2,3));
else ans=min(ans,1+solve(index+1,2,3));
}
if(check(tog1[index]))
{
if(last==4)ans=min(ans,solve(index+1,2,4));
else ans=min(ans,1+solve(index+1,2,4));
}
}
return dp[index][state][last]=ans;
}
ll preprocess(ll arr[])
{
ll fin=-1;
ll deb=-1;
bool ka=false;
for (ll i = 0; i < n; ++i)
{
if(arr[i]==0)
{
if(deb!=-1 && fin!=-1)
{
for (ll j = deb; j <= fin; ++j)
{
arr[j]=fin;
}
}
deb=fin=-1;
ka=false;
arr[i]=-1;
}
else if(!ka)
{
ka=true;
deb=fin=i;
}
else
{
fin++;
}
}
if(deb!=-1)
{
for (ll j = deb; j <= fin; ++j)
{
arr[j]=fin;
}
}
}
int main()
{
flash;
cin>>n;
cin>>kal>>kal1;
memset(dp,-1,sizeof dp);
for (ll i = 0; i < kal.size(); ++i)
{
if(kal[i]=='0')
{
if(kal1[i]==kal[i])
{
off[i]=1;
noth[i]=1;
tog1[i]=1;
on1[i]=1;
}
else
{
on[i]=1;
tog[i]=1;
off1[i]=1;
noth1[i]=1;
}
}
else
{
if(kal1[i]==kal[i])
{
on[i]=1;
noth[i]=1;
tog1[i]=1;
off1[i]=1;
}
else
{
off[i]=1;
tog[i]=1;
on1[i]=1;
noth1[i]=1;
}
}
}
preprocess(off);
preprocess(on);
preprocess(tog);
preprocess(noth);
preprocess(off1);
preprocess(on1);
preprocess(tog1);
preprocess(noth1);
cout<<solve(0,1,0);
return 0;
}
//code the AC sol !
// BS/queue/map
Compilation message
lamp.cpp:4:0: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
#pragma GCC optimization ("O3")
lamp.cpp:5:0: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
#pragma GCC optimization ("unroll-loops")
lamp.cpp: In function 'long long int preprocess(long long int*)':
lamp.cpp:141:1: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
lamp.cpp: In function 'int main()':
lamp.cpp:148:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (ll i = 0; i < kal.size(); ++i)
~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
65 ms |
117752 KB |
Output is correct |
2 |
Correct |
66 ms |
117752 KB |
Output is correct |
3 |
Correct |
65 ms |
117752 KB |
Output is correct |
4 |
Correct |
69 ms |
117856 KB |
Output is correct |
5 |
Correct |
70 ms |
117856 KB |
Output is correct |
6 |
Correct |
67 ms |
117752 KB |
Output is correct |
7 |
Correct |
69 ms |
117880 KB |
Output is correct |
8 |
Correct |
83 ms |
117764 KB |
Output is correct |
9 |
Correct |
72 ms |
117752 KB |
Output is correct |
10 |
Correct |
70 ms |
117856 KB |
Output is correct |
11 |
Correct |
72 ms |
117880 KB |
Output is correct |
12 |
Correct |
71 ms |
117752 KB |
Output is correct |
13 |
Correct |
68 ms |
117752 KB |
Output is correct |
14 |
Correct |
71 ms |
117880 KB |
Output is correct |
15 |
Correct |
69 ms |
117752 KB |
Output is correct |
16 |
Correct |
67 ms |
117752 KB |
Output is correct |
17 |
Correct |
68 ms |
117752 KB |
Output is correct |
18 |
Correct |
66 ms |
117752 KB |
Output is correct |
19 |
Correct |
68 ms |
117752 KB |
Output is correct |
20 |
Correct |
66 ms |
117752 KB |
Output is correct |
21 |
Correct |
68 ms |
117752 KB |
Output is correct |
22 |
Correct |
66 ms |
117752 KB |
Output is correct |
23 |
Correct |
67 ms |
117752 KB |
Output is correct |
24 |
Correct |
67 ms |
117752 KB |
Output is correct |
25 |
Correct |
68 ms |
117900 KB |
Output is correct |
26 |
Correct |
65 ms |
117752 KB |
Output is correct |
27 |
Correct |
68 ms |
117752 KB |
Output is correct |
28 |
Correct |
71 ms |
117752 KB |
Output is correct |
29 |
Correct |
69 ms |
117752 KB |
Output is correct |
30 |
Correct |
67 ms |
117748 KB |
Output is correct |
31 |
Correct |
65 ms |
117752 KB |
Output is correct |
32 |
Correct |
67 ms |
117752 KB |
Output is correct |
33 |
Correct |
67 ms |
117752 KB |
Output is correct |
34 |
Correct |
67 ms |
117752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
65 ms |
117752 KB |
Output is correct |
2 |
Correct |
66 ms |
117752 KB |
Output is correct |
3 |
Correct |
65 ms |
117752 KB |
Output is correct |
4 |
Correct |
69 ms |
117856 KB |
Output is correct |
5 |
Correct |
70 ms |
117856 KB |
Output is correct |
6 |
Correct |
67 ms |
117752 KB |
Output is correct |
7 |
Correct |
69 ms |
117880 KB |
Output is correct |
8 |
Correct |
83 ms |
117764 KB |
Output is correct |
9 |
Correct |
72 ms |
117752 KB |
Output is correct |
10 |
Correct |
70 ms |
117856 KB |
Output is correct |
11 |
Correct |
72 ms |
117880 KB |
Output is correct |
12 |
Correct |
71 ms |
117752 KB |
Output is correct |
13 |
Correct |
68 ms |
117752 KB |
Output is correct |
14 |
Correct |
71 ms |
117880 KB |
Output is correct |
15 |
Correct |
69 ms |
117752 KB |
Output is correct |
16 |
Correct |
67 ms |
117752 KB |
Output is correct |
17 |
Correct |
68 ms |
117752 KB |
Output is correct |
18 |
Correct |
66 ms |
117752 KB |
Output is correct |
19 |
Correct |
68 ms |
117752 KB |
Output is correct |
20 |
Correct |
66 ms |
117752 KB |
Output is correct |
21 |
Correct |
68 ms |
117752 KB |
Output is correct |
22 |
Correct |
66 ms |
117752 KB |
Output is correct |
23 |
Correct |
67 ms |
117752 KB |
Output is correct |
24 |
Correct |
67 ms |
117752 KB |
Output is correct |
25 |
Correct |
68 ms |
117900 KB |
Output is correct |
26 |
Correct |
65 ms |
117752 KB |
Output is correct |
27 |
Correct |
68 ms |
117752 KB |
Output is correct |
28 |
Correct |
71 ms |
117752 KB |
Output is correct |
29 |
Correct |
69 ms |
117752 KB |
Output is correct |
30 |
Correct |
67 ms |
117748 KB |
Output is correct |
31 |
Correct |
65 ms |
117752 KB |
Output is correct |
32 |
Correct |
67 ms |
117752 KB |
Output is correct |
33 |
Correct |
67 ms |
117752 KB |
Output is correct |
34 |
Correct |
67 ms |
117752 KB |
Output is correct |
35 |
Correct |
69 ms |
118008 KB |
Output is correct |
36 |
Correct |
69 ms |
118008 KB |
Output is correct |
37 |
Correct |
69 ms |
118008 KB |
Output is correct |
38 |
Correct |
69 ms |
118136 KB |
Output is correct |
39 |
Correct |
70 ms |
118008 KB |
Output is correct |
40 |
Correct |
69 ms |
117968 KB |
Output is correct |
41 |
Correct |
69 ms |
118008 KB |
Output is correct |
42 |
Correct |
70 ms |
118008 KB |
Output is correct |
43 |
Correct |
68 ms |
118008 KB |
Output is correct |
44 |
Correct |
68 ms |
118008 KB |
Output is correct |
45 |
Correct |
67 ms |
118008 KB |
Output is correct |
46 |
Correct |
68 ms |
118008 KB |
Output is correct |
47 |
Correct |
69 ms |
118008 KB |
Output is correct |
48 |
Correct |
70 ms |
118012 KB |
Output is correct |
49 |
Correct |
69 ms |
118008 KB |
Output is correct |
50 |
Correct |
67 ms |
118008 KB |
Output is correct |
51 |
Correct |
69 ms |
118008 KB |
Output is correct |
52 |
Correct |
68 ms |
118008 KB |
Output is correct |
53 |
Correct |
70 ms |
118008 KB |
Output is correct |
54 |
Correct |
67 ms |
118012 KB |
Output is correct |
55 |
Correct |
67 ms |
118008 KB |
Output is correct |
56 |
Correct |
69 ms |
118008 KB |
Output is correct |
57 |
Correct |
67 ms |
118008 KB |
Output is correct |
58 |
Correct |
69 ms |
118008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
117752 KB |
Output is correct |
2 |
Correct |
68 ms |
117752 KB |
Output is correct |
3 |
Correct |
69 ms |
117752 KB |
Output is correct |
4 |
Correct |
67 ms |
117752 KB |
Output is correct |
5 |
Correct |
68 ms |
117752 KB |
Output is correct |
6 |
Correct |
70 ms |
117880 KB |
Output is correct |
7 |
Correct |
251 ms |
231368 KB |
Output is correct |
8 |
Correct |
234 ms |
231368 KB |
Output is correct |
9 |
Correct |
246 ms |
231364 KB |
Output is correct |
10 |
Correct |
241 ms |
231372 KB |
Output is correct |
11 |
Correct |
245 ms |
231372 KB |
Output is correct |
12 |
Correct |
243 ms |
231372 KB |
Output is correct |
13 |
Correct |
246 ms |
231368 KB |
Output is correct |
14 |
Correct |
249 ms |
231368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
65 ms |
117752 KB |
Output is correct |
2 |
Correct |
66 ms |
117752 KB |
Output is correct |
3 |
Correct |
65 ms |
117752 KB |
Output is correct |
4 |
Correct |
69 ms |
117856 KB |
Output is correct |
5 |
Correct |
70 ms |
117856 KB |
Output is correct |
6 |
Correct |
67 ms |
117752 KB |
Output is correct |
7 |
Correct |
69 ms |
117880 KB |
Output is correct |
8 |
Correct |
83 ms |
117764 KB |
Output is correct |
9 |
Correct |
72 ms |
117752 KB |
Output is correct |
10 |
Correct |
70 ms |
117856 KB |
Output is correct |
11 |
Correct |
72 ms |
117880 KB |
Output is correct |
12 |
Correct |
71 ms |
117752 KB |
Output is correct |
13 |
Correct |
68 ms |
117752 KB |
Output is correct |
14 |
Correct |
71 ms |
117880 KB |
Output is correct |
15 |
Correct |
69 ms |
117752 KB |
Output is correct |
16 |
Correct |
67 ms |
117752 KB |
Output is correct |
17 |
Correct |
68 ms |
117752 KB |
Output is correct |
18 |
Correct |
66 ms |
117752 KB |
Output is correct |
19 |
Correct |
68 ms |
117752 KB |
Output is correct |
20 |
Correct |
66 ms |
117752 KB |
Output is correct |
21 |
Correct |
68 ms |
117752 KB |
Output is correct |
22 |
Correct |
66 ms |
117752 KB |
Output is correct |
23 |
Correct |
67 ms |
117752 KB |
Output is correct |
24 |
Correct |
67 ms |
117752 KB |
Output is correct |
25 |
Correct |
68 ms |
117900 KB |
Output is correct |
26 |
Correct |
65 ms |
117752 KB |
Output is correct |
27 |
Correct |
68 ms |
117752 KB |
Output is correct |
28 |
Correct |
71 ms |
117752 KB |
Output is correct |
29 |
Correct |
69 ms |
117752 KB |
Output is correct |
30 |
Correct |
67 ms |
117748 KB |
Output is correct |
31 |
Correct |
65 ms |
117752 KB |
Output is correct |
32 |
Correct |
67 ms |
117752 KB |
Output is correct |
33 |
Correct |
67 ms |
117752 KB |
Output is correct |
34 |
Correct |
67 ms |
117752 KB |
Output is correct |
35 |
Correct |
69 ms |
118008 KB |
Output is correct |
36 |
Correct |
69 ms |
118008 KB |
Output is correct |
37 |
Correct |
69 ms |
118008 KB |
Output is correct |
38 |
Correct |
69 ms |
118136 KB |
Output is correct |
39 |
Correct |
70 ms |
118008 KB |
Output is correct |
40 |
Correct |
69 ms |
117968 KB |
Output is correct |
41 |
Correct |
69 ms |
118008 KB |
Output is correct |
42 |
Correct |
70 ms |
118008 KB |
Output is correct |
43 |
Correct |
68 ms |
118008 KB |
Output is correct |
44 |
Correct |
68 ms |
118008 KB |
Output is correct |
45 |
Correct |
67 ms |
118008 KB |
Output is correct |
46 |
Correct |
68 ms |
118008 KB |
Output is correct |
47 |
Correct |
69 ms |
118008 KB |
Output is correct |
48 |
Correct |
70 ms |
118012 KB |
Output is correct |
49 |
Correct |
69 ms |
118008 KB |
Output is correct |
50 |
Correct |
67 ms |
118008 KB |
Output is correct |
51 |
Correct |
69 ms |
118008 KB |
Output is correct |
52 |
Correct |
68 ms |
118008 KB |
Output is correct |
53 |
Correct |
70 ms |
118008 KB |
Output is correct |
54 |
Correct |
67 ms |
118012 KB |
Output is correct |
55 |
Correct |
67 ms |
118008 KB |
Output is correct |
56 |
Correct |
69 ms |
118008 KB |
Output is correct |
57 |
Correct |
67 ms |
118008 KB |
Output is correct |
58 |
Correct |
69 ms |
118008 KB |
Output is correct |
59 |
Correct |
70 ms |
117752 KB |
Output is correct |
60 |
Correct |
68 ms |
117752 KB |
Output is correct |
61 |
Correct |
69 ms |
117752 KB |
Output is correct |
62 |
Correct |
67 ms |
117752 KB |
Output is correct |
63 |
Correct |
68 ms |
117752 KB |
Output is correct |
64 |
Correct |
70 ms |
117880 KB |
Output is correct |
65 |
Correct |
251 ms |
231368 KB |
Output is correct |
66 |
Correct |
234 ms |
231368 KB |
Output is correct |
67 |
Correct |
246 ms |
231364 KB |
Output is correct |
68 |
Correct |
241 ms |
231372 KB |
Output is correct |
69 |
Correct |
245 ms |
231372 KB |
Output is correct |
70 |
Correct |
243 ms |
231372 KB |
Output is correct |
71 |
Correct |
246 ms |
231368 KB |
Output is correct |
72 |
Correct |
249 ms |
231368 KB |
Output is correct |
73 |
Correct |
249 ms |
231500 KB |
Output is correct |
74 |
Correct |
245 ms |
231456 KB |
Output is correct |
75 |
Correct |
244 ms |
231372 KB |
Output is correct |
76 |
Correct |
237 ms |
231368 KB |
Output is correct |
77 |
Correct |
245 ms |
231368 KB |
Output is correct |
78 |
Correct |
242 ms |
231372 KB |
Output is correct |
79 |
Correct |
238 ms |
231372 KB |
Output is correct |
80 |
Correct |
242 ms |
231368 KB |
Output is correct |
81 |
Correct |
241 ms |
231372 KB |
Output is correct |
82 |
Correct |
240 ms |
231368 KB |
Output is correct |
83 |
Correct |
283 ms |
231372 KB |
Output is correct |
84 |
Correct |
272 ms |
231364 KB |
Output is correct |
85 |
Correct |
272 ms |
231364 KB |
Output is correct |
86 |
Correct |
271 ms |
231364 KB |
Output is correct |
87 |
Correct |
267 ms |
231388 KB |
Output is correct |
88 |
Correct |
274 ms |
231364 KB |
Output is correct |
89 |
Correct |
272 ms |
231368 KB |
Output is correct |
90 |
Correct |
284 ms |
231544 KB |
Output is correct |
91 |
Correct |
296 ms |
231368 KB |
Output is correct |
92 |
Correct |
299 ms |
231364 KB |
Output is correct |
93 |
Correct |
309 ms |
231368 KB |
Output is correct |
94 |
Correct |
308 ms |
231240 KB |
Output is correct |
95 |
Correct |
299 ms |
231368 KB |
Output is correct |
96 |
Correct |
318 ms |
231368 KB |
Output is correct |
97 |
Correct |
304 ms |
231368 KB |
Output is correct |
98 |
Correct |
323 ms |
231368 KB |
Output is correct |
99 |
Correct |
303 ms |
231364 KB |
Output is correct |
100 |
Correct |
306 ms |
231368 KB |
Output is correct |
101 |
Correct |
305 ms |
231544 KB |
Output is correct |
102 |
Correct |
303 ms |
231372 KB |
Output is correct |