-
Notifications
You must be signed in to change notification settings - Fork 0
/
intervalcover.cpp
81 lines (60 loc) · 1.39 KB
/
intervalcover.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <bits/stdc++.h>
using namespace std;
double A, B;
int N;
tuple<double, double, int> intervals[20'007];
int next_idx[20'007];
int memo[20'007];
int dp(const int i) {
if (~memo[i]) return memo[i];
const auto [from, to, _] = intervals[i];
if (B <= to) return 1;
int res = 30'000;
for (int j = i + 1; j < N; j++) {
const auto [a, b, _] = intervals[j];
if (a <= from) continue;
if (to < a) continue;
if (b <= from) continue;
const int new_res = 1 + dp(j);
if (new_res <= res) {
res = new_res;
next_idx[i] = j;
}
}
return memo[i] = res;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
while (cin >> A >> B) {
memset(memo, -1, sizeof memo);
cin >> N;
for (int i = 0; i < N; i++) {
double a, b;
cin >> a >> b;
intervals[i] = {a, b, i};
}
int idx = 0;
int amount = INT_MAX;
sort(intervals, intervals + N);
for (int i = 0; i < N; i++) {
const auto [from, to, _] = intervals[i];
if (A < from) continue;
if (to < A) continue;
if (dp(i) < amount) {
idx = i;
amount = dp(i);
}
}
if (amount > 20'000) {
cout << "impossible" << endl;
continue;
}
cout << amount << endl;
for (int it = 0; it < amount; it++) {
cout << get<2>(intervals[idx]) << ' ';
idx = next_idx[idx];
}
cout << endl;
}
}