BZOJ - 1537: [POI2005]Aut- The Bus

Portal

题解:把车站看成点,以$x$为第一关键字,$y$为第二关键字排序。记$dp_i$为纵坐标为$y$时的最大值,转移就行。

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 using namespace std;
 5 inline char nc() {
 6     static char b[1<<16],*s=b,*t=b;
 7     return s==t&&(t=(s=b)+fread(b,1,1<<16,stdin),s==t)?-1:*s++;
 8 }
 9 inline void read(int &x) {
10     char b = nc(); x = 0;
11     for (; !isdigit(b); b = nc());
12     for (; isdigit(b); b = nc()) x = x * 10 + b - '0';
13 }
14 struct P {
15     int x, y, z;
16     inline void init() {
17         read(x), read(y), read(z);
18     }
19     inline bool operator<(const P &p) const {
20         return x != p.x ? x < p.x : y < p.y;    
21     }
22 } p[100005];
23 int n, m, k, c[100005], H, ans, v[100005], dp[100005];
24 inline void gmax(int &x, int y) {
25     if (x < y) x = y;    
26 }
27 inline int lowbit(int x) {
28     return x & -x;    
29 }
30 inline void add(int p, int x) {
31     for (; p <= H; p += lowbit(p)) gmax(c[p], x);    
32 }
33 inline int mx(int p) {
34     int res = 0;
35     for (; p; p -= lowbit(p)) gmax(res, c[p]);
36     return res;
37 }
38 inline int h(int x) {
39     return lower_bound(v + 1, v + 1 + H, x) - v;    
40 }
41 int main() {
42     read(n); read(m); read(k);
43     for (int i = 1; i <= k; ++i) p[i].init(), v[i] = p[i].y;
44     sort(v + 1, v + 1 + k); H = unique(v + 1, v + 1 + k) - v - 1;
45     for (int i = 1; i <= k; ++i) p[i].y = h(p[i].y);
46     sort(p + 1, p + 1 + k);
47     for (int i = 1; i <= k; ++i) {
48         dp[i] = mx(p[i].y) + p[i].z;
49         gmax(ans, dp[i]);
50         add(p[i].y, dp[i]);
51     } printf("%d\n", ans);
52     return 0;    
53 }
View Code

 

posted @ 2018-01-06 10:36  p0ny  阅读(150)  评论(0编辑  收藏  举报