2018CCPC網絡賽1010 HDU6447 YJJ's Salesman(線段樹 + DP + 離散化)

YJJ's Salesman

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

 

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

 

 

題目大意是說在一個1e9*1e9的地圖上有1e5個村莊,每個村莊有一定的利潤。每次只能從(x,y)走到(x+1,y)或(x,y+1)或(x+1,y+1),並且只有是用最後一種走法走到村莊的才能獲得該村莊的利益。

我的做法是先把村莊按照縱座標從小到大,橫座標從大到小排序一下(橫座標從小到大是類似01揹包),再把橫座標離散化一下這樣遍歷村莊的時候就能直接在一個一維的數組裏找最大利益並更新當前橫座標的利益,用線段樹或樹狀數組維護最大利益即可。

#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
typedef long long ll;
int maxx[4 * maxn];
int n;
int x[maxn];
struct fun
{
    int x,y,v;
}z[maxn];
int cntx;
void update(int l, int r, int rt, int p, int v)
{
    if (p <= l && r <= p)
    {
        maxx[rt] = max(maxx[rt], v);
        return;
    }
    int mid = (l + r) >> 1;
    if (p <= mid)
        update(l, mid, rt << 1, p, v);
    else
        update(mid + 1, r, (rt << 1) + 1, p, v);
    maxx[rt] = max(maxx[rt << 1], maxx[(rt << 1) + 1]);
}
int query(int l, int r, int rt, int l1, int r1)
{
    if (l > r || l1 > r1)
        return 0;
    if (l1 <= l && r <= r1)
        return maxx[rt];
    int mid = (l + r) >> 1;
    int ans1,ans2;
    ans1 = ans2 = 0;
    if (l1 <= mid)
        ans1 = query(l, mid, rt << 1, l1, r1);
    if (r1 > mid)
        ans2 = query(mid + 1, r, (rt << 1) + 1, l1, r1);
    return max(ans1, ans2);
}
bool cmp(fun a, fun b)
{
    if (a.y != b.y)
        return a.y < b.y;
    return a.x > b.x;
}
int main()
{
//    freopen("in.txt", "r", stdin);
    int t;
    scanf("%d", &t);
    while (t--)
    {
        memset(maxx, 0, sizeof(maxx));
        cntx = 1;
        scanf("%d", &n);
        for (int i = 0; i < n; i++)
        {
            scanf("%d%d%d", &z[i].x, &z[i].y, &z[i].v);
            x[i] = z[i].x;
        }
        sort(x, x + n);
        for (int i = 0; i < n; i++)
            z[i].x = lower_bound(x, x + n, z[i].x) - x + 1;
        sort(z, z + n, cmp);
        int ans = 0;
        int tmp;
        for (int i = 0; i < n; i++)
        {
            tmp = query(1, n, 1, 1, z[i].x - 1) + z[i].v;
            ans = max(ans, tmp);
            update(1, n, 1, z[i].x, tmp);
        }
        printf("%d\n", ans);
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章