POJ2796 Feel Good -- 单调队列
2024-09-01 14:24:44
Feel Good
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 14489 | Accepted: 4015 | |
Case Time Limit: 1000MS | Special Judge |
Description
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 106 - 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 题意:
给出一个数列,求其一个区间中 最小值 与 区间元素和 的乘积的最大值;
分析:
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 ;
}
个人随笔,望大佬勿喷,若能提供帮助不胜荣幸。
最新文章
- NYOJ 187
- 自动化测试selenium----css选择器总结
- 8. javacript高级程序设计-BOM
- Java并发学习之十九——线程同步工具之Phaser
- 2038: [2009国家集训队]小Z的袜子(hose) - BZOJ
- 查询可用的Nuget服务地址
- js中调用mangeto的js翻译
- 2015年十大热门Android开源新项目
- js脚本中try与cache捕获异常处理
- Spring集成Redis缓存
- js 移动端上拉加载下一页通用方案
- linux上创建svn服务器(centos7.3)
- redis_字符串对象
- intellj idea 跑覆盖率
- The stacking context
- sap 调试工具,修改变量值
- 判断窗体 show完成
- SpingBoot:Unregistering JMX-exposed beans on shutdown
- hadoop输出lzo文件并添加索引
- ubuntu查询命令行安装的软件的安装路径
热门文章
- bzoj1189 [HNOI2007]紧急疏散
- 【BZOJ3994】[SDOI2015] 约数个数和(莫比乌斯反演)
- Json的本地写入和读取,也可以方便在开发中数据的调试
- 使用OpenFileDialog组件打开对话框
- js当中mouseover和mouseout多次触发(非冒泡)
- 5- vue django restful framework 打造生鲜超市 -完成商品列表页(上)
- ECMAScript 6入门扩展笔记
- JZOJ 5184. 【NOIP2017提高组模拟6.29】Gift
- JZOJ 3509. 【NOIP2013模拟11.5B组】倒霉的小C
- 09.VUE学习之watch监听属性变化实现类百度搜索栏功能ajax异步请求数据,返回字符串