Submission #1210966

#TimeUsernameProblemLanguageResultExecution timeMemory
1210966madamadam3Port Facility (JOI17_port_facility)C++20
10 / 100
6093 ms652 KiB
#include <bits/stdc++.h>

using namespace std;

struct Event {
    int time, box_id;
    bool is_remove;

    const bool operator<(const Event &other) {
        return time < other.time;
    }
};

const int MAXN = 1'000'001;
const int MOD = 1'000'000'007;
int n;

int A[MAXN], B[MAXN];
vector<Event> events;

int dfs(int cur_time, vector<int> &left_stack, vector<int> &right_stack) {
    int our_box = events[cur_time].box_id;
    bool remove = events[cur_time].is_remove;

    if (remove && !(left_stack.back() == our_box || right_stack.back() == our_box)) {
        return 0;
    }

    int ways = 0;
    if (cur_time >= 2*n-1) {
        ways = remove ? 1: 0;
        return ways;
    }

    if (remove ) {
        bool is_left = false;
        if (left_stack.back() == our_box) {
            left_stack.pop_back();
            is_left = true;
        } else {
            right_stack.pop_back();
        }

        ways += dfs(cur_time + 1, left_stack, right_stack);
        (is_left ? left_stack : right_stack).push_back(our_box);
    } else {
        left_stack.push_back(our_box);
        ways += dfs(cur_time + 1, left_stack, right_stack);
        left_stack.pop_back();
        
        right_stack.push_back(our_box);
        ways += dfs(cur_time + 1, left_stack, right_stack);
        right_stack.pop_back();
    }

    return ways;
}

void solve() {
    vector<int> ls(1, -1), rs(1, -1);
    cout << dfs(0, ls, rs);
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> A[i] >> B[i];
    }

    for (int i = 0; i < n; i++) {
        events.push_back(Event {A[i], i, false});
        events.push_back(Event {B[i], i, true});
    }

    sort(events.begin(), events.end());

    solve();
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...