单调栈(SOJ 3085)

SOJ 3085: windy‘s cake V http://acm.scu.edu.cn/soj/problem.action?id=3085

Problem: Given a list of $n$ positive integers $num[1], ..., num[n]$, denote the score of one sublist from $i$ to $j$ as $(\min_{i\le k\le j}num[k])*\left(\Sigma_{k=i}^{j}num[k]\right)$. The question is that which sublist has the largest score? Output the largest score.

Analysis: we can enumerate each $num[i], 1\le i\le n$ to be the minimum number in one sublist. Therefore, for each $num[i], 1\le i\le n$, we need to find the corresponding sublist, that is, to find the first number $num[i‘]$ that is smaller than $num[i]$ from $i-1$ to $1$ and the first number $num[i‘‘]$ that is smaller than $num[i]$ from $i+1$ to $n$ (similar to SOJ 2511, please see here). Then, for $num[i]$ the score $score[i]=num[i]*\left(\Sigma_{k=i‘}^{i‘‘}num[k]\right)$, and the solution is $\max_{1\le k\le n}score[i]$.

Technique: Maintaining an increasing stack.

Code:

#include<iostream>
#include<stack>
using namespace std;
struct node
{
    int no;
    int num;
    int prevNo;
    node(){prevNo=0;}
};
node arr[100005];
stack<node>s;
long long sum[100005];
int main()
{
    int n;
    int temp1;
    int i;
    long long temp2;
    long long ans;
    while(scanf("%d",&n)==1)
    {
        sum[0]=0;
        for(i=1;i<=n;i++)
        {
            scanf("%d",&temp1);
            sum[i]=sum[i-1]+temp1;
            arr[i].no=i;
            arr[i].num=temp1;
        }
        arr[0].no=0;
        arr[0].num=-1;
        arr[n+1].no=n+1;
        arr[n+1].num=-1;
        s.push(arr[0]);
        ans=0;
        for(i=1;i<=n+1;i++)
        {
            while(arr[i].num<s.top().num)
            {
                temp2=s.top().num*(sum[i-1]-sum[s.top().prevNo]);
                ans=temp2>ans ? temp2 : ans;
                s.pop();
            }
            arr[i].prevNo=s.top().no;
            s.push(arr[i]);
        }
        while(!s.empty())
            s.pop();
        printf("%lld\n",ans);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/ClearMoonlight/p/10529584.html

时间: 2024-07-29 16:40:52

单调栈(SOJ 3085)的相关文章

(转载)单调栈题目总结

POJ 2796 Feel Good 题意:给出一个长度为n(n<100000)的序列,求出一个子序列,使得这个序列中的最小值乘以这个序列的和的值最大. 思路:枚举每一个点,然后算出以这个点为最小值的区间能向左向右扩展到哪里,然后选择最优的就行. SOJ 3085: windy's cake V 题意:和POJ2796一样,只是不要求输出子序列的两个端点. 思路:粘贴上一题的代码就行 SOJ 3329: Maximum Submatrix II 题意:给出一个n*m的矩阵,求出这个矩阵的最大0矩

(单调栈)poj-2559 Largest Rectangle in a Histogram

A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figure on the left shows the histogram that consists of rectangles with the

【单调栈】hdu1506 Largest Rectangle in a Histogram

单调栈的介绍及一些基本性质 http://blog.csdn.net/liujian20150808/article/details/50752861 依次把矩形塞进单调栈,保持其单增,矩形中的元素是一个三元组,存储其位置,高度,以及以其为高度的情况下,大矩形的左边界最多扩展到哪里. 每次将新的元素塞进栈的时候,其左边界就是其左侧第一个小于它的矩形的位置+1. 然后,每个矩形出栈的时候,记录其右边界为当前往栈里面塞的矩形的位置-1,然后更新答案即可. 注意最后把所有的矩形出栈,更新答案. #in

BZOJ 3238 AHOI 2013 差异 后缀数组+单调栈

题目大意: 思路:一看各种后缀那就是后缀数组没跑了. 求出sa,height之后就可以乱搞了.对于height数组中的一个值,height[i]来说,这个值能够作为lcp值的作用域只在左边第一个比他小的位置到右边第一个比他小的位置.这个东西很明显可以倍增RMQ+二分/单调栈. 之后就是数学题了 Σlen[Ti] + len[Tj] = (len + 1) * len * (len - 1),之后吧所有求出来的Σ2 * lcp(Ti,Tj)减掉就是答案. 记得答案开long long CODE:

51nod 1215 数组的宽度&amp;poj 2796 Feel Good(单调栈)

单调栈求每个数在哪些区间是最值的经典操作. 把数一个一个丢进单调栈,弹出的时候[st[top-1]+1,i-1]这段区间就是弹出的数为最值的区间. poj2796 弹出的时候更新答案即可 #include<iostream> #include<cstdlib> #include<cstring> #include<cstdio> #include<algorithm> #include<queue> #include<cmath

[poj3250]单调栈 Bad Hair Day

解题关键:将每头牛看到的牛头数总和转化为每头牛被看到的次数,然后用单调栈求解,其实做这道题的目的只是熟悉下单调栈 此题为递减栈 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<cstdlib> 5 #include<stack> 6 #include<iostream> 7 using namespace std; 8 typedef lo

HDU 5033---Building(单调栈)

题目链接 Problem Description Once upon a time Matt went to a small town. The town was so small and narrow that he can regard the town as a pivot. There were some skyscrapers in the town, each located at position xi with its height hi. All skyscrapers loc

POJ 3415 Common Substrings(后缀数组+单调栈)

[题目链接] http://poj.org/problem?id=3415 [题目大意] 求出两个字符串长度大于k的公共子串的数目. [题解] 首先,很容易想到O(n2)的算法,将A串和B串加拼接符相连, 做一遍后缀数组,把分别属于A和B的所有后缀匹配,LCP-k+1就是对答案的贡献, 但是在这个基础上该如何优化呢. 我们可以发现按照sa的顺序下来,每个后缀和前面的串的LCP就是区间LCP的最小值, 那么我们维护一个单调栈,将所有单调递减的LCP值合并, 保存数量和长度,对每个属于B串的后缀更新

poj2796 维护区间栈//单调栈

http://poj.org/problem?id=2796 题意:给你一段区间,需要你求出(在这段区间之类的最小值*这段区间所有元素之和)的最大值...... 例如: 6 3 1 6 4 5 2 以4为最小值,向左右延伸,6 4 5  值为60....... 思路:解决完为这道题目,我才真正明白了单调栈的原理,它就是以某一个值为最小(最大)值,向这个值的两侧延伸,遇到大于它(小于它)的值,就将它延伸的范围扩大,当然,一般来说,要这样做的算法复杂度为o(n^2),但是借助栈这个玩意,维护其单调增