Feel Good
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 12409   Accepted: 3484
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

Source

 
题意: 给你n个数,求得 max(某个区间和*区间最小值)
 
题解:此代码只能通过poj测评 .... 坑啊
         求得以a[i]为最小值的区间的左界与右界  维护一个单调栈  代码中的*****
这个叫 滑窗算法?
滑动窗口的精髓是在一个数列里保存着一个递增数列,同时这个数列维持了它在原数列的位次递增,这个窗口里保存的第一个数即在这个区间里最小的数。这样不停得把新输入的数同这个滑动窗口里右边的数比较,如果比它大,删除窗口里的这个数,同时删除的数统治区域的最右边就是新输入的数,它的左统治区域即新输入的数的左统治区域,然后不停地向窗口的左边比。这样只需要一个数组记录它左边区域的边界就可以了,  右边同理..
 #include<iostream>
#include<cstring>
#include<cstdio>
#define ll long long
using namespace std;
ll n;
ll a[];
ll sum[];
ll l[],r[];
ll ans;
int gg;
int main()
{
gg=;
while(scanf("%lld",&n)!=EOF)
{
if(gg)
cout<<endl;
gg++;
sum[]=;
ans=-;
int flag=;
for(int i=;i<=n;i++)
{
scanf("%lld",&a[i]);
sum[i]=sum[i-]+a[i];
}
a[]=-;a[n+]=-;
l[]=;
for(int i=;i<=n;i++)//关键********
{
int temp=i-;
while(a[temp]>=a[i])//维护一个递增的序列
temp=l[temp]-;
l[i]=temp+;
}
r[n]=n;
for (int i=n-;i>=;i--)
{
int temp=i+;
while(a[temp]>=a[i])
temp=r[temp]+;
r[i]=temp-;
}
for(int i=;i<=n;i++)
{
ll ggg=(sum[r[i]]-sum[l[i]-])*a[i];
if(ggg>ans)
flag=i;
ans=max(ans,ggg);
}
printf("%lld\n",ans);
printf("%lld %lld\n",l[flag],r[flag]);
}
return ;
}

UVA 1619/POJ2796 滑窗算法/维护一个单调栈的更多相关文章

  1. bzoj 1012 维护一个单调数列

    Description 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作.语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值.限制:L不超过当前数列的长度. 2. ...

  2. HDU 5945 维护一个单调队列 dp

    Fxx and game Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Tot ...

  3. UVa 12265 (单调栈) Selling Land

    紫书上分析了很多很多,超详细,= ̄ω ̄= 每扫描一行可以计算一个height数组,表示从这块空地向上延伸多少块空地,而且这个数组可以逐行递推. 首先对于每一行来说维护一个单调栈,栈里放的是矩形的左上角 ...

  4. UVa 12265 贩卖土地 单调栈

    题意 输入一个\(n\times m\)的矩阵,每个格子可能是空地,也可能是沼泽.对于每个空地格子,求出以它为右下角的空矩形的最大周长,然后统计每个周长出现了多少次. 思路 对于 每一行 每两个沼泽之 ...

  5. HDU 5033 Building (维护单调栈)

    题目链接 题意:n个建筑物,Q条询问,问所在的位置,看到天空的角度是多少,每条询问的位置左右必定是有建筑物的. 思路 : 维护一个单调栈,将所有的建筑物和所有的人都放到一起开始算就行,每加入一个人,就 ...

  6. 紫书 例题8-19 UVa 12265 (扫描法+单调栈)

    首先可以用扫描法处理出一个height数组, 来保存从当前行开始, 每一个格子可以向上延伸的最大长度. 这种"延伸"的问题用扫描法, 因为往往这个时候可以利用前一次的结果来更新当前 ...

  7. c#滑窗缓存

    前言 在大数据时代,软件系统需要具备处理海量数据的能力,同时也更加依赖于系统强大的存储能力与数据响应能力.各种大数据的工具如雨后春笋般孕育而生,这对于系统来说是极大的利好.但在后端采用分布式.云存储和 ...

  8. (转)协议森林10 魔鬼细节 (TCP滑窗管理)

    协议森林10 魔鬼细节 (TCP滑窗管理) 作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 在TCP协议与"流" ...

  9. POJ2796 Feel Good 单调栈

    题意:给定一个序列,需要找出某个子序列S使得Min(a[i])*Σa[i] (i属于S序列)最大 正解:单调栈 这题的暴力还是很好想的,只需3分钟的事就可以码完,以每个点拓展即可,但这样的复杂度是O( ...

随机推荐

  1. Charles拦截请求

    一.通过Charles抓包,可拦截请求并篡改交互信息 1.可篡改客户端向服务器发起的请求信息(服务器收到的是假消息) 2.可篡改服务器返回给客户端的响应结果(客户端看到的是假消息) 二.篡改用户请求 ...

  2. 前端小记6——项目中常用的ES6方法

    现在很多功能用es5的方法也能实现功能,但es6提供的方法显得更为高效.记录下目前常用的几个方法. 1.字符包含 通过str.includes('a')来判断, 若str中包含a则结果为true,否则 ...

  3. Springboot端口设置

    application.properties 加入 server.port=80

  4. 2017年9月22日作业 c++算术运算符 自增 自减 逻辑运算符 位运算符 条件运算符(三元运算符)

    作业1: c++算术运算符试题,分析下面程序的输出结果是什么 //第一个: int x=8999;int value=x*1000/1000; //第二个 int x=8999;int value=x ...

  5. GVIM——简直美如画,有没有!

    "========================================== " Author: wklken " Version: 9.1 " Em ...

  6. 【思维题 最大权闭合子图】loj#6045. 「雅礼集训 2017 Day8」价

    又是经典模型的好题目 题目描述 人类智慧之神 zhangzj 最近有点胖,所以要减肥,他买了 NN 种减肥药,发现每种减肥药使用了若干种药材,总共正好有 NN 种不同的药材. 经过他的人脑实验,他发现 ...

  7. 重载&重写

    重载:同一个类中,方法名相同,方法参数不同(参数个数.参数类型),返回类型无关,所以返回类型不能作为重载的区别依据. 重写:子父类中,子类的方法名.参数位置.参数个数.返回类型和父类一致,方法体不同 ...

  8. Django之视图和URL配置

    1.在创建项目时,Django会自动创建URL配置,在urls.py文件中 文件的默认内容如下所示: """mysite URL Configuration The ur ...

  9. Mysql忘记密码找回步骤

    Mysql密码忘记找回步骤: 1.首先停止数据库 [root@localhost ~]# /etc/init.d/mysqld stop 2.使用--skip-grant-tables启动mysql, ...

  10. Nginx认证

    Nginx 的 ngx_http_auth_basic_module模块允许通过使用“HTTP基本认证”协议验证用户名和密码来限制对资源的访问. 配置举例: location / { auth_bas ...