2018ccpc网络赛1010 J - YJJ's Salesman

 又是一道没a的题。。。

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)(0,0) on the rectangle map and B (109,109)(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)(x,y) now (0≤x≤109,0≤y≤109)(0≤x≤109,0≤y≤109), he will only forward to (x+1,y)(x+1,y), (x,y+1)(x,y+1) or (x+1,y+1)(x+1,y+1). 
On the rectangle map from (0,0)(0,0) to (109,109)(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 kk on (xk,yk)(xk,yk) (1≤xk≤109,1≤yk≤109)(1≤xk≤109,1≤yk≤109), only the one who was from (xk−1,yk−1)(xk−1,yk−1) to (xk,yk)(xk,yk) will be able to earn vkvk 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.

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

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

题意很明显,简单题。。。结果还是没过,,自己真是个辣鸡

然后看数据写代码吧。。 代码1000+ms

1e9 离散化 然后x相等时 y从大到小更新就行了

1e5  nlogn  线段树或者树状数组   朴素的n2肯定t了。。

一直写线段树,,一直tle。。忘记了树状数组这货。。

还是树状数组的常数小呀。。

 

#include<cstring>
#include<cmath>
#include<string.h>
#include<iostream>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
#include<cstdio>
#define lowbit(x) (x&(-x))
int read() {
    int x = 0;
    char c = getchar();
    while (c < '0' || c > '9')c = getchar();
    while (c >= '0' && c <= '9') {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x;
}
int cnt; 

struct node{
	int x,y;
	int value;
};
using namespace std;
int dp[400005];
int y[100005];
node data[100005];

int n;
bool cmp(node a,node b)
{
	if(a.x<b.x)
	return 1;
	else if(a.x==b.x&&a.y<b.y)
	return 1;
	else return 0;
}

void updata(int u,int k)
{
    for(int i=u;i<=cnt;i+=lowbit(i)){
        dp[i]=max(dp[i],k);
    }
}

int query(int u)
{
    int res=0;
    for(int i=u;i>0;i-=lowbit(i)){res=max(res,dp[i]);}
    return res;
}

int main(){
	int T;
	T=read();
	while(T--){
		n=read();
		memset(dp,0,sizeof(dp));
		for(int i=0;i<n;i++){   
			data[i].x=read();	data[i].y=read();	data[i].value=read();			
		}		
		sort(data,data+n,cmp);    		
		
		for(int i=0;i<n;i++){        
			y[i]=data[i].y;								
		}
		sort(y,y+n);
		cnt=unique(y,y+n)-y; 

		for(int i=0;i<n;i++){	
			data[i].y=lower_bound(y,y+cnt,data[i].y)-y+1;
		}		
		
		int m=0;		
		for(int i=0;i<n;i++){     
			int j=i;
			int temp=data[i].x;
			while((data[i+1].x==temp)&&(i<n)){
				i++; 
			}					
			for(int z=i;z>=j;z--)
			{
				dp[data[z].y]=max(query(data[z].y-1)+data[z].value,dp[data[z].y]);
				updata(data[z].y,dp[data[z].y]);
				m=max(m,dp[data[z].y]);
			}	
		}
		printf("%d\n",m);
	}	
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值