# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
376148 | daniel920712 | Lamps (JOI19_lamps) | C++14 | 612 ms | 262148 KiB |
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 <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <queue>
using namespace std;
char a[1000005];
char b[1000005];
bool have[1000005]={0};
int DP[1000005],N;
bool vis[1000005]={0};
queue < pair < int , int > > BFS;
int F(int here)
{
int i,tt;
if(here==N) return 0;
if(have[here]) return DP[here];
have[here]=1;
if(a[here]==b[here]) DP[here]=F(here+1);
else DP[here]=F(here+1)+1;
tt=0;
for(i=here;i<N;i++)
{
if(b[i]=='1'&&(i==here||b[i-1]=='0')) tt++;
DP[here]=min(DP[here],F(i+1)+tt+1);
}
tt=0;
for(i=here;i<N;i++)
{
if(b[i]=='0'&&(i==here||b[i-1]=='1')) tt++;
DP[here]=min(DP[here],F(i+1)+tt+1);
}
tt=0;
for(i=here;i<N;i++)
{
if(a[i]==b[i]&&(i==here||a[i-1]!=b[i-1])) tt++;
DP[here]=min(DP[here],F(i+1)+tt+1);
}
return DP[here];
}
int main()
{
int M,ans=0,i,j,x=0,y=0,aa,tt;
scanf("%d",&N);
scanf("%s %s",a,b);
x=y=0;
for(i=0;i<N;i++)
{
x*=2;
x+=a[i]-'0';
y*=2;
y+=b[i]-'0';
}
BFS.push(make_pair(x,0));
while(!BFS.empty())
{
x=BFS.front().first;
aa=BFS.front().second;
BFS.pop();
if(vis[x]) continue;
vis[x]=1;
//printf("%d %d %d\n",x,y,aa);
if(x==y)
{
printf("%d\n",aa);
return 0;
}
tt=0;
for(i=0;i<N;i++)
{
tt=x;
for(j=i;j<N;j++)
{
if(tt&(1<<j)) tt-=1<<j;
BFS.push(make_pair(tt,aa+1));
}
}
for(i=0;i<N;i++)
{
tt=x;
for(j=i;j<N;j++)
{
if(!(tt&(1<<j))) tt+=1<<j;
BFS.push(make_pair(tt,aa+1));
}
}
for(i=0;i<N;i++)
{
tt=x;
for(j=i;j<N;j++)
{
if(!(tt&(1<<j))) tt+=1<<j;
else tt-=1<<j;
BFS.push(make_pair(tt,aa+1));
}
}
}
//printf("%d\n",F(0));
return 0;
}
Compilation message (stderr)
# | 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... |