#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[X] > depth[Y]) 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.
//X and Y are now at the same depth
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]);
}
}
//sequence of moves from root to both X and Y found.
// 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";
//sequence of moves up to LCA removed.
if(M[0].empty())
{
cout << ans << '\n';
return 0;
}
//if one is ancestor of other, return
if(M[X][0] != 0) swap(X, Y);
//now, X is on the left.
// 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();
//sequence of moves from LCA-children to X and Y.
// 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 << 1 + ans << '\n';
return 0;
}
//if X and Y are already on subtree sides, return 1 + ans
while(int(M[X].size()) - 5 > neq)
{
M[X].pop_back();
M[Y].pop_back();
ans += 2;
}
int newAns = 1'000'000;
while(int(M[X].size()) - 1 >= neq)
{
int v1 = 0;
int v2 = 1;
for(int f = neq; f < int(M[X].size()); f++)
{
v1 = 2*v1 + M[X][f];
v2 = 2*v2 + M[Y][f];
}
newAns = min(newAns, ans + (v2 - v1));
ans += 2;
M[X].pop_back();
M[Y].pop_back();
}
cout << newAns << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
1100 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
1228 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
972 KB |
Output is correct |
2 |
Incorrect |
2 ms |
972 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
1100 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1100 KB |
Output is correct |
2 |
Correct |
2 ms |
1100 KB |
Output is correct |
3 |
Correct |
2 ms |
1100 KB |
Output is correct |
4 |
Correct |
2 ms |
972 KB |
Output is correct |
5 |
Correct |
2 ms |
1100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
1100 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
1208 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1400 KB |
Output is correct |
2 |
Incorrect |
3 ms |
1464 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
1424 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
1472 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |