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

来源:互联网 发布:维特比算法 维基百科 编辑:程序博客网 时间:2024/05/17 23:43

Feel Good
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 10430 Accepted: 2855Case 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 106 - 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

63 1 6 4 5 2

Sample Output

603 5

Source

Northeastern Europe 2005

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

基本思路就是从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 longint 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 100001typedef 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
原创粉丝点击