Submission #217431

#TimeUsernameProblemLanguageResultExecution timeMemory
217431pit4hLamps (JOI19_lamps)C++14
100 / 100
323 ms4556 KiB
#include<bits/stdc++.h> using namespace std; int dp[2][4][4][4], dp1[2][4], dp2[2][4][4]; int main() { ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); int n; string s, t; cin>>n>>s>>t; for(int i=0; i<=1; ++i) { for(int a=0; a<=3; ++a) { for(int b=0; b<=3; ++b) { for(int c=0; c<=3; ++c) { dp[i][a][b][c] = n; } } } } dp[0][0][0][0]=0; for(int a=1; a<=3; ++a) { dp1[0][a]=n; for(int b=1; b<=3; ++b) { dp2[0][a][b]=n; } } for(int i=1; i<=n; ++i) { int v1 = n; for(int a=0; a<=3; ++a) { for(int b=0; b<=3; ++b) { for(int c=0; c<=3; ++c) { dp[i%2][a][b][c]=n; v1 = min(v1, dp[(i+1)%2][a][b][c]); } } } if(s[i-1]==t[i-1]) { dp[i%2][0][0][0] = v1; } for(int a=1; a<=3; ++a) { if((a==1 && t[i-1]=='0') || (a==2 && t[i-1]=='1') || (a==3 && s[i-1]!=t[i-1])) { dp[i%2][a][0][0] = min(v1+1, dp1[(i+1)%2][a]); } for(int b=1; b<=3; ++b) { if(b==a) continue; if((a==1 && t[i-1]=='0') || (a==2 && t[i-1]=='1') || (a==3 && ((b==1 && t[i-1]=='1') || (b==2 && t[i-1]=='0')))) { dp[i%2][a][b][0] = min(v1+2, min(dp1[(i+1)%2][a], dp1[(i+1)%2][b])+1); dp[i%2][a][b][0] = min(dp[i%2][a][b][0], dp2[(i+1)%2][a][b]); } else { continue; } for(int c=1; c<=3; ++c) { if(c==a || c==b) continue; dp[i%2][a][b][c] = min(v1+3, min(dp1[(i+1)%2][a], min(dp1[(i+1)%2][b], dp1[(i+1)%2][c]))+2); dp[i%2][a][b][c] = min(dp[i%2][a][b][c], min(dp2[(i+1)%2][a][b], min(dp2[(i+1)%2][a][c], dp2[(i+1)%2][b][c]))+1); dp[i%2][a][b][c] = min(dp[i%2][a][b][c], dp[(i+1)%2][a][b][c]); } } } for(int a=1; a<=3; ++a) { dp1[i%2][a]=n; for(int j=0; j<=3; ++j) { for(int k=0; k<=3; ++k) { dp1[i%2][a] = min(dp1[i%2][a], dp[i%2][a][j][k]); dp1[i%2][a] = min(dp1[i%2][a], dp[i%2][j][a][k]); dp1[i%2][a] = min(dp1[i%2][a], dp[i%2][j][k][a]); } } for(int b=1; b<=3; ++b) { dp2[i%2][a][b]=n; for(int j=0; j<=3; ++j) { dp2[i%2][a][b] = min(dp2[i%2][a][b], dp[i%2][a][b][j]); dp2[i%2][a][b] = min(dp2[i%2][a][b], dp[i%2][j][a][b]); } } } } int ans = n; for(int a=0; a<=3; ++a) { for(int b=0; b<=3; ++b) { for(int c=0; c<=3; ++c) { ans = min(ans, dp[n%2][a][b][c]); } } } cout<<ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...