《洛谷P2659 美丽的序列》

算下作为最小值的最大长度,然后找下最大的。

《洛谷P2659 美丽的序列》
// Author: levil
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
const int N = 2e6+5;
const int M = 1e6+5;
const LL Mod = 1e9+7;
#define rg register
#define pi acos(-1)
#define INF 1e9
#define CT0 cin.tie(0),cout.tie(0)
#define IO ios::sync_with_stdio(false)
#define dbg(ax) cout << "now this num is " << ax << endl;
namespace FASTIO{
    inline LL read(){
        LL x = 0,f = 1;char c = getchar();
        while(c < '0' || c > '9'){if(c == '-') f = -1;c = getchar();}
        while(c >= '0' && c <= '9'){x = (x<<1)+(x<<3)+(c^48);c = getchar();}
        return x*f;
    }
    void print(int x){
        if(x < 0){x = -x;putchar('-');}
        if(x > 9) print(x/10);
        putchar(x%10+'0');
    }
}
using namespace FASTIO;
void FRE(){/*freopen("data1.in","r",stdin);
freopen("data1.out","w",stdout);*/}

int a[N],L[N],r[N],S[N];
int main()
{
    int n;n = read();
    for(rg int i = 1;i <= n;++i) a[i] = read();
    int top = 0;
    S[++top] = 0;
    a[0] = -1,a[n+1] = -1;
    for(rg int i = 1;i <= n+1;++i)
    {
        while(top != 0 && a[i] < a[S[top]])
        {
            r[S[top]] = i;
            L[S[top]] = S[top-1];
            top--;
        }
        S[++top] = i;
    }
    LL ans = -1;
    for(rg int i = 1;i <= n;++i)
    {
        ans = max(ans,1LL*a[i]*(r[i]-L[i]-1));
    }
    printf("%lld\n",ans);
   // system("pause");    
}
View Code

 

上一篇:数的三次方根


下一篇:龟速乘,快速乘法