-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuva10020.cpp
More file actions
58 lines (49 loc) · 1.09 KB
/
Copy pathuva10020.cpp
File metadata and controls
58 lines (49 loc) · 1.09 KB
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
/*
* Contest : UVA
* Problem : 10020 - Minimal Coverage
* Link : https://vjudge.net/problem/UVA-10020
*/
#include <bits/stdc++.h>
using namespace std;
#define fastio ios::sync_with_stdio(0); cin.tie(0);
void solve() {
int m; cin >> m;
vector<pair<int, int>> seg;
int l, r;
while (cin >> l >> r, l or r)
if (r > 0 && l < m)
seg.push_back({l, r});
sort(seg.begin(), seg.end());
vector<pair<int, int>> chosen;
int left = 0, i = 0, n = seg.size();
bool flag = true;
while (left < m) {
// entre todos com l <= left, pega o de maior R
int right = left, idx = -1;
while (i < n && seg[i].first <= left) {
if (seg[i].second > right) {
right = seg[i].second;
idx = i;
}
i++;
}
if (idx == -1) {
flag = false;
break;
}
chosen.push_back(seg[idx]);
left = right;
}
if (!flag) cout << 0 << '\n';
else {
cout << chosen.size() << '\n';
for (auto &[l, r] : chosen) cout << l << ' ' << r << '\n';
}
cout << '\n';
}
int main() {
fastio
int tc; cin >> tc;
while (tc--) solve();
return 0;
}