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 <vector>
#include <string>
#include <deque>
using namespace std;
const int MX = 100'000; //FIX
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
vector<int> mov[2];
vector<int> depth(2, 0);
for(int q = 0; q < 2; q++)
{
string A;
cin >> A;
mov[q] = vector<int>(1+MX, 0);
for(char c: A)
{
if(c == '1') depth[q]++;
else if(c == '2')
{
depth[q]++;
mov[q][depth[q]]++;
}
else if(c == 'U') depth[q]--;
else if(c == 'L') mov[q][depth[q]]--;
else if(c == 'R') mov[q][depth[q]]++;
}
for(int d = MX; d >= 1; d--)
{
// cerr << d << ' ' << mov[q][d] << '\n';
mov[q][d-1] += mov[q][d]/2;
mov[q][d] -= 2*(mov[q][d]/2);
if(mov[q][d] == -1)
{
mov[q][d-1]--;
mov[q][d] += 2;
}
}
// for(int h = 1; h <= depth[q]; h++)
// {
// if(mov[q][h] == 0) cerr << "L";
// else cerr << "R";
// }
// cerr << "\n";
}
int X = 0, Y = 1;
if(depth[0] > depth[1]) swap(X, Y);
//depth[X] <= depth[Y];
int ans = 0;
ans += depth[Y] - depth[X];
depth[Y] = depth[X]; //Y := ancestor of Y at level of X.
deque<int> M[2];
for(int q = 0; q < 2; q++)
{
for(int v = 1; v <= depth[X]; v++)
{
M[q].push_back(mov[q][v]);
}
}
// for(int g = 0; g < int(M[0].size()); g++) cerr << M[0][g] << ' ' << M[1][g] << '\n';
// cerr << "\n\n";
while(!M[0].empty() && M[0][0] == M[1][0])
{
M[0].pop_front();
M[1].pop_front();
}
// for(int g = 0; g < int(M[0].size()); g++) cerr << M[0][g] << ' ' << M[1][g] << '\n';
// cerr << "\n\n";
if(M[0].empty())
{
cout << ans << '\n';
return 0;
}
if(M[X][0] != 0) swap(X, Y);
//X on left, Y on right
// for(int g = 0; g < int(M[0].size()); g++) cerr << M[X][g] << ' ' << M[Y][g] << '\n';
// cerr << "\n\n";
M[X].pop_front();
M[Y].pop_front();
// for(int g = 0; g < int(M[0].size()); g++) cerr << M[X][g] << ' ' << M[Y][g] << '\n';
// cerr << "\n\n";
// cerr << ans << '\n';
int neq = -1;
for(int i = 0; i < (int)M[X].size(); i++)
{
if(M[X][i] != 1 || M[Y][i] != 0)
{
neq = i;
break;
}
}
// cerr << neq << '\n';
// for(int g = 0; g < int(M[0].size()); g++) cerr << M[X][g] << ' ' << M[Y][g] << '\n';
// cerr << "\n\n";
if(neq == -1)
{
cout << ans << '\n';
return 0;
}
while(int(M[X].size()) - 1 > neq)
{
M[X].pop_back();
M[Y].pop_back();
ans += 2;
}
ans += 1 + (M[X].back() != 1) + (M[Y].back() != 0);
cout << ans << '\n';
}
# | 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... |