#include <bits/stdc++.h>
using namespace std;
#define MAXN 100005
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define f first
#define s second
int r = 0;
int res = INT_MAX;
pair<int, int> getnd(string str){
pair<int, int> nd = {0, 1};
for(char c : str){
if(c == '1'){
nd.f++;
nd.s = nd.s * 2 - 1;
}else if(c == '2'){
nd.f++;
nd.s *= 2;
}else if(c == 'U'){
nd.f--;
nd.s = (nd.s + 1) / 2;
}else if(c == 'L'){
nd.s--;
}else if(c == 'R'){
nd.s++;
}
// cout << " => " << nd.f << ", " << nd.s << endl;
}
return nd;
}
pair<int, int> moveup(pair<int, int> nd, int upsz){
FOR(i, 1, upsz){
nd.f--;
nd.s = (nd.s + 1) / 2;
}
return nd;
}
void go(pair<int, int> a, pair<int, int> b){
if(a.s > b.s) swap(a, b);
while(a.f != 0){
int ud = 2 + (b.s - a.s) / 2 + (a.s % 2 == 0 && b.s % 2 == 1);
res = min(res, r + (b.s - a.s));
a = moveup(a, 1), b = moveup(b, 1);
r += 2;
}
res = min(res, r);
}
int main(){
string sa, sb; cin >> sa >> sb;
pair<int, int> a = getnd(sa);
// cout << endl;
pair<int, int> b = getnd(sb);
if(a.f < b.f) swap(a, b);
r += a.f - b.f;
a = moveup(a, a.f - b.f);
go(a, b);
cout << res << endl;
}
Compilation message
board.cpp: In function 'void go(std::pair<int, int>, std::pair<int, int>)':
board.cpp:46:13: warning: unused variable 'ud' [-Wunused-variable]
46 | int ud = 2 + (b.s - a.s) / 2 + (a.s % 2 == 0 && b.s % 2 == 1);
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
856 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
4 ms |
704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
856 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
860 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
856 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |