Submission #479913

# Submission time Handle Problem Language Result Execution time Memory
479913 2021-10-13T22:00:46 Z hidden1 Board (CEOI13_board) C++14
80 / 100
117 ms 716 KB
#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; \
    prllDebug(", "#__VA_ARGS__, __VA_ARGS__)
template <typename T>
void prllDebug(const char* name, T&& arg1) { cout << (name + 2) << " = " << arg1 << endl; }
template <typename T, typename... T2>
void prllDebug(const char* names, T&& arg1, T2&&... args) {
    const char* end = strchr(names + 1, ',');
    cout.write(names + 2, end - names - 2) << " = " << arg1 << endl;
    prllDebug(end, args...);
}

const ll MAX_N = 2e5 + 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(ll i = 0; i < WORD_COUNT; i ++) {
            nmb[i] = 0;
        }
    }

    void add(ll pos) {
        ll word = pos / WORD_SIZE;
        nmb[word] += 1ll << (WORD_SIZE - 1ll - (pos % WORD_SIZE));

        ll 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(ll pos) {
        ll word = pos / WORD_SIZE;
        nmb[word] -= 1ll << (WORD_SIZE - 1ll - (pos % WORD_SIZE));

        ll carry = 0;

        do {
            nmb[word] -= carry;
            if(nmb[word] < 0) {
                nmb[word] += (1ll << WORD_SIZE);
                carry = 1;
            }
            word --;
        } while(word >= 0 && carry);
    }

    void unset(ll pos) {
        ll word = pos / WORD_SIZE;
        nmb[word] &= ~(1ll << (WORD_SIZE - 1ll - (pos % WORD_SIZE)));
    }

    bool get(ll pos) {
        ll word = pos / WORD_SIZE;
        return nmb[word] & (1ll << (WORD_SIZE - 1ll - (pos % WORD_SIZE)));
    }

    void prll() {
        for(ll i = 0; i < MAX_N; i ++) {
            cout << get(i);
        }
        cout << endl;
    }
};

void swap(myBitset &a, myBitset &b) {
    for(ll 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
1 Correct 0 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 464 KB Output is correct
2 Correct 2 ms 332 KB Output is correct
3 Correct 5 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 460 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 332 KB Output is correct
2 Correct 7 ms 716 KB Output is correct
3 Correct 5 ms 460 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 356 KB Output is correct
2 Correct 7 ms 588 KB Output is correct
3 Incorrect 4 ms 556 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 46 ms 588 KB Output is correct
2 Correct 48 ms 588 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 50 ms 640 KB Output is correct
2 Correct 47 ms 588 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 117 ms 628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 55 ms 640 KB Output is correct
2 Correct 47 ms 588 KB Output is correct
3 Incorrect 26 ms 564 KB Output isn't correct
4 Halted 0 ms 0 KB -