This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4")
#define endl "\n"
typedef long long ll;
template<class T, class T2> inline ostream &operator <<(ostream &out, const pair<T, T2> &x) { out << x.first << " " << x.second; return out;}
template<class T, class T2> inline istream &operator >>(istream &in, pair<T, T2> &x) { in >> x.first >> x.second; return in;}
template<class T, class T2> inline bool chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; }
template<class T, class T2> inline bool chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; }
const ll mod = 1e9 + 7;
#define debug(...) cout << "Line: " << __LINE__ << endl; \
printDebug(", "#__VA_ARGS__, __VA_ARGS__)
template <typename T>
void printDebug(const char* name, T&& arg1) { cout << (name + 2) << " = " << arg1 << endl; }
template <typename T, typename... T2>
void printDebug(const char* names, T&& arg1, T2&&... args) {
const char* end = strchr(names + 1, ',');
cout.write(names + 2, end - names - 2) << " = " << arg1 << endl;
printDebug(end, args...);
}
const ll MAX_N = 1e5 + 10;;
//const ll MAX_N = 60;
const ll WORD_SIZE = 60;
const ll WORD_COUNT = MAX_N / WORD_SIZE + 1;
struct myBitset {
ll nmb[WORD_COUNT];
myBitset() {
for(int i = 0; i < WORD_COUNT; i ++) {
nmb[i] = 0;
}
}
void add(int pos) {
ll word = pos / WORD_SIZE;
nmb[word] += 1ll << (WORD_SIZE - 1ll - (pos % WORD_SIZE));
int carry = 0;
do {
nmb[word] += carry;
if(nmb[word] >= (1ll << WORD_SIZE)) {
nmb[word] -= (1ll << WORD_SIZE);
carry = 1;
}
word --;
} while(word >= 0 && carry);
}
void sub(int pos) {
ll word = pos / WORD_SIZE;
nmb[word] -= 1ll << (WORD_SIZE - 1ll - (pos % WORD_SIZE));
int carry = 0;
do {
nmb[word] -= carry;
if(nmb[word] < 0) {
nmb[word] += (1ll << WORD_SIZE);
carry = 1;
}
word --;
} while(word >= 0 && carry);
}
void unset(int pos) {
ll word = pos / WORD_SIZE;
nmb[word] &= ~(1ll << (WORD_SIZE - 1ll - (pos % WORD_SIZE)));
}
bool get(int pos) {
ll word = pos / WORD_SIZE;
return nmb[word] & (1ll << (WORD_SIZE - 1ll - (pos % WORD_SIZE)));
}
void print() {
for(int i = 0; i < MAX_N; i ++) {
cout << get(i);
}
cout << endl;
}
};
void swap(myBitset &a, myBitset &b) {
for(int i = 0; i < WORD_COUNT; i ++) {
swap(a.nmb[i], b.nmb[i]);
}
}
myBitset sum[2];
signed main() {
// ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
string a, b;
cin >> a >> b;
sum[0].add(0);
sum[1].add(0);
ll levela = 0;
for(auto it : a) {
if(it == '1') {
levela ++;
} else if(it == '2') {
levela ++;
sum[0].add(levela);
} else if(it == 'L') {
sum[0].sub(levela);
} else if(it == 'R') {
sum[0].add(levela);
} else if(it == 'U') {
sum[0].unset(levela);
levela --;
}
}
ll levelb = 0;
for(auto it : b) {
if(it == '1') {
levelb ++;
} else if(it == '2') {
levelb ++;
sum[1].add(levelb);
} else if(it == 'L') {
sum[1].sub(levelb);
} else if(it == 'R') {
sum[1].add(levelb);
} else if(it == 'U') {
sum[1].unset(levelb);
levelb --;
}
}
for(ll i = 0; i < MAX_N; i ++) {
if(sum[0].get(i) != sum[1].get(i)) {
if(sum[0].get(i)) {
swap(sum[0], sum[1]);
}
break;
}
}
ll dp = 0;
ll best = 4 * MAX_N;
for(ll i = 0; i <= min(levela, levelb); i ++) {
dp = (dp * 2ll);
if(sum[0].get(i) != sum[1].get(i)) {
if(sum[0].get(i)) {
dp --;
} else {
dp ++;
}
}
if(dp > 2 * best) {
break;
}
chkmin(best, dp + levela + levelb - 2 * i);
}
cout << best << endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |