/**
____ ____ ____ ____ ____
||a |||t |||o |||d |||o ||
||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|
**/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
string convert (string path) {
vector <pair <bool, int>> blocks = {{1, 1}};
for (char c : path) {
if (c == '1') {
if (blocks.back().first == 0) {
blocks.back().second++;
} else {
blocks.push_back({0, 1});
}
} else if (c == '2') {
if (blocks.back().first == 1) {
blocks.back().second++;
} else {
blocks.push_back({1, 1});
}
} else if (c == 'U') {
if (--blocks.back().second == 0) {
blocks.pop_back();
}
} else if (c == 'L') {
int tail = 0;
if (blocks.back().first == 0) {
tail = blocks.back().second;
blocks.pop_back();
}
if (--blocks.back().second == 0) {
blocks.pop_back();
}
if (blocks.back().first == 0) {
blocks.back().second++;
} else {
blocks.push_back({0, 1});
}
if (tail != 0) {
blocks.push_back({1, tail});
}
} else if (c == 'R') {
int tail = 0;
if (blocks.back().first == 1) {
tail = blocks.back().second;
blocks.pop_back();
}
if (--blocks.back().second == 0) {
blocks.pop_back();
}
if (blocks.back().first == 1) {
blocks.back().second++;
} else {
blocks.push_back({1, 1});
}
if (tail != 0) {
blocks.push_back({0, tail});
}
}
}
string binary;
for (pair <bool, int> block : blocks) {
while (block.second--) {
binary += (block.first == 1 ? '1' : '0');
}
}
return binary;
}
int getInt (string binary) {
int ret = 0;
int pw = 1;
while (binary.empty() == false) {
if (binary.back() == '1') {
ret += pw;
}
binary.pop_back();
pw *= 2;
}
return ret;
}
int main () {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
string A, B;
cin >> A >> B;
A = convert(A);
B = convert(B);
int cost = 0;
while ((int) A.size() < (int) B.size()) {
cost++;
B.pop_back();
}
while ((int) B.size() < (int) A.size()) {
cost++;
A.pop_back();
}
while ((int) A.size() > 20) {
A.pop_back();
B.pop_back();
cost += 2;
}
if (A < B) {
swap(A, B);
}
int diff = getInt(A) - getInt(B);
int answer = diff + cost;
while (diff > 0) {
diff /= 2;
if (A.back() == '0' && B.back() == '1') {
diff++;
}
A.pop_back();
B.pop_back();
cost += 2;
answer = min(answer, diff + cost);
}
cout << answer << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
4 ms |
720 KB |
Output is correct |
3 |
Incorrect |
2 ms |
596 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Incorrect |
1 ms |
320 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
3 ms |
596 KB |
Output is correct |
3 |
Incorrect |
2 ms |
468 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
724 KB |
Output is correct |
2 |
Correct |
3 ms |
724 KB |
Output is correct |
3 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
724 KB |
Output is correct |
2 |
Correct |
3 ms |
852 KB |
Output is correct |
3 |
Incorrect |
1 ms |
324 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
724 KB |
Output is correct |
2 |
Correct |
4 ms |
724 KB |
Output is correct |
3 |
Incorrect |
2 ms |
596 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |