poj 2796 st算法+二分 / 单调栈

Feel Good
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 10430 Accepted: 2855
Case Time Limit: 1000MS Special Judge

Description

Bill is developing a new mathematical theory for human emotions. His recent investigations are dedicated to studying how good or bad days influent people's memories about some period of life. 

A new idea Bill has recently developed assigns a non-negative integer value to each day of human life. 

Bill calls this value the emotional value of the day. The greater the emotional value is, the better the daywas. Bill suggests that the value of some period of human life is proportional to the sum of the emotional values of the days in the given period, multiplied by the smallest emotional value of the day in it. This schema reflects that good on average period can be greatly spoiled by one very bad day. 

Now Bill is planning to investigate his own life and find the period of his life that had the greatest value. Help him to do so.

Input

The first line of the input contains n - the number of days of Bill's life he is planning to investigate(1 <= n <= 100 000). The rest of the file contains n integer numbers a1, a2, ... an ranging from 0 to 10 6 - the emotional values of the days. Numbers are separated by spaces and/or line breaks.

Output

Print the greatest value of some period of Bill's life in the first line. And on the second line print two numbers l and r such that the period from l-th to r-th day of Bill's life(inclusive) has the greatest possible value. If there are multiple periods with the greatest possible value,then print any one of them.

Sample Input

6
3 1 6 4 5 2

Sample Output

60
3 5

Source


计算区间的最大值,某个区间的值等于 (区间的最小值)*(区间所有值之和),求出所有这样的值中的最大值。

基本思路就是从1到n每次枚举第i个值作为最小值,从i往左边找到尽可能小的下标l,满足[l,i]区间的值>=a[i],同理往右边找出尽可能大的下标r,满足[i,r]>=a[i],也就是找到以a[i]为最小值区间的起点和终点,该区间计算出来的最大值就是a[i]*区间和。显然需要预处理前缀和。

对于l,r,直接查找的话是o(n^2),但是区间最小值的满足单调性的,因为一定满足min[l,r] >= min[l,r+1],也满足min[l-1,r] <= min[l,r],所以可以用st算法预处理区间最小值,每次枚举后利用二分查找,找出符合条件的最大区间,O(nlogn)。

用st算法必须预处理出所有的log(i),不然会慢很多图


#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>

using namespace std;

#define maxn 100001
#define ll long long
int n;
int stmin[maxn][20];
ll pre[maxn];
double ln[maxn];

void init_st()
{
    for(int i = 1; i <= n; i++)
        scanf("%d", &stmin[i][0]);

    for(int j = 1; (1<<j) <= n; j++)
        for(int i = 1; i+(1<<j)-1 <= n; i++)
        stmin[i][j] = min(stmin[i][j-1], stmin[i+(1<<(j-1))][j-1]);
}

int get_min(int l, int r)
{
    if(l > r) return -1;
    int p = ln[r-l+1];

    return min(stmin[l][p], stmin[r-(1<<p)+1][p]);
}


int main()
{

    for(int i = 1; i <= maxn; i++)
        ln[i] = log(i+0.0)/log(2+0.0);

    while(~scanf("%d", &n)){
        init_st();
        pre[0] = 0;
        for(int i = 1; i <= n; i++)
            pre[i] = pre[i-1]+stmin[i][0];

        int l,r; ll ans = -1;
        for(int i = 1; i <= n; i++){
            int mini = stmin[i][0];

            int tmp = i;
            int lb, ub, mid;
            lb = 1, ub = i+1;
            while(lb < ub){
                mid = lb+(ub-lb)/2;
                if(get_min(mid, i) >= mini){
                    tmp = mid;
                    ub = mid;
                }
                else
                    lb = mid+1;
            }
            ll val = (pre[i]-pre[tmp-1])*mini;

            int tmp1 = i;
            lb = i, ub = n+1;
            while(lb < ub){
                mid = lb+(ub-lb)/2;
                if(get_min(i, mid) >= mini){
                    tmp1 = mid;
                    lb = mid+1;
                }
                else
                    ub = mid;
            }
            val += (pre[tmp1]-pre[i])*mini;
           // cout <<tmp << " " << tmp1 << " "<<val <<endl;
            if(val > ans){
                ans = val;
                l = tmp;
                r = tmp1;
            }
        }


        printf("%I64d\n%d %d\n", ans , l, r);

    }
    return 0;
}


还有一种做法是利用单调栈,O(n)求出每个点i的左边界和右边界. 总体复杂度O(n)

#include <iostream>
#include <cstring>
#include <cstdio>
#include <stack>

using namespace std;

#define maxn 100001
typedef long long ll;
int a[maxn];
ll pre[maxn];
int l[maxn], r[maxn];
int n;

int main()
{
    while(~scanf("%d", &n)){
        pre[0] = 0;
        for(int i = 0; i < n; i++){
            scanf("%d", a+i);
            pre[i+1] = pre[i]+a[i];
        }

        stack<int> s;
        for(int i = 0; i < n; i++){
            while(!s.empty() && a[s.top()] >= a[i]) s.pop();
            l[i] = s.empty() ? 0 : s.top()+1;
            s.push(i);
        }
        while(!s.empty()) s.pop();
        for(int i = n-1; i >= 0; i--){
            while(!s.empty() && a[s.top()] >= a[i]) s.pop();
            r[i] = s.empty() ? n-1 : s.top()-1;
            s.push(i);
        }

        ll ans = -1, tmp;
        int ll, rr;
        for(int i = 0; i < n; i++){
            tmp = a[i]*(pre[r[i]+1]-pre[l[i]]);
            if(tmp > ans) ans = tmp, ll = l[i]+1, rr = r[i]+1;
        }

        printf("%I64d\n%d %d\n", ans, ll, rr);
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值