【POJ】2796:Feel Good【单调栈】
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 18449 | Accepted: 5125 | |
Case Time Limit: 1000MS | Special Judge |
Description
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
Output
Sample Input
6
3 1 6 4 5 2
Sample Output
60
3 5
Source
Solution
题意:找一个区间,使这个区间最小值乘上这个区间的和最大。
用单调栈维护递增,找出以每个$i$为最小值的最远的左右端点即可。
每次弹栈就更新被弹元素的右端点,入栈时更新入栈元素的左端点即可。
(为什么不把题说清楚有多组数据还有spj太垃圾了吧必须要区间最小!!!!)
(真的受不了了为什么poj那么卡aaaaa!!!浪费了我好多好多时间!!!!!!!!【粉转黑!!)
Code
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#define LL long long
using namespace std; LL a[], pre[];
int stk[], R[], L[], n;
int main() {
while(~scanf("%d", &n)) {
int top = ;
memset(pre, , sizeof(pre));
memset(L, , sizeof(L));
memset(R, , sizeof(R));
for(int i = ; i <= n; i ++) {
scanf("%I64d", &a[i]);
pre[i] = pre[i - ] + a[i];
while(a[i] < a[stk[top]] && top) {
R[stk[top --]] = i - ;
}
L[i] = stk[top] + ;
stk[++ top] = i;
}
while(top) {
R[stk[top --]] = n;
}
LL ans = ; int l, r;
for(int i = ; i <= n; i ++) {
LL tmp = (pre[R[i]] - pre[L[i] - ]) * a[i];
if(tmp > ans) {
ans = tmp;
l = L[i], r = R[i];
}
if(tmp == ans) {
if(R[i] - L[i] + < r - l + ) {
l = L[i], r = R[i];
}
}
}
printf("%I64d\n%d %d\n", ans, l, r);
} return ;
}
【POJ】2796:Feel Good【单调栈】的更多相关文章
- poj 2796 Feel Good单调栈
Feel Good Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 20408 Accepted: 5632 Case T ...
- poj 2796 Feel Good 单调栈区间问题
Feel Good 题意:给你一个非负整数数组,定义某个区间的参考值为:区间所有元素的和*区间最小元素.求该数组中的最大参考值以及对应的区间. 比如说有6个数3 1 6 4 5 2 最大参考值为6,4 ...
- POJ 3658 Artificial Lake (单调栈)
题意: 析:利用单调栈,维护一个单调递增的栈,首先在最低的平台开始,每次向两边进行扩展,寻找两边最低的,然后不断更新宽度. 代码如下: #pragma comment(linker, "/S ...
- poj 2559 Largest Rectangle(单调栈)
Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26549 ...
- POJ - 2796 Feel Good 单调递增栈+前缀和
Feel Good Bill is developing a new mathematical theory for human emotions. His recent investigations ...
- POJ 3415 后缀数组+单调栈
题目大意: 给定A,B两种字符串,问他们当中的长度大于k的公共子串的个数有多少个 这道题目本身理解不难,将两个字符串合并后求出它的后缀数组 然后利用后缀数组求解答案 这里一开始看题解说要用栈的思想,觉 ...
- poj 2796 Feel Good 单调队列
Feel Good Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 8753 Accepted: 2367 Case Ti ...
- [poj 2796]单调栈
题目链接:http://poj.org/problem?id=2796 单调栈可以O(n)得到以每个位置为最小值,向左右最多扩展到哪里. #include<cstdio> #include ...
- 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 ...
随机推荐
- angular架构
angular架构包括以下部分: 1.模块 2.组件 3.模板 4.元数据 5.数据绑定 6.指令 7.服务 8.依赖注入 9.动画 10.变更检测 11.事件 12.表单 13.HTTP 14.生命 ...
- 详解MySQL大表优化方案
单表优化 除非单表数据未来会一直不断上涨,否则不要一开始就考虑拆分,拆分会带来逻辑.部署.运维的各种复杂度,一般以整型值为主的表在千万级以下,字符串为主的表在五百万以下是没有太大问题的.而事实上很多时 ...
- jquery 绑定,mvc和webform的三种方式
asp.net里的绑定方式,on的绑定方式无效 $('#SelCommandType').bind('click', function () { }); mvc里的绑定方式 $('#DownList' ...
- Flask 对象关系
建立一个关系 from sqlalchemy import Column, Integer, String, MetaData, ForeignKey from sqlalchemy.ext.decl ...
- 定制Eclipse
转载自http://chriszz.sinaapp.com 一般从Eclipse官网eclipse.org下载的,都是打包好的版本,比如标准版.jee版.java版.c++版.php版.测试版等.有时 ...
- Elasticsearch的相关知识
Elasticsearch的备份和恢复 http://keenwon.com/1393.html ETL kettle 数据转成json 发送POST请求 http://blog.csdn.net/a ...
- 结合Python代码介绍音符起始点检测 (onset detection)
本文由 meelo 原创,请务必以链接形式注明 本文地址 音符起始点检测介绍 音符起始点检测(onset detection)是音乐信号处理中非常重要的一个算法.节拍和速度(tempo)的检测都会基于 ...
- Hadoop案例(十一)MapReduce的API使用
一学生成绩---增强版 数据信息 computer,huangxiaoming,,,,,,, computer,xuzheng,,,,, computer,huangbo,,,, english,zh ...
- windows7无声音,提示未插入扬声器或耳机的解决
windows7无声音,提示未插入扬声器或耳机的解决: http://jingyan.baidu.com/article/358570f6043a85ce4624fc47.html
- Gitlab-API各状态码解释
200 – OK : This means that the GET , PUT, or DELETE request was successful. When you request a resou ...