#include <bits/stdc++.h>
using namespace std;
vector<int> state[105];
int dp[1000005][17];
vector<int> t[2][2];
int cost[17][17];
int cnt;
string A;
string B;
int n;
int getval(vector<int> ord,int me)
{
for(int i:ord)
{
if(i==0)
me=0;
if(i==1)
me=1;
if(i==2)
me=1-me;
}
return me;
}
int mypoz[7];
int getcost(vector<int> a,vector<int> b)
{
swap(a,b);
int ans=a.size();
for(int i=0;i<3;i++)
mypoz[i]=-1;
for(int i=0;i<b.size();i++)
mypoz[b[i]]=i;
int lg=a.size();
for(int mask=0;mask<(1<<lg);mask++)
{
vector<int> sub;
for(int i=0;i<lg;i++)
if((mask>>i)&1)
sub.push_back(a[i]);
int cur=-1;
bool ok=1;
for(int i:sub)
{
if(mypoz[i]==-1||mypoz[i]<cur)
{
ok=0;
break;
}
cur=mypoz[i];
}
if(ok)
ans=min(ans,(int)a.size()-(int)sub.size());
}
return ans;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n;
cin>>A>>B;
A="0"+A;
B="0"+B;
for(int mask=0;mask<(1<<3);mask++)
{
vector<int> me;
for(int i=0;i<=2;i++)
if((mask>>i)&1)
me.push_back(i);
do
{
cnt++;
state[cnt]=me;
}while(next_permutation(me.begin(),me.end()));
}
for(int i=1;i<=cnt;i++)
for(int j=0;j<=1;j++)
{
int val=getval(state[i],j);
t[j][val].push_back(i);
}
for(int i=1;i<=cnt;i++)
for(int j=1;j<=cnt;j++)
{
cost[i][j]=getcost(state[i],state[j]);
}
for(int i=1;i<=16;i++)
dp[0][i]=2e6;
dp[0][1]=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=16;j++)
dp[i][j]=2e6;
for(int j:t[A[i]-'0'][B[i]-'0'])
for(int k:t[A[i-1]-'0'][B[i-1]-'0'])
dp[i][j]=min(dp[i][j],dp[i-1][k]+cost[k][j]);
}
int ans=2e6;
for(int i=1;i<=16;i++)
ans=min(ans,dp[n][i]);
cout<<ans;
return 0;
}
Compilation message
lamp.cpp: In function 'int getcost(std::vector<int>, std::vector<int>)':
lamp.cpp:32:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
32 | for(int i=0;i<b.size();i++)
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
464 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
496 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
464 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
496 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
604 KB |
Output is correct |
36 |
Correct |
1 ms |
604 KB |
Output is correct |
37 |
Correct |
1 ms |
604 KB |
Output is correct |
38 |
Correct |
1 ms |
604 KB |
Output is correct |
39 |
Correct |
1 ms |
604 KB |
Output is correct |
40 |
Correct |
1 ms |
604 KB |
Output is correct |
41 |
Correct |
1 ms |
604 KB |
Output is correct |
42 |
Correct |
1 ms |
604 KB |
Output is correct |
43 |
Correct |
1 ms |
604 KB |
Output is correct |
44 |
Correct |
1 ms |
604 KB |
Output is correct |
45 |
Correct |
1 ms |
604 KB |
Output is correct |
46 |
Correct |
1 ms |
604 KB |
Output is correct |
47 |
Correct |
1 ms |
600 KB |
Output is correct |
48 |
Correct |
1 ms |
600 KB |
Output is correct |
49 |
Correct |
1 ms |
604 KB |
Output is correct |
50 |
Correct |
1 ms |
856 KB |
Output is correct |
51 |
Correct |
1 ms |
856 KB |
Output is correct |
52 |
Correct |
1 ms |
604 KB |
Output is correct |
53 |
Correct |
1 ms |
604 KB |
Output is correct |
54 |
Correct |
1 ms |
604 KB |
Output is correct |
55 |
Correct |
1 ms |
604 KB |
Output is correct |
56 |
Correct |
1 ms |
604 KB |
Output is correct |
57 |
Correct |
1 ms |
604 KB |
Output is correct |
58 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
592 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
146 ms |
70896 KB |
Output is correct |
8 |
Correct |
135 ms |
70648 KB |
Output is correct |
9 |
Correct |
138 ms |
70864 KB |
Output is correct |
10 |
Correct |
132 ms |
70892 KB |
Output is correct |
11 |
Correct |
132 ms |
70876 KB |
Output is correct |
12 |
Correct |
132 ms |
70648 KB |
Output is correct |
13 |
Correct |
136 ms |
70896 KB |
Output is correct |
14 |
Correct |
135 ms |
70908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
464 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
496 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
604 KB |
Output is correct |
36 |
Correct |
1 ms |
604 KB |
Output is correct |
37 |
Correct |
1 ms |
604 KB |
Output is correct |
38 |
Correct |
1 ms |
604 KB |
Output is correct |
39 |
Correct |
1 ms |
604 KB |
Output is correct |
40 |
Correct |
1 ms |
604 KB |
Output is correct |
41 |
Correct |
1 ms |
604 KB |
Output is correct |
42 |
Correct |
1 ms |
604 KB |
Output is correct |
43 |
Correct |
1 ms |
604 KB |
Output is correct |
44 |
Correct |
1 ms |
604 KB |
Output is correct |
45 |
Correct |
1 ms |
604 KB |
Output is correct |
46 |
Correct |
1 ms |
604 KB |
Output is correct |
47 |
Correct |
1 ms |
600 KB |
Output is correct |
48 |
Correct |
1 ms |
600 KB |
Output is correct |
49 |
Correct |
1 ms |
604 KB |
Output is correct |
50 |
Correct |
1 ms |
856 KB |
Output is correct |
51 |
Correct |
1 ms |
856 KB |
Output is correct |
52 |
Correct |
1 ms |
604 KB |
Output is correct |
53 |
Correct |
1 ms |
604 KB |
Output is correct |
54 |
Correct |
1 ms |
604 KB |
Output is correct |
55 |
Correct |
1 ms |
604 KB |
Output is correct |
56 |
Correct |
1 ms |
604 KB |
Output is correct |
57 |
Correct |
1 ms |
604 KB |
Output is correct |
58 |
Correct |
1 ms |
604 KB |
Output is correct |
59 |
Correct |
1 ms |
592 KB |
Output is correct |
60 |
Correct |
1 ms |
348 KB |
Output is correct |
61 |
Correct |
1 ms |
344 KB |
Output is correct |
62 |
Correct |
0 ms |
348 KB |
Output is correct |
63 |
Correct |
0 ms |
348 KB |
Output is correct |
64 |
Correct |
1 ms |
348 KB |
Output is correct |
65 |
Correct |
146 ms |
70896 KB |
Output is correct |
66 |
Correct |
135 ms |
70648 KB |
Output is correct |
67 |
Correct |
138 ms |
70864 KB |
Output is correct |
68 |
Correct |
132 ms |
70892 KB |
Output is correct |
69 |
Correct |
132 ms |
70876 KB |
Output is correct |
70 |
Correct |
132 ms |
70648 KB |
Output is correct |
71 |
Correct |
136 ms |
70896 KB |
Output is correct |
72 |
Correct |
135 ms |
70908 KB |
Output is correct |
73 |
Correct |
134 ms |
70904 KB |
Output is correct |
74 |
Correct |
133 ms |
70888 KB |
Output is correct |
75 |
Correct |
132 ms |
70904 KB |
Output is correct |
76 |
Correct |
140 ms |
70908 KB |
Output is correct |
77 |
Correct |
135 ms |
70948 KB |
Output is correct |
78 |
Correct |
136 ms |
70900 KB |
Output is correct |
79 |
Correct |
133 ms |
70656 KB |
Output is correct |
80 |
Correct |
132 ms |
70888 KB |
Output is correct |
81 |
Correct |
133 ms |
70900 KB |
Output is correct |
82 |
Correct |
135 ms |
70904 KB |
Output is correct |
83 |
Correct |
137 ms |
70880 KB |
Output is correct |
84 |
Correct |
132 ms |
70900 KB |
Output is correct |
85 |
Correct |
134 ms |
70888 KB |
Output is correct |
86 |
Correct |
132 ms |
70904 KB |
Output is correct |
87 |
Correct |
138 ms |
70960 KB |
Output is correct |
88 |
Correct |
131 ms |
70880 KB |
Output is correct |
89 |
Correct |
132 ms |
70900 KB |
Output is correct |
90 |
Correct |
132 ms |
70896 KB |
Output is correct |
91 |
Correct |
133 ms |
70692 KB |
Output is correct |
92 |
Correct |
132 ms |
70900 KB |
Output is correct |
93 |
Correct |
134 ms |
70884 KB |
Output is correct |
94 |
Correct |
136 ms |
70720 KB |
Output is correct |
95 |
Correct |
132 ms |
70896 KB |
Output is correct |
96 |
Correct |
132 ms |
70800 KB |
Output is correct |
97 |
Correct |
133 ms |
71156 KB |
Output is correct |
98 |
Correct |
135 ms |
70656 KB |
Output is correct |
99 |
Correct |
132 ms |
70900 KB |
Output is correct |
100 |
Correct |
132 ms |
70904 KB |
Output is correct |
101 |
Correct |
138 ms |
70724 KB |
Output is correct |
102 |
Correct |
132 ms |
70900 KB |
Output is correct |