POJ 3250 Bad Hair Day、POJ 2796 Feel Good(单调栈)

简介: POJ 3250 Bad Hair Day、POJ 2796 Feel Good(单调栈)

单调栈是栈内元素保持一定单调性(单调递增或单调递减)的栈。这里的单调递增或递减是指的从栈顶栈底单调递增或递减, 需要注意的是它具有严格性(严格单调增或减)。

单调递增栈的实现:如果栈为空或入栈元素值小于栈顶元素值,则入栈;否则,如果入栈则会破坏栈的单调性,则需要把比入栈元素小的元素全部出栈。

单调递增栈模板

stack<int> s;
while (!s.empty() && t>=s.top())
    s.pop();
ans+=s.size();
s.push(t);

POJ 3250 Bad Hair Day 应用单调栈解题
转化成第i个牛可以被左边几个牛看见

#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
#include <algorithm>
using namespace std;
typedef long long ll;

int main()
{
   
    int n, t;
    scanf("%d", &n);
    stack<int> s;
    scanf("%d", &t);
    s.push(t);
    ll ans=0;
    for (int i=2; i<=n; i++){
   
        scanf("%d", &t);
        while (!s.empty() && t>=s.top())
            s.pop();
        ans+=s.size();
        s.push(t);
    }
    printf("%lld", ans);
    return 0;
}

POJ 2796 Feel Good
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
主要思路:
找出每个点向左向右第一个比它小的位置l和r,那么在l+1和r-1的区间内
他就是最小的值,用一个前缀和的差来记录这个区间的和,最后求从1位置到n位置
最小值乘区间和的最大值即可

#include <iostream>
#include <cstdio>
#include <cmath> 
#include <cstring> 
#include <string>
#include <map> 
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
using namespace std;

typedef long long ll;
ll a[100005], sum[100005];

int main()
{
   
    int n, t=0;
    int left[100005], right[100005];
    while (scanf("%d", &n)!=EOF && n){
   
        //if (t++)    printf("\n");
        memset(a, 0, sizeof(a));
        memset(left, -1, sizeof(left));
        memset(right, -1, sizeof(right));
        memset(sum, 0, sizeof(sum));

        for (int i=1; i<=n; i++){
   
            scanf("%lld", &a[i]);
            sum[i]=sum[i-1]+a[i];
        }

        stack<int> s;
        //找每个结点右边第一个比它小的位置 
        for (int i=1; i<=n; i++){
   
            while (!s.empty() && a[s.top()]>a[i]){
   
                right[s.top()]=i;
                s.pop();
            }
            s.push(i);
        }

        while (!s.empty())    s.pop();
        //找每个结点左边第一个比它小的位置 
        for (int i=n; i>=1; i--){
   
            while (!s.empty() && a[s.top()]>a[i]){
   
                left[s.top()]=i;
                s.pop();
            }
            s.push(i);
        }

        ll ans=-1;
        int ans_l=-1, ans_r=-1;
        for (int i=1; i<=n; i++){
   
            int l=(left[i]==-1? 0: left[i]);
            int r=(right[i]==-1? n: right[i]-1);
            ll cur=(sum[r]-sum[l])*a[i];
            if (cur>ans){
   
                ans=cur;
                ans_l=l+1;
                ans_r=r;
            }
        }
        printf("%lld\n%d %d\n", ans, ans_l, ans_r);
    }
    return 0;
}

参考链接:
https://blog.csdn.net/Floraqiu/article/details/81947461
https://blog.csdn.net/flx413/article/details/80188776

相关文章
|
5天前
|
算法 C语言 C++
【practise】栈的压入和弹出序列
【practise】栈的压入和弹出序列
|
3天前
栈的几个经典应用,真的绝了
文章总结了栈的几个经典应用场景,包括使用两个栈来实现队列的功能以及利用栈进行对称匹配,并通过LeetCode上的题目示例展示了栈在实际问题中的应用。
栈的几个经典应用,真的绝了
|
4天前
|
存储 网络协议 Linux
用户态协议栈06-TCP三次握手
用户态协议栈06-TCP三次握手
|
4天前
|
存储
全局变量和局部变量在堆和栈的区别
全局变量和局部变量在堆和栈的区别
11 0
|
5天前
|
存储 人工智能 运维
高质量存储力发展问题之浪潮信息发布的大模型智算软件栈的定义如何解决
高质量存储力发展问题之浪潮信息发布的大模型智算软件栈的定义如何解决
8 0
|
5天前
|
设计模式 算法 C语言
【CPP】栈、双端队列、队列、优先级队列与反向迭代器
【CPP】栈、双端队列、队列、优先级队列与反向迭代器
|
5天前
|
存储 算法 C++
【CPP】栈简介及简化模拟实现
【CPP】栈简介及简化模拟实现
|
5天前
【数据结构】用栈实现队列
【数据结构】用栈实现队列
|
5天前
【数据结构】用队列实现栈
【数据结构】用队列实现栈
|
5天前
【数据结构】栈
【数据结构】栈