poj 2796 Feel Good 单调栈/dp

本文介绍了一种利用单调栈解决区间最值问题的方法,并提供两种实现思路:一是使用单调栈直接求解;二是采用类似动态规划的方式进行递推求解。通过这两种方法对比,帮助读者理解单调栈的应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目

题目链接:http://poj.org/problem?id=2796

题目来源:某单调栈题目总结

简要题意:求(区间内最小值乘以区间的和)的最大值

题解

根据题意,只需要求出以某值为最小值,向左右最多到哪里。

这题首先可以用单调栈来解决,利用单调栈求出左右到哪然后更新。

然后也可以利用一个类似dp的递推去求,利用之前处理的结果来加速递推,达到线性。

在此给出两个版本的代码,两种方法作用类似,运行时间也是接近的,代码行数也接近。

个人是觉得第二种方法更加直观,不过都学学也是好的。

单调栈代码

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>

#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
LL powmod(LL a,LL b, LL MOD) {LL res=1;a%=MOD;for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;}
// head
const int N = 1E5+5;

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

        LL ans = -1;
        int l, r;
        stack<int> s;
        s.push(0);
        for (int i = 1; i <= n+1; i++) {
            while (a[s.top()] > a[i]) {
                int cur = s.top();
                s.pop();

                int cl = s.top()+1, cr = i-1;
                LL cans = (sum[cr]-sum[cl-1]) * a[cur];
                if (cans > ans) {
                    ans = cans;
                    l = cl, r = cr;
                }
            }
            s.push(i);
        }
        printf("%I64d\n%d %d\n", ans, l, r);
    }
    return 0;
}

递推代码

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>

#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
LL powmod(LL a,LL b, LL MOD) {LL res=1;a%=MOD;for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;}
// head
const int N = 1E5+5;

int a[N], l[N], r[N];
LL sum[N];
int main() {
    int n;
    while (scanf("%d", &n) == 1 && n) {
        a[0] = a[n+1] = -1;
        for (int i = 1; i <= n; i++) {
            scanf("%d", a+i);
            l[i] = r[i] = i;
            sum[i] = sum[i-1]+a[i];
        }

        for (int i = 1; i <= n; i++) {
            while (a[i] <= a[l[i]-1]) {
                l[i] = l[l[i]-1];
            }
        }
        for (int i = n; i > 0; i--) {
            while (a[i] <= a[r[i]+1]) {
                r[i] = r[r[i]+1];
            }
        }

        LL ans = -1;
        int al, ar;
        for (int i = 1; i <= n; i++) {
            LL cans = (sum[r[i]]-sum[l[i]-1]) * a[i];
            if (cans > ans) {
                ans = cans;
                al = l[i], ar = r[i];
            }
        }
        printf("%I64d\n%d %d\n", ans, al, ar);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值