#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
auto START=chrono::steady_clock().now();
int mytime()
{
auto END=chrono::steady_clock().now();
int x=chrono::duration_cast<chrono::milliseconds> (END-START).count();
return x;
}
int a,b;
string s;
int val[100005],lg;
char va[100005],vb[100005];
int lga,lgb;
void multiply()
{
int last=lg%2;
if(last==1)
val[lg++]=1;
else
val[lg-1]++;
}
void divide()
{
int last=lg%2;
if(val[lg-1]==1)
lg--;
else
val[lg-1]--;
}
void add()
{
int last=lg%2;
if(last==0)
{
if(val[lg-1]>1)
{
val[lg-1]--;
val[lg++]=1;
}
else
{
lg--;
val[lg-1]++;
}
return;
}
else
{
int nr1=val[lg-1];
int nr0=val[lg-2];
lg-=2;
if(nr0==1)
{
val[lg-1]++;
val[lg++]=nr1;
}
else
{
nr0--;
val[lg++]=nr0;
val[lg++]=1;
val[lg++]=nr1;
}
}
}
void subtract()
{
int last=lg%2;
if(last==1)
{
if(val[lg-1]==1)
{
lg--;
val[lg-1]++;
}
else
{
val[lg-1]--;
val[lg++]=1;
}
return;
}
else
{
int nr0=val[lg-1];
int nr1=val[lg-2];
lg-=2;
if(nr1==1)
{
val[lg-1]++;
val[lg++]=nr0;
}
else
{
nr1--;
val[lg++]=nr1;
val[lg++]=1;
val[lg++]=nr0;
}
}
}
int getniv(int x)
{
for(int d=0;d<=62;d++)
{
int st=(1<<d);
int dr=st*2-1;
if(x>=st&&x<=dr)
return d;
}
return -1;
}
vector<int> dif;
void getdif()
{
dif.clear();
int t=0;
for(int i=lga-1;i>=0;i--)
{
int x=va[i]-t-vb[i];
t=0;
if(x<0)
{
x+=2;
t=1;
}
dif.push_back(x);
}
while(!dif.empty()&&dif.back()==0)
dif.pop_back();
if(dif.empty())
dif.push_back(0);
reverse(dif.begin(),dif.end());
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>s;
lg=1;
val[0]=1;
for(int i=0;i<s.size();i++)
{
char c=s[i];
if(c=='1')
multiply();
if(c=='2')
{
multiply();
add();
}
if(c=='U')
divide();
if(c=='L')
subtract();
if(c=='R')
add();
}
for(int i=0;i<lg;i++)
{
if(i%2==0)
{
for(int j=1;j<=val[i];j++)
va[lga++]=1;
}
else
{
for(int j=1;j<=val[i];j++)
va[lga++]=0;
}
}
cin>>s;
lg=1;
val[0]=1;
for(int i=0;i<s.size();i++)
{
char c=s[i];
if(c=='1')
multiply();
if(c=='2')
{
multiply();
add();
}
if(c=='U')
divide();
if(c=='L')
subtract();
if(c=='R')
add();
}
for(int i=0;i<lg;i++)
{
if(i%2==0)
{
for(int j=1;j<=val[i];j++)
vb[lgb++]=1;
}
else
{
for(int j=1;j<=val[i];j++)
vb[lgb++]=0;
}
}
int rez=0;
int ans=1e9;
if(lga<lgb)
{
swap(va,vb);
swap(lga,lgb);
}
while(lga>lgb)
{
rez++;
lga--;
}
bool need=0;
for(int i=0;i<lga;i++)
if(va[i]!=vb[i])
{
if(va[i]<vb[i])
need=1;
break;
}
if(need)
swap(va,vb);
/*for(int i:va)
cout<<i;
cout<<'\n';
for(int i:vb)
cout<<i;
cout<<'\n';*/
getdif();
while(dif.size()>2)
{
dif.pop_back();
lga--;
lgb--;
rez+=2;
}
while(dif.size())
{
/*if(mytime()>=199)
{
cout<<ans;
return 0;
}*/
getdif();
int x=0;
for(int i:dif)
x=x*2+i;
ans=min(ans,x+rez);
if(x==0)
break;
rez+=2;
lga--;
lgb--;
}
cout<<ans;
return 0;
}
Compilation message
board.cpp: In function 'void divide()':
board.cpp:26:9: warning: unused variable 'last' [-Wunused-variable]
26 | int last=lg%2;
| ^~~~
board.cpp: In function 'int main()':
board.cpp:144:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
144 | for(int i=0;i<s.size();i++)
| ~^~~~~~~~~
board.cpp:177:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
177 | for(int i=0;i<s.size();i++)
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
0 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
4 ms |
724 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
468 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
3 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1044 KB |
Output is correct |
2 |
Correct |
2 ms |
1044 KB |
Output is correct |
3 |
Correct |
93 ms |
620 KB |
Output is correct |
4 |
Correct |
68 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1044 KB |
Output is correct |
2 |
Correct |
2 ms |
1044 KB |
Output is correct |
3 |
Correct |
41 ms |
468 KB |
Output is correct |
4 |
Correct |
51 ms |
408 KB |
Output is correct |
5 |
Correct |
2 ms |
1044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
916 KB |
Output is correct |
2 |
Correct |
3 ms |
980 KB |
Output is correct |
3 |
Correct |
2 ms |
980 KB |
Output is correct |
4 |
Correct |
164 ms |
484 KB |
Output is correct |
5 |
Correct |
165 ms |
640 KB |
Output is correct |
6 |
Execution timed out |
1089 ms |
1044 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |