Created
May 15, 2018 14:28
-
-
Save henrybear327/8d543b3e1e1f6738832bc0890fc96950 to your computer and use it in GitHub Desktop.
Interval Independent
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using namespace std; | |
struct Interval { | |
int s, t, w; | |
}; | |
void solve() | |
{ | |
int n; | |
scanf("%d", &n); | |
Interval inp[n]; | |
for (int i = 0; i < n; i++) | |
scanf("%d %d %d", &inp[i].s, &inp[i].t, &inp[i].w); | |
int dp[n]; | |
sort(inp, inp + n, | |
[](const Interval &a, const Interval &b) -> bool { return a.t < b.t; }); | |
dp[0] = inp[0].w; | |
for (int i = 1; i < n; i++) { | |
dp[i] = max(dp[i - 1], inp[i].w); | |
int left = -1, right = i; | |
while (right - left > 1) { | |
int mid = left + (right - left) / 2; | |
if (inp[mid].t <= inp[i].s) | |
left = mid; | |
else | |
right = mid; | |
} | |
// left | |
if (left != -1) | |
dp[i] = max(dp[i], dp[left] + inp[i].w); | |
} | |
printf("%d\n", dp[n - 1]); | |
} | |
int main() | |
{ | |
int ncase; | |
scanf("%d", &ncase); | |
while (ncase--) | |
solve(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment