[洛谷P1901]发射站

原题传送门

这道题跟[NOIP2012]开车旅行的预处理完全一样。通过链表来实现。

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 #define re register
 6 #define rep(i, a, b) for (re int i = a; i <= b; ++i)
 7 #define repd(i, a, b) for (re int i = a; i >= b; --i)
 8 #define maxx(a, b) a = max(a, b);
 9 #define minn(a, b) a = min(a, b);
10 #define LL long long
11 #define inf (1 << 30)
12 
13 const int maxn = 1e6 + 5;
14 
15 inline int read() {
16     int w = 0, f = 1; char c = getchar();
17     while (!isdigit(c)) f = c == '-' ? -1 : f, c = getchar();
18     while (isdigit(c)) w = (w << 3) + (w << 1) + (c ^ '0'), c = getchar();
19     return w * f;
20 }
21 
22 struct Tower {
23     int H, n;
24 } a[maxn];
25 bool cmp(Tower a, Tower b) {
26     return a.H < b.H;
27 }
28 
29 int n, L[maxn], R[maxn], V[maxn], ans[maxn];
30 
31 int main() {
32     n = read();
33 
34     rep(i, 1, n) a[i].n = i, L[i] = i-1, R[i] = i+1, a[i].H = read(), V[i] = read();
35 
36     sort(a + 1, a + n + 1, cmp);
37 
38     rep(i, 1, n) {
39         ans[L[a[i].n]] += V[a[i].n];
40         ans[R[a[i].n]] += V[a[i].n];
41         R[L[a[i].n]] = R[a[i].n];
42         L[R[a[i].n]] = L[a[i].n];
43     }
44     int res = 0;
45     rep(i, 1, n) maxx(res, ans[i]);
46     printf("%d", res);
47 
48     return 0;
49 }

 这道题还有更加简单的方法,就是维护一个栈。写法比上面的要短。这里就不放了。

posted @ 2019-01-29 10:45  AC-Evil  阅读(158)  评论(0编辑  收藏  举报