欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

2018 CCPC 网络赛 1010 YJJ's Salesman | DP+树状数组

程序员文章站 2022-03-13 19:29:05
...

题目:

YJJ's Salesman

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

 

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


题解:

2018 CCPC 网络赛 1010 YJJ's Salesman | DP+树状数组

 


代码:

感谢强大的队友提供的代码支持

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<set>
#include<map>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define oo cout<<"!!!"<<endl;
typedef long long ll;
typedef unsigned long long ull;
#define ms(s) memset(s, 0, sizeof(s))
const int inf = 1e9;
//head

const int maxn = 2e5+11;

struct node
{
    int x,y,id,val;
    bool operator < (const node& o)
    {
        if(x != o.x)return x < o.x;
        return y < o.y;
    }
}a[maxn];
int d[maxn+maxn],bit[maxn],dp[maxn];
int tot,cnt;
void add(int x,int v)
{
    while(x <= tot)
    {
        bit[x] = max(bit[x],v),x+=x&-x;
    }
}
int sum(int x)
{
    int res = -inf;
    while(x)
    {
        res = max(res,bit[x]);
        x-=x&-x;
    }
    return res;
}
int find(int x)
{
    return lower_bound(d+1,d+tot+1,x)-d-1;
}

int main() 
{
    int T;
    cin>>T;
    while(T--)
    {
        tot = cnt = 0;
        rep(i,0,maxn)bit[i] = dp[i] = -inf;
        d[++tot] = 0,d[++tot] = -1;
        int n;
        cin>>n;
        rep(i,1,n+1)
        {
            int x,y,v;
            scanf("%d%d%d",&x,&y,&v);
            a[++cnt] = node{x-1,y-1,i,v};
            a[++cnt] = node{x,y,i,0};
            d[++tot] = y-1;
            d[++tot] = y;
        }
        a[++cnt] = node{inf,inf,n+1,0};
        d[++tot] = inf;
        sort(d+1,d+tot+1);
        tot = unique(d+1,d+tot+1)-d-1;
        sort(a+1,a+cnt+1);
        add(find(0),0);
        rep(i,1,cnt+1)
        {
            dp[a[i].id] = max(dp[a[i].id],sum(find(a[i].y))+a[i].val);
            if(a[i].val==0)
                add(find(a[i].y),dp[a[i].id]);
        }
        printf("%d\n",dp[n+1]);
    }
   
}

 

相关标签: ACM

上一篇: 弹性盒

下一篇: 使用TimerTask的坑