Feel Good(POJ 2796) 单调栈

还是单调栈的应用,对单调栈的理解很深了一步。也渐渐的理解了,需要优化算法的时间直到满足题目要求,虽然这个优化很简单……

1.题目原文

Language:
Feel Good
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 13381 Accepted: 3721
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

题目大意:有n个元素,定义一个区间的参考值是区间内所有元素的和与该区间内元素的最小值。求最大参考值及对应的区间。

2.解题思路1

利用单调栈,对于每个点,看它左右最远能延伸到哪里。
先考虑左边的。
对于每一个i,从左到右扫描i之前的元素,如果栈顶元素比i对应的值大,说明i可以延伸到栈顶元素,此时退栈,继续进行从上述操作,直到栈为空或者栈顶元素小于i对应的值,更新left,把i对应元素的信息,加入栈中。
不懂的是为什么向右的延伸值就是i对应元素的前一个元素(即i-1)。这样可以直接一一次求完。

3.AC代码1

使用了一个结构体存储每个元素左延伸值、右延伸值、元素值以及对应的区间和。
在求左延伸值的时候更新最大参考值。
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<string>
#include<set>
#include<vector>
#include<cmath>
#include<bitset>
#include<stack>
#include<sstream>
#include<deque>
using namespace std;
#define INF 0x7fffffff
const int maxn=100005;

typedef long long ll;

struct node
{
    ll val;
    ll left,right,sum;
    node(ll left,ll right,ll val,ll sum){
        this->left=left;
        this->right=right;
        this->val=val;
        this->sum=sum;
    }
};
int n;
int a[maxn];

void solve()
{
    ll ans=0,ansl=1,ansr=1;
    stack<node> s;
    for(int i=0;i<n;i++){
        ll sums=0;
        ll left=i;
        while(!s.empty()&&s.top().val>=a[i]){
            sums+=s.top().sum;
            left=s.top().left;
            if(sums*s.top().val>ans){
                ans=sums*s.top().val;
                ansl=s.top().left;
                ansr=i-1;
            }
            s.pop();
        }
        s.push(node(left,i,a[i],sums+a[i]));
    }
    cout<<ans<<endl<<ansl+1<<" "<<ansr+1<<endl;
}
int main()
{
    while(scanf("%d",&n)!=EOF&&n){
        for(int i=0;i<n;i++){
            scanf("%d",&a[i]);
        }
        a[n++]=0;
        solve();
    }
    return 0;
}

4.解题思路2

不是很懂解题思路1中的:向右的延伸值就是i对应元素的前一个元素(即i-1)。那就分开来求好了,像挑战里的poj2559.
第一次TLE了,没考虑求区间和可能会使时间复杂度从O(n)上升到O(n^2)(最坏的情况下为O(n^2))。
可以利用前缀和,其实就是区间和,可以理解为预处理区间和。这样时间复杂度为O(n)。
这种思路没必要更新ans,因此结构体中少一个成员变量,其他的与解题思路1无异。

5.AC代码2

#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<string>
#include<set>
#include<vector>
#include<cmath>
#include<bitset>
#include<stack>
#include<sstream>
#include<deque>
using namespace std;
#define INF 0x7fffffff
const int maxn=100005;

typedef long long ll;

struct node
{
    ll val;
    ll left,right;
    node(ll left,ll right,ll val){
        this->left=left;
        this->right=right;
        this->val=val;
    }
};
int n;
int a[maxn];
int L[maxn],R[maxn];

ll pre[maxn];

void solve()
{
    //前缀和,预处理区间和
    pre[0]=a[0];
    for(int i=1;i<n;i++){
        pre[i]=pre[i-1]+a[i];
    }

    //求左边的延伸值
    ll ans=0,ansl=1,ansr=1;
    stack<node> s;
    for(int i=0;i<n;i++){
        //ll sums=0;
        ll left=i;
        while(!s.empty()&&s.top().val>=a[i]){
            //sums+=s.top().sum;
            left=s.top().left;
            /*if(sums*s.top().val>ans){
                ans=sums*s.top().val;
                ansl=s.top().left;
                //ansr=i-1;
            }*/
            s.pop();
        }
        L[i]=left;
        s.push(node(left,i,a[i]));
    }

    while(!s.empty()){
        s.pop();
    }
    
    //求右边的延伸值
    for(int i=n-1;i>=0;i--){
        //ll sums=0;
        ll right=i;
        while(!s.empty()&&s.top().val>=a[i]){
            //sums+=s.top().sum;
            right=s.top().right;
            /*if(sums*s.top().val>ans){
                ans=sums*s.top().val;
                ansr=s.top().right;
            }*/
            s.pop();
        }
        R[i]=right;
        s.push(node(i,right,a[i]));
    }

    for(int i=0;i<n;i++){
        /*for(int j=L[i];j<=R[i];j++){
            res+=a[i]*a[j];
        }*/
        //直接求会TLE
        ll res=a[i]*(pre[R[i]]-pre[L[i]-1]);
        if(res>ans){
            ans=res;
            ansl=L[i];
            ansr=R[i];
        }
    }
    cout<<ans<<endl<<ansl+1<<" "<<ansr+1<<endl;
}
int main()
{
    while(scanf("%d",&n)!=EOF&&n){
        for(int i=0;i<n;i++){
            scanf("%d",&a[i]);
        }
        a[n++]=0;
        solve();
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值