-
Notifications
You must be signed in to change notification settings - Fork 1
/
G.cpp
93 lines (88 loc) · 2.42 KB
/
G.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
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <cstring>
#include <string>
#include <sstream>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <algorithm>
#include <functional>
#include <iomanip>
using namespace std;
#define LL long long
const int maxn = 2000 + 100;
struct Node {
int l, r;
};
bool operator<(const Node &a, const Node &b) {
if(a.l == b.l) {
return a.r < b.r;
}
return a.l < b.l;
}
int T, n, m, k, x, rr;
Node node[maxn];
int dpr[maxn][maxn];
int dp[maxn][maxn];
int main() {
#ifdef LOCAL
freopen("test.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#endif // LOCAL
// ios::sync_with_stdio(false);
scanf("%d", &T);
int cas = 0;
while(T--) {
printf("Case #%d: ", ++cas);
scanf("%d%d%d", &n, &m, &k);
for(int i = 1; i <= m; ++i) {
scanf("%d%d", &node[i].l, &node[i].r);
}
for(int i = 0; i <= m; ++i) {
for(int j = 0; j <= k; ++j) {
dp[i][j] = dpr[i][j] = 0;
}
}
sort(node + 1, node + m + 1);
for(int i = 1; i <= m; ++i) {
for(int j = 1; j <= k; ++j) {
if(node[i].l <= dpr[i - 1][j - 1]) {
if(node[i].r <= dpr[i - 1][j - 1]) {
x = 0;
rr = dpr[i - 1][j - 1];
} else {
x = node[i].r - dpr[i - 1][j - 1];
rr = node[i].r;
}
} else {
x = node[i].r - node[i].l + 1;
rr = node[i].r;
}
if(dp[i - 1][j] > dp[i - 1][j - 1] + x) {
dp[i][j] = dp[i - 1][j];
dpr[i][j] = dpr[i - 1][j];
} else if(dp[i - 1][j] == dp[i - 1][j - 1] + x) {
dp[i][j] = dp[i - 1][j];
dpr[i][j] = min(dpr[i - 1][j], rr);
} else {
dp[i][j] = dp[i - 1][j - 1] + x;
dpr[i][j] = rr;
}
// printf("x = %d\n", x);
// printf("dp[%d][%d] = %d\n", i, j, dp[i][j]);
// printf("dpr[%d][%d] = %d\n", i, j, dpr[i][j]);
}
}
printf("%d\n", dp[m][k]);
}
return 0;
}