Poj 2796 单调栈
关于单调栈的性质,和单调队列基本相同,只不过单调栈只使用数组的尾部, 类似于栈。
Accepted Code:
/*************************************************************************
> File Name: 2796.cpp
> Author: Stomach_ache
> Mail: sudaweitong@gmail.com
> Created Time: 2014年07月21日 星期一 22时45分56秒
> Propose:
************************************************************************/
#include <cmath>
#include <string>
#include <cstdio>
#include <fstream>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long LL;
const int maxn = ;
int n;
// b[i] holds the smallest index where a[b[i]]...a[i] are not larger than a[i], and e[i] is similar, just extends to the right of a[i]
int a[maxn], b[maxn], e[maxn], st[maxn];
LL sum[maxn]; //prefix sum int
main(void) {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
while (~scanf("%d", &n)) {
for (int i = ; i <= n; i++) scanf("%d", a+i);
for (int i = ; i <= n; i++) b[i] = e[i] = i;
sum[] = ;
for (int i = ; i <= n; i++) {
sum[i] = sum[i-] + a[i];
}
// the top of no decreasing stack
int top = ;
for (int i = ; i <= n; i++) {
if (!top || a[i] > a[st[top-]]) {
// push an element to stack
st[top++] = i;
} else {
while (top > && a[st[top-]] > a[i]) {
// update e[st[top-1]]
e[st[top-]] = i - ;
// update b[i]
b[i] = b[st[top-]];
// pop the toppest element of stack
top--;
}
if (!top || a[st[top-]] != a[i]) {
// push
st[top++] = i;
} else if(a[st[top-] == a[i]]) {
// update b[i]
b[i] = b[st[top-]];
}
}
}
// update e[i] of those elements that still in the stack
for (int i = ; i < top; i++) e[st[i]] = n;
// find the answer
LL ans = -, bb, ee;
for (int i = ; i <= n; i++) {
if (ans < a[i] * (sum[e[i]]-sum[b[i]-])) {
ans = a[i] * (sum[e[i]]-sum[b[i]-]);
bb = b[i];
ee = e[i];
}
}
printf("%lld\n%lld %lld\n", ans, bb, ee);
} return ;
}
Poj 2796 单调栈的更多相关文章
- [poj 2796]单调栈
题目链接:http://poj.org/problem?id=2796 单调栈可以O(n)得到以每个位置为最小值,向左右最多扩展到哪里. #include<cstdio> #include ...
- uva 1619 - Feel Good || poj 2796 单调栈
1619 - Feel Good Time limit: 3.000 seconds Bill is developing a new mathematical theory for human ...
- Poj 3250 单调栈
1.Poj 3250 Bad Hair Day 2.链接:http://poj.org/problem?id=3250 3.总结:单调栈 题意:n头牛,当i>j,j在i的右边并且i与j之间的所 ...
- poj 2059 单调栈
题意:求柱状图中最大矩形面积. 单调栈:顾名思义就是栈内元素单调递增的栈. 每次插入数据来维护这个栈,假设当前须要插入的数据小于栈顶的元素,那就一直弹出栈顶的元素.直到满足当前须要插入的元素大于栈顶元 ...
- POJ 3044单调栈
题意: 思路: 单调栈 // by SiriusRen #include <stack> #include <cstdio> using namespace std; stac ...
- poj 2082 单调栈 ***
和poj2082差不多,加了一个宽度的条件 #include<iostream> #include<string> #include<cmath> #include ...
- poj 2559 单调栈 ***
给出一系列的1*h的矩形,求矩形的最大面积. 如图: 题解链接:点我 #include <iostream> #include <cstdio> using namespace ...
- poj 2599 单调栈 ***
和poj2082差不多,加了一个宽度的条件 #include<cstdio> #include<cmath> #include<algorithm> #includ ...
- POJ 2796:Feel Good(单调栈)
http://poj.org/problem?id=2796 题意:给出n个数,问一个区间里面最小的元素*这个区间元素的和的最大值是多少. 思路:只想到了O(n^2)的做法. 参考了http://ww ...
- POJ 2796 Feel Good 【单调栈】
传送门:http://poj.org/problem?id=2796 题意:给你一串数字,需要你求出(某个子区间乘以这段区间中的最小值)所得到的最大值 例子: 6 3 1 6 4 5 2 当L=3,R ...
随机推荐
- Cesium官方教程11--建模人员必读
原文地址:https://cesium.com/blog/2014/12/15/gltf-tips-for-artists/ 这篇文章是Branden Coker, an artist from AG ...
- PAT甲级——A1087 All Roads Lead to Rome【30】
Indeed there are many different tourist routes from our city to Rome. You are supposed to find your ...
- mybtais分批insert
这里自己写了个对集合按一批的数量进行分批操作的分页bean,见PagenationUtil如下: package com.util; import java.util.List ; /** * @au ...
- PageBarHelper分页显示类
一共有两个分页类,都可以使用(单独使用) using System;using System.Collections.Generic;using System.Linq;using System.Te ...
- DataLossError (see above for traceback): file is too short to be an sstable [[Node: save/RestoreV2 = RestoreV2[dtypes=[DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, ..., DT_FLOAT, DT_FLOAT, DT_F
DataLossError (see above for traceback): file is too short to be an sstable [[Node: save/RestoreV2 = ...
- 不小心使用vcpkg之后再使用conan,一直报链接错误
原来是使用vcpkg的时候,不小心使用了.\vcpkg integrate install命令,把vcpkg到所有的vs项目(这个不需要什么其他的引用,但是容易起冲突) 然后卸载掉就好了,这篇文章真是 ...
- C#端一个不错的订单号生成规则
/// <summary> /// 订单助手 /// </summary> public class OrderHelper { /// <summary> /// ...
- Redis List类型学习
- [POI2017]Sabota【观察+树形Dp】
Online Judge:Bzoj4726 Label:观察,树形Dp,水题 题目描述 某个公司有n个人, 上下级关系构成了一个有根树.公司中出了个叛徒(这个人不知道是谁). 对于一个人, 如果他下属 ...
- CodeForces - 1087D
CodeForces - 1087Dhttps://vjudge.net/problem/2115151/origin2*和/叶子结点的个数 #include<iostream> #inc ...