forked from Diusrex/UVA-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10020 Minimal coverage.cpp
79 lines (64 loc) · 1.81 KB
/
10020 Minimal coverage.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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Pair
{
int start, end;
bool operator<(const Pair & other) const
{
return start < other.start;
}
};
Pair allPairs[100005];
Pair solution[100005];
int main()
{
string sep = "";
int T, M;
cin >> T;
while (T--)
{
cout << sep;
sep = "\n";
cin >> M;
int numPairs = 0;
while (cin >> allPairs[numPairs].start >> allPairs[numPairs].end,
allPairs[numPairs].start || allPairs[numPairs].end)
{
// If not, ignore it
if (allPairs[numPairs].end > 0 && allPairs[numPairs].start < M)
++numPairs;
}
sort(allPairs, allPairs + numPairs);
// Ensure this will never be selected
allPairs[numPairs].start = M + 1;
int count = 0;
int currentX = 0;
int currentPair = 0;
// Pick the best end out of all of the possible pairs
while (currentX < M && currentPair < numPairs)
{
solution[count].end = 0;
for (; allPairs[currentPair].start <= currentX; ++currentPair)
if (allPairs[currentPair].end > solution[count].end)
solution[count] = allPairs[currentPair];
if (solution[count].end == currentX)
{
break;
}
currentX = solution[count].end;
++count;
}
if (currentX >= M)
{
cout << count << '\n';
for (int i = 0; i < count; ++i)
{
cout << solution[i].start << ' ' << solution[i].end << '\n';
}
}
else
cout << "0\n";
}
}