树状数组优化dp——nkoj3771公共汽车
2016-09-20 00:08
225 查看
P3771公共汽车 | ||
|
问题描述
Byte City 的街道形成了一个标准的棋盘网络 – 他们要么是北南走向要么就是西东走向. 北南走向的路口从 1 到 n编号, 西东走向的路从1 到 m编号. 每个路口用两个数(i, j) 表示(1 <= i <= n, 1 <= j <= m).
Byte City里有一条公交线, 在某一些路口设置了公交站点.公交车从 (1, 1) 发车, 在(n, m)结束.公交车只能往北或往东走. 现在有一些乘客在某些站点等车. 公交车司机希望在路线中能接到尽量多的乘客.帮他想想怎么才能接到最多的乘客.
输入格式
第一行三个数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.
输出格式
一个数表示最多能接到的乘客数量.
样例输入
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
样例输出
11
来源
POI2005
#include<cstdio> #include<iostream> #include<algorithm> #include<cstdlib> #include<cstring> #include<map> using namespace std; inline void _read(int &x){ char t=getchar();bool sign=true; while(t<'0'||t>'9') {if(t=='-')sign=false;t=getchar();} for(x=0;t>='0'&&t<='9';t=getchar())x=x*10+t-'0'; if(!sign)x=-x; } const int maxn=500005; int totx,toty; map<int,int> mapx; map<int,int> mapy; int x_appear[maxn],y_appear[maxn]; int n,m,k; struct node{ int x,y,w; node(){} node(int a,int b,int c){x=a;y=b;w=c;} }; node stop[maxn]; int c[maxn]; int f[maxn]; int lowbit(int x){return x&(-x);} void modify(int x,int d){ int i,j,k; for(i=x;i<=maxn;i+=lowbit(i)){ c[i]=max(c[i],d); } } int getmax(int x){ int ans=0,i,j; for(i=x;i;i-=lowbit(i)){ ans=max(ans,c[i]); } return ans; } bool cmp(node a,node b){ if(a.x==b.x)return a.y<b.y; else return a.x<b.x; } int main(){ int i,j,ans=0; cin>>n>>m>>k; for(i=1;i<=k;i++){ int x,y; _read(stop[i].x);_read(stop[i].y);_read(stop[i].w); x=stop[i].x; y=stop[i].y; if(mapx[x]==0)x_appear[++totx]=x; if(mapy[y]==0)y_appear[++toty]=y; } sort(x_appear+1,x_appear+1+totx); sort(y_appear+1,y_appear+1+toty); for(i=1;i<=totx;i++){ mapx[x_appear[i]]=i; } for(i=1;i<=toty;i++){ mapy[y_appear[i]]=i; } sort(stop+1,stop+1+k,cmp); for(i=1;i<=k;i++){ int temp=getmax(mapy[stop[i].y]); f[i]=temp+stop[i].w; modify(mapy[stop[i].y],f[i]); ans=max(ans,f[i]); } cout<<ans; }
相关文章推荐
- zoj 3349 简单DP 线段树或树状数组优化
- 2015 CCPC C题 【树状数组优化dp】
- [DP] [1D1D优化] [树状数组] [最短路] 遭遇战 (clean)
- 【HDU4991】dp 树状数组优化
- hdu 3450 树状数组优化dp
- CodeForces 602E【概率DP】【树状数组优化】
- hdu 5542(树状数组优化dp)
- bzoj4361 isn(树状数组优化DP)
- 【Codeforces 597C】【DP 树状数组优化】Subsequences 【n个不同数,长度为k+1的LIS数】
- HDOJ 5406 CRB and Apple 树状数组优化DP
- Codeforces Round #333 (Div. 1) C. Kleofáš and the n-thlon 树状数组优化dp
- hdu5542 树状数组优化dp
- hdu 3450(树状数组优化dp)
- 【扩展KMP】【树状数组优化dp】字符串游戏
- hdoj 2227 Find the nondecreasing subsequences 【树状数组优化dp】
- Codeforces 597C Subsequences 【树状数组优化DP】
- UVA11775 Unique Story dp+二维树状数组优化
- hdu 4991(树状数组优化dp)
- hdu 2227(树状数组优化dp)
- Codeforces 629D Babaei and Birthday Cake(树状数组优化dp)