【刷题】BZOJ 1537 [POI2005]Aut- The Bus

Description

Byte City 的街道形成了一个标准的棋盘网络 – 他们要么是北南走向要么就是西东走向. 北南走向的路口从 1 到 n编号, 西东走向的路从1 到 m编号. 每个路口用两个数(i, j) 表示(1 <= i <= n, 1 <= j <= m). Byte City里有一条公交线, 在某一些路口设置了公交站点. 公交车从 (1, 1) 发车, 在(n, m)结束.公交车只能往北或往东走. 现在有一些乘客在某些站点等车. 公交车司机希望在路线中能接到尽量多的乘客.帮他想想怎么才能接到最多的乘客.

Input

第一行三个数n, m 和 k – 表示北南走向的路的个数以及西东走向的路和乘客等车的站点的个数. ( 1 <= n <= 10^9, 1 <= m <= 10^9, 1 <= k <= 10^5). 接下来k 行每行描述一个公交站的信息.第 i + 1 行三个正整数 xi, yi 和 pi, 1 <= xi <= n, 1 <= yi <= m, 1 <= pi <= 10^6. 表示在(xi, yi) 有 pi 个乘客在等车. 每个路口在数据中最多出现一次,乘客总数不会超过1 000 000 000.

Output

一个数表示最多能接到的乘客数量.

Sample Input

8 7 11
4 3 4
6 2 4
2 3 2
5 6 1
2 5 2
1 5 5
2 1 1
3 1 1
7 7 1
7 4 2
8 6 2

Sample Output

11

Solution

先离散化,然后按照第一维排序,保证转移的时候第一维严格满足条件
第二位写个dp, \(f[i]\) 代表第二维为 \(i\) 时最优答案是多少
转移用个线段树维护就好了

#include<bits/stdc++.h>
#define ui unsigned int
#define ll long long
#define db double
#define ld long double
#define ull unsigned long long
const int MAXK=100000+10;
int n,m,k,f[MAXK];
std::vector<int> V;
std::map<int,int> M;
struct node{
	int x,y,w;
	inline bool operator < (const node &A) const {
		return x<A.x||(x==A.x&&y<A.y);
	};
};
node pot[MAXK];
template<typename T> inline void read(T &x)
{
	T data=0,w=1;
	char ch=0;
	while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
	if(ch=='-')w=-1,ch=getchar();
	while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar();
	x=data*w;
}
template<typename T> inline void write(T x,char ch='\0')
{
	if(x<0)putchar('-'),x=-x;
	if(x>9)write(x/10);
	putchar(x%10+'0');
	if(ch!='\0')putchar(ch);
}
template<typename T> inline void chkmin(T &x,T y){x=(y<x?y:x);}
template<typename T> inline void chkmax(T &x,T y){x=(y>x?y:x);}
template<typename T> inline T min(T x,T y){return x<y?x:y;}
template<typename T> inline T max(T x,T y){return x>y?x:y;}
#define Mid ((l+r)>>1)
#define ls rt<<1
#define rs rt<<1|1
#define lson ls,l,Mid
#define rson rs,Mid+1,r
struct Segment_Tree{
	int Mx[MAXK<<2];
	inline void PushUp(int rt)
	{
		Mx[rt]=max(Mx[ls],Mx[rs]);
	}
	inline void Update(int rt,int l,int r,int ps,int k)
	{
		if(l==r&&r==ps)chkmax(Mx[rt],k);
		else
		{
			if(ps<=Mid)Update(lson,ps,k);
			else Update(rson,ps,k);
			PushUp(rt);
		}
	}
	inline int Query(int rt,int l,int r,int L,int R)
	{
		if(L<=l&&r<=R)return Mx[rt];
		else
		{
			int res=0;
			if(L<=Mid)chkmax(res,Query(lson,L,R));
			if(R>Mid)chkmax(res,Query(rson,L,R));
			return res;
		}
	}
};
Segment_Tree T;
#undef Mid
#undef ls
#undef rs
#undef lson
#undef rson
inline void init()
{
	for(register int i=1;i<=k;++i)V.push_back(pot[i].x);
	std::sort(V.begin(),V.end());
	V.erase(std::unique(V.begin(),V.end()),V.end());
	for(register int i=0,lt=V.size();i<lt;++i)M[V[i]]=i+1;
	for(register int i=1;i<=k;++i)pot[i].x=M[pot[i].x];
	V.clear();M.clear();
	for(register int i=1;i<=k;++i)V.push_back(pot[i].y);
	std::sort(V.begin(),V.end());
	V.erase(std::unique(V.begin(),V.end()),V.end());
	for(register int i=0,lt=V.size();i<lt;++i)M[V[i]]=i+1;
	for(register int i=1;i<=k;++i)pot[i].y=M[pot[i].y];
}
int main()
{
	read(n);read(m);read(k);
	for(register int i=1;i<=k;++i)read(pot[i].x),read(pot[i].y),read(pot[i].w);
	std::sort(pot+1,pot+k+1);
	init();
	for(register int i=1;i<=k;++i)
	{
		chkmax(f[pot[i].y],T.Query(1,1,k,1,pot[i].y)+pot[i].w);
		T.Update(1,1,k,pot[i].y,f[pot[i].y]);
	}
	int ans=0;
	for(register int i=1;i<=k;++i)chkmax(ans,f[i]);
	write(ans,'\n');
	return 0;
}
posted @ 2018-08-04 09:16  HYJ_cnyali  阅读(197)  评论(0编辑  收藏  举报