UVA 1619 Feel Good

//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define MS0(a) memset(a,0,sizeof(a))

using namespace std;

typedef long long ll;
const int maxn=1000100;
const int INF=1<<29;

int n;
ll a[maxn],s[maxn];
int L[maxn],R[maxn];
struct Node
{
    int x,y;
    ll w;
};Node ans;

int main()
{
    freopen("in.txt","r",stdin);
    while(cin>>n){
        REP(i,1,n) scanf("%lld",&a[i]);
        s[0]=0;REP(i,1,n) s[i]=s[i-1]+a[i];
        L[1]=1;
        REP(i,2,n){
            int j=i-1;
            while(j>=1&&a[i]<=a[j]) j=L[j]-1;
            L[i]=j+1;
        }
        R[n]=n;
        for(int i=n-1;i>=1;i--){
            int j=i+1;
            while(j<=n&&a[i]<=a[j]) j=R[j]+1;
            R[i]=j-1;
        }
        Node ans={1,1,-1};
        REP(i,1,n){
            ll t=(s[R[i]]-s[L[i]-1])*a[i];
            if(t>ans.w) ans=(Node){L[i],R[i],t};
        }
        //REP(i,1,n) cout<<L[i]<<" ";cout<<endl;
        //REP(i,1,n) cout<<R[i]<<" ";cout<<endl;
        printf("%lld\n",ans.w);
        printf("%d %d\n",ans.x,ans.y);
    }
    return 0;
}
View Code

uva上无限坑爹。。。在poj上过了也算过了吧。。。

posted @ 2015-12-19 21:49  __560  阅读(375)  评论(0编辑  收藏  举报