P2659 美丽的序列

P2659 美丽的序列
对于当前的最小值,找到最大的左右边界,然后更新答案。用单调队列确定左右边界,O(n)做法。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<queue>
 4 #include<algorithm>
 5 #include<cmath>
 6 #include<ctime>
 7 #include<set>
 8 #include<map>
 9 #include<stack>
10 #include<cstring>
11 #define inf 2147483647
12 #define For(i,a,b) for(register long long i=a;i<=b;i++)
13 #define p(a) putchar(a)
14 #define g() getchar()
15 //by war
16 //2017.11.2
17 using namespace std;
18 long long ans;
19 long long n;
20 struct node
21 {
22     long long l,r;
23 }aa[2000010];
24 long long q[2000010];
25 long long l,r;
26 long long x;
27 long long a[2000010];
28 void in(long long &x)
29 {
30     long long y=1;
31     char c=g();x=0;
32     while(c<'0'||c>'9')
33     {
34     if(c=='-')
35     y=-1;
36     c=g();
37     }
38     while(c<='9'&&c>='0')x=(x<<1)+(x<<3)+c-'0',c=g();
39     x*=y;
40 }
41 void o(long long x)
42 {
43     if(x<0)
44     {
45         p('-');
46         x=-x;
47     }
48     if(x>9)o(x/10);
49     p(x%10+'0');
50 }
51 int main()
52 {
53     in(n);
54     l=1,r=0;
55     For(i,1,n)
56     in(a[i]);
57     For(i,1,n)
58     {
59         while(l<=r&&a[q[r]]>=a[i])aa[q[r--]].r=i-1;
60         aa[i].l=q[r++]+1;
61         q[r]=i;
62     }
63     while(l<=r)aa[q[r--]].r=n;
64     For(i,1,n)
65     ans=max(ans,(aa[i].r-aa[i].l+1)*a[i]);
66     o(ans);
67      return 0;
68 }
原文地址:https://www.cnblogs.com/war1111/p/7774107.html