0
点赞
收藏
分享

微信扫一扫

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

Soy丶sauce 2022-06-17 阅读 51

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

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

单调递增栈模板

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;
}



举报

相关推荐

0 条评论