#include<iostream>
#include<cstring>
#define DIM 100005
using namespace std;
int i, sol, x, y, dist, t;
int ad[DIM];
char s[DIM], p[2][DIM];
int solve(int t){
int i, niv, n;
cin>> s + 1;
n = strlen(s + 1);
niv = 0;
for(i = 1; i <= n; i++){
if(s[i] == '1' || s[i] == '2'){
niv++;
ad[niv] = s[i] - '1';
}
else{
if(s[i] == 'U'){
ad[niv - 1] += ad[niv] / 2;
if(ad[niv] < 0 && ad[niv] % 2 != 1){
ad[niv - 1]--;
}
niv--;
}
else{
if(s[i] == 'R'){
ad[niv]++;
}
else{
ad[niv]--;
}
}
}
}
for(i = niv; i >= 1; i--){
ad[i - 1] += ad[i] / 2;
ad[i] %= 2;
if(ad[i] == -1){
ad[i] = 1;
ad[i - 1]--;
}
p[t][i] = ad[i] + '0';
}
return niv;
}
int main(){
x = solve(0);
y = solve(1);
sol = x + y;
for(i = 1; i <= min(x, y); i++){
if(p[0][i] != p[1][i]){
break;
}
}
sol -= 2 * (i - 1);
t = p[0][i] - '0';
if(i <= min(x, y) ){
sol--;
}
for(i = i + 1; i <= min(x, y); i++){
dist *= 2;
if(p[t][i] == '0'){
dist++;
}
if(p[1 - t][i] == '1'){
dist++;
}
sol = min(sol, dist + 1 + x - i + y - i);
if(dist > 200000){
break;
}
}
cout<< sol;
}
Compilation message
board.cpp: In function 'int solve(int)':
board.cpp:10:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
cin>> s + 1;
~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
384 KB |
Output is correct |
2 |
Incorrect |
6 ms |
384 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
384 KB |
Output is correct |
2 |
Correct |
15 ms |
640 KB |
Output is correct |
3 |
Incorrect |
9 ms |
512 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
6 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
384 KB |
Output is correct |
2 |
Incorrect |
14 ms |
640 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
640 KB |
Output is correct |
2 |
Incorrect |
14 ms |
896 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
716 KB |
Output is correct |
2 |
Correct |
14 ms |
896 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
14 ms |
896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
640 KB |
Output is correct |
2 |
Incorrect |
14 ms |
896 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |