luogu 1901 发射站

题目大意:

一个数列,它左边第一个比它高的人和右边第一个比它高的人要加上它的权值

思路:

单调栈维护一个单调递减的栈

正反各维护一遍

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cmath>
 5 #include<cstring>
 6 #include<cstdlib>
 7 #include<set>
 8 #include<map>
 9 #include<vector>
10 #include<stack>
11 #include<queue>
12 #define ll long long
13 #define inf 2147383611
14 #define MAXN 1001001
15 using namespace std;
16 inline ll read()
17 {
18     ll x=0,f=1;
19     char ch;ch=getchar();
20     while(!isdigit(ch)) {if(ch=='-') f=-1;ch=getchar();}
21     while(isdigit(ch)) {x=x*10+ch-'0';ch=getchar();}
22     return x*f;
23 }
24 int n,a[MAXN],v[MAXN],st[MAXN],top,ans[MAXN],maxn;
25 int main()
26 {
27     n=read();
28     for(int i=1;i<=n;i++) a[i]=read(),v[i]=read();
29     for(int i=1;i<=n;i++)
30     {
31         while(top&&a[st[top]]<=a[i]) top--;
32         ans[st[top]]+=v[i];
33         st[++top]=i;
34     }
35     top=0;
36     for(int i=n;i>=1;i--)
37     {
38         while(top&&a[st[top]]<=a[i]) top--;
39         ans[st[top]]+=v[i];
40         st[++top]=i;
41     }
42     for(int i=1;i<=n;i++) maxn=max(maxn,ans[i]);
43     printf("%d",maxn);
44 }
View Code
原文地址:https://www.cnblogs.com/yyc-jack-0920/p/7718558.html