poj 2796 Feel Good 单调队列
|
Feel Good
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 Sample Output 60 Source |
[Submit] [Go Back] [Status] [Discuss]
/*
题意:10^5个数字,求max { 区间[ ] * 该区间最小值 };
解决思路:
找区间最小值,如果能够求出其长度,那么就可以扫一遍解决。
关键在于找区间最小值。
而且存在一种情况,同一个值,会是许多区间的最小值。
所以,转化一下,求每一个i为最小值的时候,能到到达的最远位置。 */ #include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
using namespace std; int a[],b[];
__int64 s[];
int L[],R[];
typedef struct
{
int num;
int rp;
}Queue;
Queue q[],tmp; int main()
{
int n,i;
int head,tail,Num,l,r,lx,rx;
__int64 hxl,tom;
while(scanf("%d",&n)>)
{
for(i=,s[]=;i<=n;i++)
{
scanf("%d",&a[i]);
b[n-i+]=a[i];
s[i]=s[i-]+a[i];
}
memset(L,-,sizeof(L));
memset(R,-,sizeof(R));
head=;tail=-;
for(i=;i<=n;i++)
{
tmp.num=i;
tmp.rp=a[i];
while( head<=tail && q[tail].rp>tmp.rp )
{
if( R[q[tail].num]==-)
{
R[q[tail].num]= i - q[tail].num-;
}
tail--;
}
q[++tail]=tmp;
}
while( head<=tail )
{
Num=q[tail].num-q[head].num;
if( R[q[head].num]==-)
{
R[q[head].num]=Num;
}
head++;
}
head=;tail=-;
for(i=;i<=n;i++)
{
tmp.num=i;
tmp.rp=b[i];
while( head<=tail && q[tail].rp>tmp.rp )
{
Num=n-q[tail].num+;
if( L[Num]==-)
L[Num]= i - q[tail].num-;
tail--;
}
q[++tail]=tmp;
}
while( head<=tail )
{
Num=n-q[head].num+;
if( L[Num]==-)
{
L[Num]=q[tail].num-q[head].num;
}
head++;
}
for(tom=-,i=;i<=n;i++)
{
l=i-L[i];
r=i+R[i];
hxl=(s[r]-s[l-])*a[i];
if( hxl>tom)
{
tom=hxl;
lx=l;
rx=r;
}
}
printf("%I64d\n%d %d\n",tom,lx,rx);
}
return ;
}
poj 2796 Feel Good 单调队列的更多相关文章
- POJ 2823 Sliding Window + 单调队列
一.概念介绍 1. 双端队列 双端队列是一种线性表,是一种特殊的队列,遵守先进先出的原则.双端队列支持以下4种操作: (1) 从队首删除 (2) 从队尾删除 (3) 从队尾插入 (4) ...
- poj 2823 Sliding Window (单调队列入门)
/***************************************************************** 题目: Sliding Window(poj 2823) 链接: ...
- poj 1821 Fence(单调队列优化DP)
poj 1821 Fence \(solution:\) 这道题因为每一个粉刷的人都有一块"必刷的木板",所以可以预见我们的最终方案里的粉刷匠一定是按其必刷的木板的顺序排列的.这就 ...
- POJ 2823 滑动窗口 单调队列模板
我们从最简单的问题开始: 给定一个长度为N的整数数列a(i),i=0,1,...,N-1和窗长度k. 要求: f(i) = max{a(i-k+1),a(i-k+2),..., a(i)},i = 0 ...
- POJ 3709 K-Anonymous Sequence (单调队列优化)
题意:给定一个不下降数列,一个K,将数列分成若干段,每段的数字个数不小于K,每段的代价是这段内每个数字减去这段中最小数字之和.求一种分法使得总代价最小? 思路:F[i]表示到i的最小代价.f[i]=m ...
- POJ 2823 滑动窗口 单调队列
https://vjudge.net/problem/POJ-2823 中文:https://loj.ac/problem/10175 题目 给一个长度为 $N$ 的数组,一个长为 $K$ 的滑动窗体 ...
- POJ 2823 Sliding Window (单调队列)
单调队列 加了读入挂比不加更慢.... 而且这份代码要交c++ 有大神G++跑了700ms..... orzorzorz #include<iostream> #include<cs ...
- poj 3162 树DP+单调队列
http://acm.hust.edu.cn/vjudge/problem/11552 http://blog.csdn.net/woshi250hua/article/details/7727677 ...
- poj 2823 Sliding Windows (单调队列+输入输出挂)
Sliding Window Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 73426 Accepted: 20849 ...
随机推荐
- LOJ#3083. 「GXOI / GZOI2019」与或和(单调栈)
题面 传送门 题解 按位考虑贡献,如果\(mp[i][j]\)这一位为\(1\)就设为\(1\)否则设为\(0\),对\(or\)的贡献就是全为\(1\)的子矩阵个数,对\(and\)的贡献就是总矩阵 ...
- ps与grep组合命令使用
管道命令 我们在做运维的时候,经常会使用这个命令ps -ef | grep nginx. ps -ef 表示显示所有进程的消息. | 是管道命令.通常需要借助管道命令”|”多个命令的组合,形式如下: ...
- C++实现二叉树的相应操作
1. 二叉树的遍历:先序(递归.非递归),中序(递归.非递归),后序(递归.非递归). #include <iostream> #include <string> #inclu ...
- iOS ---进阶之摇一摇
1.摇一摇的原理分析 1)在摇动手机时会产生一个动画,界面的图片会在中间分开分别进行向上.向下的位置移动. 分析:此过程就是在主屏幕上设置两个imageView,在开始摇动的方法中对这两个imageV ...
- java 实现七大基本排序算法
一. 选择排序 /** * 选择排序: int arr[] = { 5, 6, 2, 7, 8, 6, 4 }; * * 第0趟 5 2 6 7 6 4 8 第1趟 2 5 6 6 4 7 8 第2趟 ...
- 2016级算法期末上机-A.简单·Bamboo's Fight with DDLs I
简单·Bamboo's Fight with DDLs I 分析 一句话:要装满的完全背包问题. 对比完全背包只有一点要改变:初始化为负无穷 传送门: https://buaacoding.cn/pr ...
- Spring注入方式(1)
Spring支持3种依赖注入方式,分别为属性注入.构造器注入和工厂方法注入(很少使用,不推荐),下面分别对属性注入和构造器注入详细讲解. 1.常量注入 属性注入是通过setter方法注入Bean的属性 ...
- js img转换base64
方法一:canvas <script type="text/javascript"> function getBase64Image(img) { var canvas ...
- Mac OS 10.12后Caps lock(大写键)无法使用的解决办法
▲打开设置中的键盘选项,并切换至输入源选项标签, ▲取消勾选“使用大写锁定键来回切换“美国英文””, ▲这时再按下Caps lock即可正常使用大小写切换. ▲Update:目前macOS 10.12 ...
- monitorenter与monitorexit指令


