POJ2796 Feel Good -- 单调队列
| Time Limit: 3000MS | Memory Limit: 65536K | |
| Total Submissions: 14489 | Accepted: 4015 | |
| 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 题意:
给出一个数列,求其一个区间中 最小值 与 区间元素和 的乘积的最大值;
分析:
1.将每个元素看做所在区间的最小值 向左右两区间进行查找,找以其为最小值的最大区间;
2.单调队列的应用,以查找以当前元素为最小值的最大区间的左端点为例:
①构造严格递增的单调队列,即进队元素需比队尾元素大,否则队尾元素出队;
②从第一个元素开始进行进队,将当前值与队尾进行比较,若队尾大于当前元素,则队尾出队,否则队尾元素的下标便是以当前元素为最小值
的最大区间的左端点;
查找以当前元素为最小值的最大区间的右端点方法相同。
代码分析:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <map>
#include<string.h>
#include<stack>
#include<set>
#include <queue>
using namespace std; long long a[];
//数组模拟队列
long long q[];
//以每个元素为最小值的最大区间左端点
long long l[];
//以每个元素为最小值的最大区间右端点
long long r[];
//存队列中每个元素的下标
long long p[];
//区间的值
long long sum[];
int main()
{
int n,i,j;
while(~scanf("%d",&n))
{
sum[] = ;
for(i = ; i<=n; i++)
{
scanf("%lld",a+i);
sum[i] = sum[i-]+a[i];
}
//初始化队头
q[] = -;
p[] = ;
p[n+] = n+;
q[n+] = -;
int head = ;
int tail = ;
//查找以当前元素为最小值的最大区间的左端点
for(i = ; i<=n; i++)
{ while(head<=tail&&q[tail]>=a[i]) tail--;//队尾元素大于等于当前元素
//以当前元素为最小值的最大区间的左端点
l[i] = p[tail];
//当前元素进队
q[++tail] = a[i];
//记录下标
p[tail] = i;
}
//查找以当前元素为最小值的最大区间的右端点
q[] = -;
p[] = ;
p[n+] = n+;
q[n+] = -;
head = n;
tail = n+;
for(i = n ; i>=; i--)
{
while(head>=tail&&q[tail]>=a[i]) tail++;
r[i] = p[tail];
q[--tail] = a[i];
p[tail] = i;
}
long long max1 = -;
int k = ; //标记最大值的区间
for(i = ; i<=n; i++)
{ if(max1<a[i]*(sum[r[i]-]-sum[l[i]]))
{
max1= a[i]*(sum[r[i]-]-sum[l[i]]);
k = i;
}
}
printf("%lld\n",max1);
printf("%lld %lld\n",l[k]+,r[k]-); }
return ;
}
个人随笔,望大佬勿喷,若能提供帮助不胜荣幸。
POJ2796 Feel Good -- 单调队列的更多相关文章
- POJ2796 单调队列
Feel Good Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 8041 Accepted: 2177 Case Ti ...
- 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 单调队列是一种严格单调的队列,可以单调递增,也可以单调递减.队 ...
随机推荐
- a low memory warning should only destroy the layer’s bitmap
https://stablekernel.com/view-controller-in-ios-6/ Some of you may have noticed that your view contr ...
- 删除临时文件的bat文件
@echo offecho 正在清除系统垃圾文件,请稍等......del /f /s /q %systemdrive%\*.tmpdel /f /s /q %systemdrive%\*._mpde ...
- 记Tea使用中遇到的问题及解决过程
学习Markdown时,在小众软件看到一个叫Tea的软件.UI设计是简约风格:"所见即所得"的Markdown:支持插件等原因让我选择去尝试这杯"茶". 最近一 ...
- nginx反向代理与正向代理的区别
http://blog.csdn.net/m13666368773/article/details/8060481
- jeesite项目
1,登录页面.最高权限管理员 用户名:thinkgem 密码:admin 2,登陆之后展示的首页 3,分为不同的模块,由不同的入负责,我负责日志管理 已完成功能:模糊查询,分页,导入,导出Excel ...
- 关于小程序button控件上下边框的显示和隐藏问题
问题: 小程序的button控件上下有一条淡灰色的边框,在空件上加上了样式 border:(none/0); 都没办法让button上下的的边框隐藏: 代码如下 <button class=&q ...
- cf550D. Regular Bridge(构造)
题意 给出一个$k$,构造一个无向图,使得每个点的度数为$k$,且存在一个桥 Sol 神仙题 一篇写的非常好的博客:http://www.cnblogs.com/mangoyang/p/9302269 ...
- 判断Datable是否有数据
采用any()方法 检查表格的数据是否为空 var table = $('#example').DataTable(); if ( ! table.data().any() ) { alert( 'E ...
- 2D和3D效果
<style type="text/css"> #div1{ width: 200px; height: 200px; background-color:#aaa; c ...
- JZOJ 5344. 摘果子
Description Input Output Sample Input 7 9 39 6 13 2 22 6 7 4 -19 5 28 6 -17 1 2 1 3 2 4 1 5 4 6 2 7 ...