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 ...
随机推荐
- LNMP搭建流程
参考张宴的nginx搭建流程. http://zyan.cc/nginx_php_v6/
- [原创]Laravel 的缓存源码解析
目录 前言 使用 源码 Cache Facade CacheManager Repository Store 前言 Laravel 支持多种缓存系统, 并提供了统一的api接口. (Laravel 5 ...
- C#-MVC-★不同层之间的传值方式★
方式一:form表单提交(一对一传值) 例如: view层代码: action - 数据提交到的服务端(如Home控制器下的Insert动作) method - 提交数据的方式(post或get) n ...
- Could not parse UiSelector argument: 'XXX' is not a string 错误解决办法
ebDriverWait(driver,20).until(EC.visibility_of_element_located((MobileBy.ANDROID_UIAUTOMATOR,new UiS ...
- Github概念理解备忘录
总结: add就是用来建立跟踪,添加文件到缓存区: commit就是把文件缓存区的文件正式加到本地库中: push就是把本地库更新到远程库中: git命令的操作要在仓库所在目录下进行才有效: 在Git ...
- JAVA编程思想第一章——对象导论
- Polycarp Restores Permutation
http://codeforces.com/contest/1141/problem/C一开始没想法暴力的,next_permutation(),TLE 后来看了这篇https://blog.csdn ...
- [转] 遇见 TiDB - 分布式关系数据库
[From] http://kuaibao.qq.com/s/20180510G0UFL000?refer=cp_1026 最近TiDB掀起了一波分布式数据库的热潮,公司也在着手准备TiDB的落地工作 ...
- 关于 maven 打包直接运行的 fat jar (uber jar) 时需要包含本地文件系统第三方 jar 文件的问题
关于maven打包fat jar (uber jar) 时需要包含本地文件系统第三方jar文件的问题,今天折腾了一整天.最后还是用了spring boot来做.下面是几篇关于打包的有参考价值的文章,以 ...
- (转)Mysql数据库主从心得整理
Mysql数据库主从心得整理 原文:http://blog.sae.sina.com.cn/archives/4666 管理mysql主从有2年多了,管理过200多组mysql主从,几乎涉及到各个版本 ...


