#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define ll long long int
#define vi vector<int>
#define vii vector< vector<int> >
#define PI 3.1415926535897932384626433832795
#define INF 9223372036854775807LL
//15:20
bool node1[200005];
bool node2[200005];
bool dist[200005];
bool temp[200005];
ll cn1[200005];
ll cn2[200005];
int lev1,lev2;
int levdist;
void calcdist() {
levdist = 0;
bool rem = false;
for(int i = lev1; i >= 0; i--) {
bool nod1 = node1[i];
bool nod2 = node2[i];
if(rem) {
rem = false;
if(nod2 && !nod1) {
temp[i] = false;
} else if(!nod2 && !nod1) {
temp[i] = true;
rem = true;
} else if(nod1 && nod2) {
temp[i] = true;
rem = true;
} else if(nod1 && !nod2) {
temp[i] = false;
rem = true;
}
} else {
if(nod2 && !nod1) {
temp[i] = true;
} else if(nod1 == nod2) {
temp[i] = false;
} else {
temp[i] = true;
rem = true;
}
}
}
int ind = -1;
for(int i = 0; i <= lev1; i++) {
if(temp[i] && ind == -1) {
ind = 0;
}
if(ind != -1) {
dist[ind++] = temp[i];
}
}
levdist = ind-1;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
string a;
string b;
cin >> a >> b;
//a = transf(a);
//b = transf(b);
//cout << a << " " << b << endl;
node1[0] = node2[0] = 1;
lev1 = lev2 = 0;
for(int i = 0; i < a.length(); i++) {
if(a.at(i) == '1') {
lev1++;
} else if(a.at(i) == '2') {
lev1++;
node1[lev1] = true;
} else if(a.at(i) == 'L') {
cn1[lev1]--;
} else if(a.at(i) == 'R') {
cn1[lev1]++;
} else {
if(cn1[lev1] > 0) {
ll add = cn1[lev1]/2;
if(node1[lev1]) {
if(cn1[lev1]%2 == 1) {
add++;
}
}
cn1[lev1] = 0;
cn1[lev1-1]+= add;
} else if(cn1[lev1] < 0) {
ll add = (-cn1[lev1])/2;
if(!node1[lev1]) {
if((-cn1[lev1])%2 == 1) {
add++;
}
}
cn1[lev1] = 0;
cn1[lev1-1]-= add;
}
node1[lev1] = false;
lev1--;
}
}
for(int i = 0; i < b.length(); i++) {
if(b.at(i) == '1') {
lev2++;
} else if(b.at(i) == '2') {
lev2++;
node2[lev2] = true;
} else if(b.at(i) == 'L') {
cn2[lev2]--;
} else if(b.at(i) == 'R') {
cn2[lev2]++;
} else {
if(cn2[lev2] > 0) {
ll add = cn2[lev2]/2;
if(node2[lev2]) {
if(cn2[lev2]%2 == 1) {
add++;
}
}
cn2[lev2] = 0;
cn2[lev2-1]+= add;
} else if(cn2[lev2] < 0) {
ll add = (-cn2[lev2])/2;
if(!node2[lev2]) {
if((-cn2[lev2])%2 == 1) {
add++;
}
}
cn2[lev2] = 0;
cn2[lev2-1]-= add;
}
node2[lev2] = false;
lev2--;
}
}
ll rem = 0;
for(int i = 100010; i >= 0; i--) {
if(cn1[i] > 0) {
rem+= cn1[i];
}
if(rem%2 && node1[i]) {
node1[i] = false;
rem/=2;
rem++;
} else if(rem%2 && !node1[i]) {
node1[i] = true;
rem/=2;
} else {
rem/=2;
}
//}
}
rem = 0;
for(int i = 100010; i >= 0; i--) {
if(cn1[i] < 0) {
rem-= cn1[i];
}
if(rem%2 && node1[i]) {
rem--;
node1[i] = false;
} else if(rem%2 && !node1[i]) {
rem++;
node1[i] = true;
}
rem/=2;
//}
}
rem = 0;
for(int i = 100010; i >= 0; i--) {
if(cn2[i] > 0) {
rem+= cn2[i];
}
if(rem%2 && node2[i]) {
node2[i] = false;
rem/=2;
rem++;
} else if(rem%2 && !node2[i]) {
node2[i] = true;
rem/=2;
} else {
rem/=2;
}
//}
}
rem = 0;
for(int i = 100010; i >= 0; i--) {
if(cn2[i] < 0) {
rem-= cn2[i];
}
if(rem%2 && node2[i]) {
rem--;
node2[i] = false;
} else if(rem%2 && !node2[i]) {
rem++;
node2[i] = true;
}
rem/=2;
//}
}
if(lev1 > lev2) {
swap(lev1,lev2);
swap(node1,node2);
} else if(lev1 == lev2) {
bool bad = false;
bool eq = true;
for(int i = 0; i <= lev1; i++) {
if(node1[i] != node2[i]) {
eq = false;
if(node1[i]) {
bad = true;
}
break;
}
}
if(eq) {
cout << 0;
return 0;
}
if(bad) {
swap(node1,node2);
}
}
int wasdif = lev2-lev1;
for(int i = 0; i < lev2-lev1; i++) {
lev2--;
}
ll ans = INF;
ll nod1 = 0;
ll nod2 = 0;
int waslev = lev1;
calcdist();
for(int goesupmore = 0; goesupmore < waslev; goesupmore++) {
if(goesupmore%100 == 0) {
calcdist();
}
if(lev1 <= 60) {
if(nod1 == 0) {
for(int i = 0; i <= lev1; i++) {
nod1*=2;
if(node1[i]) {
nod1++;
}
nod2*=2;
if(node2[i]) {
nod2++;
}
}
}
//cout << nod1 << " " << nod2 << endl;
ans = min(ans,wasdif+goesupmore*2+abs(nod1-nod2));
nod1/=2;
nod2/=2;
} else {
if(levdist <= 60) {
calcdist();
ll dis = 0;
for(int i = 0; i <= levdist; i++) {
dis*=2;
if(dist[i]) {
dis++;
}
}
ans = min(ans,wasdif+goesupmore*2+dis);
if(dis == 0) {
break;
}
lev1--;
lev2--;
} else {
lev1--;
lev2--;
levdist--;
}
}
}
ans = min(ans,wasdif+waslev*2+abs(nod1-nod2));
calcdist();
ll dis = 0;
for(int i = 0; i <= levdist; i++) {
dis*=2;
if(dist[i]) {
dis++;
}
}
ans = min(ans,wasdif+waslev*2+dis);
//cout << node2 << endl;
cout << ans;
return 0;
}
Compilation message
board.cpp: In function 'int main()':
board.cpp:75:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < a.length(); i++) {
~~^~~~~~~~~~~~
board.cpp:110:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < b.length(); i++) {
~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
4 ms |
488 KB |
Output is correct |
3 |
Correct |
5 ms |
544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
868 KB |
Output is correct |
2 |
Correct |
5 ms |
876 KB |
Output is correct |
3 |
Correct |
7 ms |
1156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
1156 KB |
Output is correct |
2 |
Correct |
4 ms |
1156 KB |
Output is correct |
3 |
Correct |
3 ms |
1156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
1156 KB |
Output is correct |
2 |
Correct |
6 ms |
1156 KB |
Output is correct |
3 |
Correct |
6 ms |
1156 KB |
Output is correct |
4 |
Correct |
5 ms |
1156 KB |
Output is correct |
5 |
Correct |
4 ms |
1156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1156 KB |
Output is correct |
2 |
Correct |
5 ms |
1156 KB |
Output is correct |
3 |
Correct |
5 ms |
1156 KB |
Output is correct |
4 |
Correct |
6 ms |
1156 KB |
Output is correct |
5 |
Correct |
4 ms |
1156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1156 KB |
Output is correct |
2 |
Correct |
4 ms |
1156 KB |
Output is correct |
3 |
Correct |
5 ms |
1156 KB |
Output is correct |
4 |
Correct |
5 ms |
1156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1156 KB |
Output is correct |
2 |
Correct |
10 ms |
1260 KB |
Output is correct |
3 |
Correct |
5 ms |
1260 KB |
Output is correct |
4 |
Correct |
5 ms |
1260 KB |
Output is correct |
5 |
Correct |
4 ms |
1260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
1900 KB |
Output is correct |
2 |
Correct |
38 ms |
2028 KB |
Output is correct |
3 |
Incorrect |
6 ms |
2028 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
2160 KB |
Output is correct |
2 |
Correct |
35 ms |
2160 KB |
Output is correct |
3 |
Correct |
36 ms |
2160 KB |
Output is correct |
4 |
Correct |
62 ms |
2160 KB |
Output is correct |
5 |
Correct |
5 ms |
2160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
2160 KB |
Output is correct |
2 |
Correct |
45 ms |
2160 KB |
Output is correct |
3 |
Correct |
6 ms |
2160 KB |
Output is correct |
4 |
Correct |
172 ms |
2160 KB |
Output is correct |
5 |
Correct |
189 ms |
2160 KB |
Output is correct |
6 |
Execution timed out |
1076 ms |
2160 KB |
Time limit exceeded |