POJ2796 单调队列
Feel Good
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 8041 | Accepted: 2177 | |
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
#include <cstdio>
#include <deque>
#include <iostream> using namespace std; int a[],l[],r[];
long long sum[]; struct Type{
int res,data;
}; int main()
{
int n;
scanf("%d",&n);
for(int i=; i<=n; i++){
scanf("%d",&a[i]);
sum[i]=sum[i-]+a[i];
} deque<Type> p;
p.clear();
for(int i=; i<=n; i++){
while(!p.empty()&&p.back().data>=a[i])
p.pop_back();
if(p.empty()) l[i]=; else l[i]=p.back().res+;
Type t;
t.res=i;
t.data=a[i];
p.push_back(t);
} p.clear();
for(int i=n; i>=; i--){
while(!p.empty()&&p.back().data>=a[i])
p.pop_back();
if(p.empty()) r[i]=n; else r[i]=p.back().res-;
Type t;
t.res=i;
t.data=a[i];
p.push_back(t);
} // for(int i=1; i<=n; i++)
// printf("%d %d\n",l[i],r[i]); long long ans=;
int ansi;
for(int i=; i<=n; i++)
if(ans<=(sum[r[i]]-sum[l[i]-])*a[i]){
ans=(sum[r[i]]-sum[l[i]-])*a[i];
ansi=i;
} printf("%I64d\n",ans);
printf("%d %d\n",l[ansi],r[ansi]); return ;
}
POJ2796 单调队列的更多相关文章
- POJ2796 Feel Good -- 单调队列
Feel Good Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 14489 Accepted: 4015 Case T ...
- BestCoder Round #89 B题---Fxx and game(单调队列)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5945 问题描述 输入描述 输出描述 输入样例 输出样例 题意:中文题,不再赘述: 思路: B ...
- 单调队列 && 斜率优化dp 专题
首先得讲一下单调队列,顾名思义,单调队列就是队列中的每个元素具有单调性,如果是单调递增队列,那么每个元素都是单调递增的,反正,亦然. 那么如何对单调队列进行操作呢? 是这样的:对于单调队列而言,队首和 ...
- FZU 1914 单调队列
题目链接:http://acm.fzu.edu.cn/problem.php?pid=1914 题意: 给出一个数列,如果它的前i(1<=i<=n)项和都是正的,那么这个数列是正的,问这个 ...
- BZOJ 1047 二维单调队列
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1047 题意:见中文题面 思路:该题是求二维的子矩阵的最大值与最小值的差值尽量小.所以可以考 ...
- 【BZOJ3314】 [Usaco2013 Nov]Crowded Cows 单调队列
第一次写单调队列太垃圾... 左右各扫一遍即可. #include <iostream> #include <cstdio> #include <cstring> ...
- BZOJ1047: [HAOI2007]理想的正方形 [单调队列]
1047: [HAOI2007]理想的正方形 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2857 Solved: 1560[Submit][St ...
- hdu 3401 单调队列优化DP
Trade Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status ...
- 【转】单调队列优化DP
转自 : http://www.cnblogs.com/ka200812/archive/2012/07/11/2585950.html 单调队列是一种严格单调的队列,可以单调递增,也可以单调递减.队 ...
随机推荐
- learning from the previous teams
开发人员水平有限.分配任务的时候经常有说这个事儿做不到,或者压根不知道怎么做:验收工作频出意外,DEV写了一个模块之后,验收的时候发现模块质量不行,代码质量低是其次,无法按照给定的接口工作.设计不足. ...
- iOS优化内存方法推荐
1. 用ARC管理内存 ARC(Automatic ReferenceCounting, 自动引用计数)和iOS5一起发布,它避免了最常见的也就是经常是由于我们忘记释放内存所造成的内存泄露.它自动为你 ...
- MySQL、SqlServer、Oracle三大主流数据库分页查询
在这里主要讲解一下MySQL.SQLServer2000(及SQLServer2005)和ORCALE三种数据库实现分页查询的方法.可能会有人说这些网上都有,但我的主要目的是把这些知识通过我实际的应用 ...
- Asp.Net原理Version1.0
Asp.Net原理Version2.0 Asp.Net原理Version3.0_页面声明周期
- 小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100。但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数)。没多久,他就得到另一组连续正数和为100的序列:18,19,20,21,22。现在把问题交给你,你能不能也很快的找出所有和为S的连续正数序列? Good Luck!
// test20.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...
- z-index兼容问题:关于ie6/7下的z-index
z-index这个属性其实在挺多地方都会用到,在百度上搜索也有大量关于z-index的篇幅去阐述这个属性,特别是在ie6下的z-index处理有更多的相关文章,本文就不再围绕z-index这一属性的基 ...
- jsp的常用指令有哪些(编译指令/动作指令整理)
jsp的常用指令有哪些(编译指令/动作指令整理) JSP动作指令 JSP - JSP中的脚本.指令.动作和注释
- Unity3d - 初学篇 Event Functions 的 继承 机制
我们知道Start() Update() 等之类的 事件函数 在Unity 主线程中是依次调用的.至于调用的顺序可以查手册. 由此继承机制也会发生一些改变. 测试一: public class MyT ...
- hdu 4588 Count The Carries
思路:容易发现二进制表示的数的最低位规律是01010101……:接着是001100110011……:接着是:0000111100001111…… 这样我们发现每一位的循环节是2^(i+1),前2^i是 ...
- lintcode :数组剔除元素后的乘积
题目: 数组剔除元素后的乘积 给定一个整数数组A. 定义B[i] = A[0] * ... * A[i-1] * A[i+1] * ... * A[n-1], 计算B的时候请不要使用除法. 样例 给出 ...