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
3 1 6 4 5 2

Sample Output

60
3 5

题意:

  给你n个数,让你找到一个区间l,r使得(a[l] + a[l+1] + ......... + a[r-1]+a[r]) *min{a[l].....a[r]} 的值最大

题解:

  我们预处理出对于每个i位置,以他为最小值时 向左向右能延伸的最远位值

  答案就是扫一遍了,看看以哪个位置为最小的答案最大

  在POJ上交比较好

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include<vector>
#include<map>
#include<queue>
using namespace std;
const int N = 1e5+, M = , mod = , inf = 1e9+; typedef long long ll;
const ll mm = 1e14;
int n;
ll a[N],l[N],r[N];
ll sum[N];
int main() {
int T = ;
while(scanf("%d",&n)!=EOF) {
if(T) cout<<endl; T++;
sum[] = ;
ll mi = mm;
for(int i=;i<=n;i++) scanf("%lld",&a[i]) ,sum[i] = sum[i-] + a[i] ,mi = min(mi,a[i]);
l[] = ;a[] = -;a[n+] = -;
for(int i=;i<=n;i++) {
int tmp = i-;
while(a[i]<=a[tmp]) tmp = l[tmp]-;
l[i] = tmp+;
}
r[n] = n;
for(int i=n-;i>=;i--) {
int tmp = i+;
while(a[i]<=a[tmp]) tmp = r[tmp] + ;
r[i] = tmp - ;
}
ll L = ,R = n;
ll mx = sum[n]*mi;
for(int i=;i<=n;i++) {
ll now = (sum[r[i]] - sum[l[i]-])*1ll*a[i];
if(now>mx) {
L = l[i]; R = r[i];
mx = now;
}
}
printf("%lld\n",mx);
printf("%lld %lld\n",L,R);
} return ;
}

最新文章

  1. 关于Python脚本开头两行的:#!/usr/bin/python和# -*- coding: utf-8 -*-的作用 – 指定文件编码类型
  2. [Shell]Bash变量:环境变量的配置文件和登录信息
  3. Android读书笔记0-从零开始
  4. iOS 学习笔记 六 (2015.03.28)常见错误
  5. HDU 4638 Group 树状数组 + 思路
  6. Eat the Trees hdu 1693
  7. fd_set 用法
  8. Visual Studio使用技巧
  9. 转账示例(二):service层面实现(本例采用QueryRunner来执行sql语句,数据源为C3P0)
  10. react学习01
  11. 完美解决IE渲染方式进入兼容模式问题
  12. Linux学习笔记(一):常用命令(1)
  13. iOS开发基础-图片切换(2)之懒加载
  14. mobile_像素
  15. Python socket粘包解决
  16. Spark Word2Vec算法代码实现
  17. No package的问题解决
  18. 容错处理库Polly使用文档
  19. get与post区别大揭秘
  20. ext3 转 ext4 操作

热门文章

  1. 如何修改 WordPress 数据库前缀
  2. iOS中容易混淆的知识点(持续更新中)
  3. 【Oracle】DG中 Switchover 主、备切换
  4. RAP开发入门-布局管理
  5. nyoj___大数阶乘
  6. VS2012 +PTVS配置
  7. 实验7 OpenGL光照
  8. spring注解略解
  9. APICloud开发小技巧(一)
  10. Java的编译与运行