This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define F first
#define S second
#define endl '\n'
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define maxp 22
#define EPS (ld)(1e-18)
#define mod (int)(1e9+7)
#define N (int)(1e6+1)
string a,b;
int n;
int dp[N][6];
bool v[N][6];
/// operation 0 -> no operation
/// operation 1 -> set 1
/// operation 2 -> set 0
/// operation 3 -> toggled
/// operation 4 -> set 1 and toggled
/// operation 5 -> set 0 and toggled
int solve(int x, int state){
if(x==0)
return 0;
if(v[x][state])
return dp[x][state];
v[x][state]=1;
int ans=mod;
if(state==0){
if(a[x-1]==b[x-1])
ans=min(ans,solve(x-1,0));
else
ans=min(ans,solve(x-1,3)+1);
if(b[x-1]=='1'){
ans=min(ans,solve(x-1,1)+1);
ans=min(ans,solve(x-1,5)+2);
}
else{
ans=min(ans,solve(x-1,2)+1);
ans=min(ans,solve(x-1,4)+2);
}
}
else if(state==1){
if(b[x-1]=='1'){
ans=min(ans,solve(x-1,0));
ans=min(ans,solve(x-1,1));
}
else
ans=min(ans,solve(x-1,4)+1);
}
else if(state==2){
if(b[x-1]=='1')
ans=min(ans,solve(x-1,5)+1);
else{
ans=min(ans,solve(x-1,0));
ans=min(ans,solve(x-1,2));
}
}
else if(state==3){
if(a[x-1]!=b[x-1]){
ans=min(ans,solve(x-1,0));
ans=min(ans,solve(x-1,3));
}
}
else if(state==4){
if(b[x-1]=='0'){
ans=min(ans,solve(x-1,4));
ans=min(ans,solve(x-1,0));
ans=min(ans,solve(x-1,1));
}
}
else{
if(b[x-1]=='1'){
ans=min(ans,solve(x-1,5));
ans=min(ans,solve(x-1,0));
ans=min(ans,solve(x-1,2));
}
}
dp[x][state]=ans;
return ans;
}
int main (){
fastio;
cin>>n;
cin>>a>>b;
int i,j;
for(i=1;i<=n;i++)
for(j=0;j<6;j++)
dp[i][j]=-1;
cout<<solve(n,0)<<endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |