Luogu3431 [POI2005]AUT-The Bus

原题链接:https://www.luogu.com.cn/problem/P3431

AUT-The Bus

题目描述

The streets of Byte City form a regular, chessboardlike network - they are either north-south or west-east directed. We shall call them NS- and WE-streets. Furthermore, each street crosses the whole city. Every NS-street intersects every WE- one and vice versa. The NS-streets are numbered from 1 1 1 to n n n, starting from the westernmost. The WE-streets are numbered from 1 1 1 to m m m, beginning with the southernmost. Each intersection of the ii’th NS-street with the jj’th WE-street is denoted by a pair of numbers ( i , j ) (i,j) (i,j) (for 1 ≤ i ≤ n 1\le i\le n 1in, 1 ≤ j ≤ m 1\le j\le m 1jm).

There is a bus line in Byte City, with intersections serving as bus stops. The bus begins its itinerary by the ( 1 , 1 ) (1,1) (1,1) intersection, and finishes by the ( n , m ) (n,m) (n,m) intersection. Moreover, the bus may only travel in the eastern and/or northern direction.

There are passengers awaiting the bus by some of the intersections. The bus driver wants to choose his route in a way that allows him to take as many of them as possible. (We shall make an assumption that the interior of the bus is spacious enough to take all of the awaiting passengers, regardless of the route chosen.)TaskWrite a programme which:

reads from the standard input a description of the road network and the number of passengers waiting at each intersection,finds, how many passengers the bus can take at the most,writes the outcome to the standard output.

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

输入格式

The first line of the standard input contains three positive integers nn, mm and kk - denoting the number of NS-streets, the number of WE-streets and the number of intersections by which the passengers await the bus, respectively ( 1 ≤ n ≤ 1 0 9 (1\le n\le 10^9 (1n109, 1 ≤ m ≤ 1 0 9 1\le m\le 10^9 1m109 , 1 ≤ k ≤ 1 0 5 1\le k\le 10^5 1k105 ).

The following k k k lines describe the deployment of passengers awaiting the bus, a single line per intersection. In the ( i + 1 ) (i+1) (i+1)'st line there are three positive integers x i x_i xi , y i y_i yi and p i p_i pi , separated by single spaces, 1 ≤ x i ≤ n 1\le x_i\le n 1xin, 1 ≤ y i ≤ m 1\le y_i\le m 1yim, 1 ≤ p i ≤ 1 0 6 1\le p_i\le 10^6 1pi106 . A triplet of this form signifies that by the intersection ( x i , y i ) p i (x_i,y_i)p_i (xi,yi)pi passengers await the bus. Each intersection is described in the input data once at the most. The total number of passengers waiting for the bus does not exceed 1   000   000   000 1\ 000\ 000\ 000 1 000 000 000.

输出格式

Your programme should write to the standard output one line containing a single integer - the greatest number of passengers the bus can take.

输入输出样例

输入 #1
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
输出 #1
11

题解

比较简单的一道题,因为车只能朝北或者朝西开,那么我们就从每个点东侧和南侧的点转移就好了,如果数据范围是 1 0 3 10^3 103,直接开二维数组循环即可。这道题范围比较大,就需要离散化东西走向的坐标,同时用线段树维护区间最大值。从南到北遍历所有的站点时,取当前位置的值和东侧所有位置的最大值比较进行转移。

代码

因为线段树空间忘开 4 4 4倍痛失一A。

#include<bits/stdc++.h>
#define ls v<<1
#define rs v<<1|1
using namespace std;
const int M=1e5+5;
struct Point{
    
    int x,y,val;}pt[M];
struct node{
    
    int le,ri,mx;}tree[M<<1];
bool cmp(Point a,Point b){
    
    return a.y==b.y?a.x<b.x:a.y<b.y;}
int n,m,k,x[M];
void up(int v){
    
    tree[v].mx=max(tree[ls].mx,tree[rs].mx);}
void build(int v,int le,int ri)
{
    
    
    tree[v].le=x[le],tree[v].ri=x[ri];
    if(le==ri)return;
    int mid=le+ri>>1;
    build(ls,le,mid),build(rs,mid+1,ri);
}
void modify(int v,int x,int val)
{
    
    
    if(tree[v].le==tree[v].ri){
    
    tree[v].mx=val;return;}
    if(x<=tree[ls].ri)modify(ls,x,val);
    else modify(rs,x,val);
    up(v);
}
int ask(int v,int le,int ri)
{
    
    
    if(le>ri)return 0;
    if(le<=tree[v].le&&tree[v].ri<=ri){
    
    return tree[v].mx;}
    int r=0;
    if(le<=tree[ls].ri)r=ask(ls,le,ri);
    if(tree[rs].le<=ri)r=max(r,ask(rs,le,ri));
    return r;
}
void in()
{
    
    
    scanf("%d%d%d",&n,&m,&k);
    for(int i=1;i<=k;++i)
    {
    
    
        scanf("%d%d%d",&pt[i].x,&pt[i].y,&pt[i].val);
        x[i]=pt[i].x;
    }
}
void ac()
{
    
    
    sort(x+1,x+1+k);
    n=unique(x+1,x+1+k)-1-x;
    build(1,1,n);
    sort(pt+1,pt+1+k,cmp);
    for(int i=1;i<=k;++i)
    {
    
    
        modify(1,pt[i].x,pt[i].val+max(ask(1,pt[i].x,pt[i].x),ask(1,x[1],pt[i].x-1)));
    }
    printf("%d\n",ask(1,1,x[n]));
}
int main()
{
    
    
    in(),ac();
    system("pause");
}

猜你喜欢

转载自blog.csdn.net/ShadyPi/article/details/113696467
今日推荐