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;
#define ff first
#define ss second
#define int long long
map<int,int>dist;
string a,b;
signed main(){
cin>>a;
int x=1;
int y=1;
int lvl=1;
dist[1]=1;
for(auto c:a){
if(c=='1'){
x*=2;
lvl++;
}
else if(c=='2'){
x=x*2+1;
lvl++;
}
else if(c=='U'){
x=x/2;
lvl--;
}
else if(c=='L'){
x--;
}
else{
x++;
}
dist[x]=lvl;
}
lvl=1;
cin>>a;
for(auto c:a){
if(c=='1'){
y*=2;
lvl++;
}
else if(c=='2'){
y=y*2+1;
lvl++;
}
else if(c=='U'){
y=y/2;
lvl--;
}
else if(c=='L'){
y--;
}
else{
y++;
}
dist[y]=lvl;
}
if(dist[y]>dist[x]){
swap(y,x);
}
int k=0;
int p=dist[x];
while(dist[x]>dist[y]){
x=x/2;
dist[x]= --p;
k++;
}
int ans=k+abs(x-y);
p=dist[x];
while(p>1){
x=x/2;
y=y/2;
p--;
k+=2;
ans=min(ans,k+abs(x-y));
}
cout<<ans;
}
# | 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... |
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |