2018 ccpc网络选拔赛 YJJ's Salesman 树状数组转移dp

YJJ's Salesman

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1082    Accepted Submission(s): 369


 

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.
One day, he is going to travel from city A to southeastern city B. Let us assume that A is (0,0) on the rectangle map and B (109,109). YJJ is so busy so he never turn back or go twice the same way, he will only move to east, south or southeast, which means, if YJJ is at (x,y) now (0≤x≤109,0≤y≤109), he will only forward to (x+1,y), (x,y+1) or (x+1,y+1).
On the rectangle map from (0,0) to (109,109), there are several villages scattering on the map. Villagers will do business deals with salesmen from northwestern, but not northern or western. In mathematical language, this means when there is a village k on (xk,yk) (1≤xk≤109,1≤yk≤109), only the one who was from (xk−1,yk−1) to (xk,yk) will be able to earn vk dollars.(YJJ may get different number of dollars from different village.)
YJJ has no time to plan the path, can you help him to find maximum of dollars YJJ can get.

 

 

Input

The first line of the input contains an integer T (1≤T≤10),which is the number of test cases.

In each case, the first line of the input contains an integer N (1≤N≤105).The following N lines, the k-th line contains 3 integers, xk,yk,vk (0≤vk≤103), which indicate that there is a village on (xk,yk) and he can get vk dollars in that village.
The positions of each village is distinct.

 

 

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

2018中国大学生程序设计竞赛 - 网络选拔赛

 

题意: 在一个1e9*1e9的图中,给定一些点的权值,从(0,0)走到(1e9,1e9),只能向上,向右或向右上走,问能取得最大

价值是多少

比赛时瞬间觉得是dp,在全队d都菜的情况下,我们放弃了开这道题

之后看题解,原来还能这样

dp的思路是(i,j)是求(0,0)到(i-1,j-1)矩形中的最大值

把x从小到大排序,所以转换为一维,然后参考背包的思路,y从大到小扫(记得离散化)

所以就是每个点维护一个最大值

然后可以用线段树或树状数组,时间复杂度都是O(nlogn)

选择了树状数组(当然是因为短)

代码:

#include <bits/stdc++.h>

typedef long long ll;
const int maxn = 1e5 + 10;
using namespace std;
struct node {
    int x, y, val;
} s[maxn];
int a[maxn];
int ma[maxn];
int ans = 0;

int ask(int x) {
    int ret = 0;
    while (x > 0) {
        ret = max(ret, ma[x]);
        x -= x & (-x);
    }
    return ret;
}

void add(int x, int val) {
    while (x < maxn) {
        ma[x] = max(val, ma[x]);
        ans = max(ans, ma[x]);
        x += x & (-x);
    }
}

bool cmp(node a, node b) {
    if (a.x == b.x) return a.y > b.y;
    return a.x < b.x;
}

int main() {
    int t, n;
    scanf("%d", &t);
    while (t--) {
        memset(ma, 0, sizeof(ma));
        memset(a, 0, sizeof(a));
        memset(s, 0, sizeof(s));
        ans=0;
        scanf("%d", &n);
        for (int i = 1; i <= n; i++) {
            scanf("%d%d%d", &s[i].x, &s[i].y, &s[i].val);
            a[i] = s[i].y;
        }
        sort(a + 1, a + n + 1);
        int cnt = unique((a + 1), (a + n + 1)) - (a + 1);
        for (int i = 1; i <= n; i++) {
            s[i].y = lower_bound(a + 1, a + cnt + 1, s[i].y) - (a + 1) + 1;
        }
        sort(s + 1, s + n + 1, cmp);
        int v;
        for (int i = 1; i <= n; i++) {
            v = ask(s[i].y - 1) + s[i].val;
            add(s[i].y, v);
        }
        printf("%d\n", ans);
    }
    return 0;
}

 

 

 

 

posted @ 2018-08-26 20:34  任小喵  阅读(291)  评论(0编辑  收藏  举报