답안 #479917

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
479917 2021-10-13T22:06:03 Z hidden1 게임판 (CEOI13_board) C++14
100 / 100
103 ms 844 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;
            } else {
                carry = 0;
            }
            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;
            } else {
                carry = 0;
            }
            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;
}
# 결과 실행 시간 메모리 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
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 588 KB Output is correct
2 Correct 1 ms 392 KB Output is correct
3 Correct 5 ms 460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 332 KB Output is correct
2 Correct 7 ms 628 KB Output is correct
3 Correct 5 ms 460 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 0 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 0 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 368 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 0 ms 336 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 332 KB Output is correct
2 Correct 7 ms 588 KB Output is correct
3 Correct 4 ms 460 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 36 ms 644 KB Output is correct
2 Correct 34 ms 556 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 39 ms 588 KB Output is correct
2 Correct 34 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 103 ms 636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 36 ms 588 KB Output is correct
2 Correct 36 ms 588 KB Output is correct
3 Correct 19 ms 552 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 2 ms 332 KB Output is correct
6 Correct 76 ms 844 KB Output is correct