// LuoGuP2866//#define LOCAL#include<bits/stdc++.h>usingnamespace std;#define ll long long#define mem(a, b) memset(a,b,sizeof(a))#define sz(a) (int)a.size()#define INF 0x3f3f3f3f#define DNF 0x7f#define DBG printf("this is a input\n")#define fi first#define se second#define mk(a, b) make_pair(a,b)#define pb push_back#define LF putchar('\n')#define SP putchar(' ')#define p_queue priority_queue#define CLOSE ios::sync_with_stdio(0); cin.tie(0)template<typename T>voidread(T &x){x =0;char ch =getchar();ll f =1;while(!isdigit(ch)){if(ch =='-')f *=-1;ch =getchar();}while(isdigit(ch)){x = x *10+ ch -48; ch =getchar();}x *= f;}template<typename T,typename... Args>voidread(T &first, Args&... args){read(first);read(args...);}template<typename T>voidwrite(T arg){T x = arg;if(x <0){putchar('-'); x =- x;}if(x >9){write(x /10);}putchar(x %10+'0');}template<typename T,typename... Ts>voidwrite(T arg, Ts ... args){write(arg);if(sizeof...(args)!=0){putchar(' ');write(args ...);}}usingnamespace std;
ll gcd(ll a, ll b){return b ==0? a :gcd(b, a % b);}
ll lcm(ll a, ll b){return a /gcd(a, b)* b;}constint N =3e5+5;int n, a[N], idx[N];int st[N], top =0;intmain(){
read (n);
a[n +1]= INF;for(int i =1; i <= n ; i ++)
read (a[i]);for(int i =1; i <= n +1; i ++){if(top ==0)
st[++top]= i;else{for(; top && a[st[top]]<= a[i]; top --)
idx[st[top]]= i;
st[++top]= i;}}
ll sum =0;for(int i =1; i <= n ; i ++){if(idx[i]!=0)
sum +=(idx[i]- i -1);}
cout << sum << endl;}
// POJ2796//#define LOCAL#include<iostream>#include<stdio.h>#include<algorithm>#include<string.h>usingnamespace std;#define ll long long#define mem(a, b) memset(a,b,sizeof(a))#define sz(a) (int)a.size()#define INF 0x3f3f3f3f#define DNF 0x7f#define DBG printf("this is a input\n")#define fi first#define se second#define mk(a, b) make_pair(a,b)#define pb push_back#define LF putchar('\n')#define SP putchar(' ')#define p_queue priority_queue#define CLOSE ios::sync_with_stdio(0); cin.tie(0)usingnamespace std;
ll gcd(ll a, ll b){return b ==0? a :gcd(b, a % b);}
ll lcm(ll a, ll b){return a /gcd(a, b)* b;}constint N =1e6+5;int n;
ll a[N], sum[N];int l[N], r[N];int st[N], top =0;intmain(){
scanf ("%d",&n);for(int i =1; i <= n ; i ++)scanf("%lld",&a[i]), sum[i]= sum[i-1]+ a[i];for(int i =1; i <= n ; i ++){if(!top) st[++top]= i;else{for(; top && a[i]< a[st[top]];-- top)
r[st[top]]= i;
st[++top]= i;}}
top =0;for(int i = n ; i >=1; i --){if(!top) st[++top]= i;else{for(; top && a[i]< a[st[top]];-- top)
l[st[top]]= i;
st[++top]= i;}}
ll ans =-1;int ansl =-1, ansr =-1;for(int i =1; i <= n ; i ++){int x , y;if(!l[i]) x =1;else x = l[i]+1;if(!r[i]) y = n;else y = r[i]-1;if(1ll*(sum[y]- sum[x-1])* a[i]> ans){
ans =1ll*(sum[y]- sum[x -1])* a[i];
ansl = x;
ansr = y;}}printf("%lld\n",ans);printf("%d %d\n",ansl , ansr);}
性质二
左右配对
代表题:LeeCode42 接雨水
classSolution{public:int q[5000005], top;int sum =0;inttrap(vector<int>& height){for(int i =0; i < height.size(); i ++){if(!top) q[++top]= i;else{int width =0;while(top && height[i]> height[q[top]]){int temp = height[q[top]];-- top;if(top ==0)break;if(temp == height[q[top]])continue;
width = i - q[top]-1;
sum += width *(min(height[i], height[q[top]])-temp);}
q[++top]= i;}}return sum;}};