1. 程式人生 > >P3431 [POI2005]AUT-The Bus

P3431 [POI2005]AUT-The Bus

code ner which 2.7 cee -a play rar vertica

P3431 [POI2005]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 111 to nnn , starting from the westernmost. The WE-streets are numbered from 111 to mmm , beginning with the southernmost. Each intersection of the iii ‘th NS-street with the jjj ‘th WE-street is denoted by a pair of numbers (i,j)(i,j)(i,j) (for 1≤i≤n1\le i\le n1in , 1≤j≤m1\le j\le m1jm ).

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 nnn , mmm and kkk - 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≤1091\le n\le 10^91n109 , 1≤m≤1091\le m\le 10^91m109 , 1≤k≤1051\le k\le 10^51k105 ).

The following kkk 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 xix_ixi? , yiy_iyi? and pip_ipi? , separated by single spaces, 1≤xi≤n1\le x_i\le n1xi?n , 1≤yi≤m1\le y_i\le m1yi?m , 1≤pi≤1061\le p_i\le 10^61pi?106 . A triplet of this form signifies that by the intersection (xi,yi)pi(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 0001\ 000\ 000\ 0001 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

————————————————————————————————————————————————————————————————————————

日常DP

code

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>
#define R register

const int MAXN = 100010;

int n, m, k;
int b[MAXN], f[MAXN], ans, cnt;

struct POS{
    int x, y, num;
    bool operator < (const POS &t) const {
        if (x == t.x) return y < t.y;
        return x < t.x;
    }
}p[MAXN];

inline int read() {
    int num = 0, f = 1; char ch = getchar();
    while (!isdigit(ch)) {if (ch == -) f = -1; ch = getchar();}
    while (isdigit(ch)) {num = num * 10 + ch - 0; ch = getchar();}
    return num * f;
}

int t[MAXN];
int Lowbit(int x) {return x&(-x);}
void Update(int x, int k) {while(x<=cnt)t[x]=std::max(t[x],k),x+=Lowbit(x);}
int Query(int x) {int d=0;while(x>0)d=std::max(d,t[x]),x-=Lowbit(x);return d;}

int main() {
    n = read(); m = read(); k = read();
    for (R int i = 1; i <= k; ++ i) {
        p[i].x = read(), p[i].y = read(), p[i].num = read();
        b[i] = p[i].y;
    }
    std::sort(b + 1, b + k + 1);
    cnt = std::unique(b + 1, b + k + 1) - (b + 1);
    for (R int i = 1; i <= k; ++ i) 
        p[i].y = std::lower_bound(b + 1, b + cnt + 1, p[i].y) - b;
    std::sort(p + 1, p + k + 1);
    for (int i = 1; i <= k; ++ i) {
        f[i] = Query(p[i].y) + p[i].num;
        ans = std::max(f[i], ans);
        Update(p[i].y, f[i]);
    }
    std::cout << ans << std::endl;
}

P3431 [POI2005]AUT-The Bus