YJJ's SalesmanTime Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1996 Accepted Submission(s): 744 Problem Description YJJ is a salesman who has traveled through western country. YJJ is always on journey. Either is he at the destination, or on the way to destination.
Input The first line of the input contains an integer T (1≤T≤10) ,which is the number of test cases.
Output The maximum of dollars YJJ can get.
Sample Input 1 3 1 1 1 1 2 2 3 3 1
Sample Output 3
Source
Recommend chendu |
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 200010;
const int inf = 0x3f3f3f3f;
int c[maxn], n, m, sot[maxn];
int dp[maxn], ans;
struct *** {
int x, y, v;
}spot[maxn];
bool cmp(*** a, *** b) {
if (a.x < b.x)
return 1;
else if (a.x == b.x&&a.y > b.y)
return 1;
return 0;
}
int lowbit(int x) {
return x&(-x);
}
int getsum(int x) {
int sum = 0;
while (x) {
sum = max(sum, c[x]);
x -= lowbit(x);
}
return sum;
}
void add(int x, int d) {
while (x<= 2 * n) {
c[x] = max(c[x], d);
ans = max(ans, c[x]);
x += lowbit(x);
}
}
void init() {
memset(c, 0, sizeof(c));
memset(dp, 0, sizeof(dp));
ans = -1;
}
int main() {
int te;
scanf("%d", &te);
while (~scanf("%d", &n)) {
init();
for (int s = 1; s <= n; s++) {
scanf("%d%d%d", &spot[s].x, &spot[s].y, &spot[s].v);
sot[2 * s - 1] = spot[s].x;
sot[2 * s - 2] = spot[s].y;
}
sort(sot, sot + 2 * n);
for (int s = 1; s <= n; s++) {
spot[s].x = lower_bound(sot, sot + 2 * n, spot[s].x) - sot + 1;
spot[s].y = lower_bound(sot, sot + 2 * n, spot[s].y) - sot + 1;
}
sort(spot + 1, spot + n + 1, cmp);
for (int s = 1; s <= n; s++) {
dp[s] = getsum(spot[s].y - 1) + spot[s].v;
add(spot[s].y, dp[s]);
}
printf("%d\n", ans);
}
}