-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcorridorThereBack.cpp
47 lines (35 loc) · 1.09 KB
/
corridorThereBack.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
#include <iostream>
#include <vector>
#include <algorithm>
struct Trap {
int room;
int time;
};
int main() {
int t;
std::cin >> t;
while (t--) {
int n;
std::cin >> n;
std::vector<Trap> traps(n);
for (int i = 0; i < n; i++) {
std::cin >> traps[i].room >> traps[i].time;
}
std::sort(traps.begin(), traps.end(), [](const Trap& a, const Trap& b) {
return a.room < b.room;
});
int max_k = 1;
std::vector<int> dp(n + 1); // dp[i] stores the maximum room reached if i traps are activated
for (int i = 1; i <= n; i++) {
dp[i] = std::max(dp[i - 1], traps[i - 1].room - 1);
for (int j = i - 2; j >= 0; j--) {
if (traps[j].room + traps[j].time >= traps[i - 1].room) {
dp[i] = std::max(dp[i], dp[j] + traps[i - 1].room - traps[j].room);
}
}
max_k = std::max(max_k, dp[i] + 1);
}
std::cout << max_k << std::endl;
}
return 0;
}