Submission #1253585

#TimeUsernameProblemLanguageResultExecution timeMemory
1253585WH8Event Hopping 2 (JOI21_event2)C++20
1 / 100
30 ms1092 KiB
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N, K;
    cin >> N >> K;
    vector<int> L(N), R(N);
    for (int i = 0; i < N; i++) {
        cin >> L[i] >> R[i];
    }

    vector<int> best; 
    bool found = false;

    // brute‐force all subsets via bitmask
    // (only works for N up to ~20)
    int maxMask = 1 << N;
    for (int mask = 0; mask < maxMask; mask++) {
        if (__builtin_popcount(mask) != K) 
            continue;

        // collect the chosen indices (1-based)
        vector<int> sel;
        sel.reserve(K);
        for (int i = 0; i < N; i++) {
            if (mask & (1 << i)) 
                sel.push_back(i + 1);
        }

        // check time‐feasibility: sort by start time, then ensure no overlap
        vector<pair<int,int>> times;
        times.reserve(K);
        for (int idx : sel)
            times.emplace_back(L[idx-1], R[idx-1]);
        sort(times.begin(), times.end());

        bool ok = true;
        for (int i = 1; i < K; i++) {
            // finish of previous must be <= start of next
            if (times[i-1].second > times[i].first) {
                ok = false;
                break;
            }
        }
        if (!ok) 
            continue;

        // if feasible, check lexicographic order of the index list
        if (!found || sel < best) {
            best = sel;
            found = true;
        }
    }

    if (!found) {
        cout << "-1\n";
    } else {
        // best is already in increasing order
        for (int x : best) 
            cout << x << "\n";
    }

    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...