Monotonous stack --POJ - 2796

Title meaning

Given the number of a pile, in which you want to find several adjacent number

They and their minimum is multiplied by the number of all the biggest

A first output value and the coordinates and the coordinates of the last number of a number thereof obtained

Topic analysis

The formula is: These numbers and these numbers are multiplied by the minimum

So if the same minimum value, the counted number is better (because the number greater than zero)

Then we can consider according to the minimum interval is divided, according to the minimum value from small to large

In this case the adjacent section ab, is less than a minimum value of the minimum value b

When the update interval, so that the minimum value of the interval b and multiplied by representatives b, b gets the value range, compared with the answer, that becomes bigger

Then b integrated into a range of range of the interval, it means that we have to count that after a minimum interval multiplied by a representation

Note that the a [n + 1] -1 is assigned, so it can update the value of the total interval

Topic Code

#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=1e5+7;
int n,top,q[maxn],Left[maxn],ll,rr;
LL a[maxn],sum[maxn],ans;
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("LLD% " , & A [I]), SUM [I] = SUM [I- . 1 ] + A [I]; 
    Top = 0 , ANS = - . 1 ; 
    A [ ++ n-] = - . 1 ;
     for ( int I = . 1 ; I <= n-; I ++ ) {
         IF (Top == 0 || A [Q [Top]] < A [I]) { 
            Q [ ++ Top] = I; 
            Left [I] = I; / // you need a number representative of the number of coordinates of the first section 
            Continue ; 
        } 
        IF (a [I] == a [Q [Top]]) Continue ;
         the while (Top> 0&&a[q[top]]>a[i]){
            LL temp=(sum[i-1]-sum[Left[q[top]]-1])*a[q[top]];
            if(temp>ans){
                ans=temp;
                rr=i-1;
                ll=Left[q[top]];
            }
            top--;
        }
//        q[++top]=i;
        Left[i]=Left[q[++top]];
        q[top]=i;
    }
    printf("%lld\n%d %d\n",ans,ll,rr);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/helman/p/11222359.html