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

 
 
题意 
求任意一个区间,它的区间和乘上区间内最小值后的数值是最大的。
 
分析 
先维护前缀和,再求出以每个元素为最小值的区间范围,然后计算取最大值即可。求以每个元素为最小值的区间范围可以使用单调栈来实现。每次入栈,都与栈顶元素比较,我们要维护一个从栈底到栈顶,从小到大的栈,若栈顶元素小于当前元素,则入栈;否则,把栈里小于当前元素的项一一出栈并计算对应的值,因为此时相对于已经发现了第一个比栈里某些元素小的值,所以那些项不可作为新区间的最小值了,也就是找到作为最小值的右边界了,同时,按照栈里的顺序就知道了左边界,这样以每个元素为最小值的区间范围就求出来了,答案便是所有的最大值了。
另外,uva上这题很坑啊,死活都是wa,最后用了一种比较巧妙的方法,即暴力作区间的标记。建议在poj上提交代码
 
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include <queue>
#include <vector>
#include<bitset>
#include<map>
using namespace std;
typedef long long LL;
const int maxn = 1e5+;
const int mod = +;
typedef pair<int,int> pii;
#define X first
#define Y second
#define pb push_back
#define mp make_pair
#define ms(a,b) memset(a,b,sizeof(a))
const int inf = 0x3f3f3f3f;
#define lson l,m,2*rt
#define rson m+1,r,2*rt+1
int a[maxn],sta[maxn];
LL sum[maxn];
int lef[maxn];
int main(){
// freopen("in.txt","r",stdin);
int n;
while(~scanf("%d",&n)){
ms(sum,); int top=-;
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
sum[i]=sum[i-]+a[i];
}
a[n+]=-;
LL ans=-;
int L,R;
for(int i=;i<=n+;i++){
// cout<<a[i]<<endl;
if(a[i]>sta[top] || top==-){
sta[++top]=a[i];
lef[top]=i;
// printf("%d,(%d,%d)\n",a[i],i,i);
}else if(a[i]<sta[top]){
int ll;
while(a[i]<sta[top]&&top>=){
ll=lef[top];
LL res=(sum[i-]-sum[lef[top]-])*sta[top]*1LL;
if(res > ans){
ans=res;
L=lef[top],R=i-;
}
top--;
}
sta[++top]=a[i];
lef[top]=ll;
// printf("%d,(%d,%d)\n",a[i],ll,i);
}
}
cout<<ans<<endl;
cout<<L<<" "<<R<<endl;
}
return ;
}

uva上能通过的代码

#include<cstdio>
#include<cstring>
#define maxn 1000010
long long sum[maxn];
int num[maxn], l[maxn], r[maxn], n;
void init() {
sum[] = ;
memset(num,-,sizeof(num));
for(int i = ; i <= n; i++) {
scanf("%d", &num[i]);
sum[i] = sum[i-] + num[i];
l[i] = i;
r[i] = i;
} for(int i = ; i <= n; i++) {
while(num[l[i] - ] >= num[i])
l[i] = l[l[i] - ];
}
for(int i = n; i >= ; i--) {
while(num[r[i] + ] >= num[i])
r[i] = r[r[i] + ];
}
} void solve() {
long long Max = ;
int ll = , rr = ;
long long s;
for(int i = ; i <= n; i++) {
s = num[i] * (sum[r[i]] - sum[l[i] - ]);
if(s > Max || (s == Max && rr - ll > r[i] - l[i])) {
Max = s;
ll = l[i];
rr = r[i];
}
}
printf("%lld\n%d %d\n", Max, ll, rr);
} int main() {
int cas = ;
while(scanf("%d", &n) != EOF) {
if(cas)
printf("\n");
cas++;
init();
solve();
}
return ;
}
 
 

POJ 2796[UVA 1619] Feel Good的更多相关文章

  1. POJ 2796 / UVA 1619 Feel Good 扫描法

    Feel Good   Description Bill is developing a new mathematical theory for human emotions. His recent ...

  2. POJ 3525/UVA 1396 Most Distant Point from the Sea(二分+半平面交)

    Description The main land of Japan called Honshu is an island surrounded by the sea. In such an isla ...

  3. uva 1619 - Feel Good || poj 2796 单调栈

    1619 - Feel Good Time limit: 3.000 seconds   Bill is developing a new mathematical theory for human ...

  4. UVA 1619 Feel Good(DP)

    Bill is developing a new mathematical theory for human emotions. His recent investigations are dedic ...

  5. POJ 1002 UVA 755 487--3279 电话排序 简单但不容易的水题

    题意:给你许多串字符串,从中提取电话号码,输出出现复数次的电话号码及次数. 以下是我艰难的AC历程:(这题估计是我刷的题目题解次数排前的了...) 题目不是很难理解,刚开始想到用map,但stl的ma ...

  6. [poj 2796]单调栈

    题目链接:http://poj.org/problem?id=2796 单调栈可以O(n)得到以每个位置为最小值,向左右最多扩展到哪里. #include<cstdio> #include ...

  7. POJ 1011 / UVA 307 Sticks

    中文题 (一般都比较坑) 思路:DFS (感谢学长的幻灯片) 这破题把我折腾惨了!!!搞了n天 // by Sirius_Ren #include <cstdio> #include &l ...

  8. ZOJ 1914 Arctic Network (POJ 2349 UVA 10369) MST

    ZOJhttp://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1914 POJhttp://poj.org/problem?id=23 ...

  9. POJ 2796:Feel Good(单调栈)

    http://poj.org/problem?id=2796 题意:给出n个数,问一个区间里面最小的元素*这个区间元素的和的最大值是多少. 思路:只想到了O(n^2)的做法. 参考了http://ww ...

随机推荐

  1. Java使用HTTPClient4.3开发的公众平台消息模板的推送功能

    代码引用,参考文章:http://www.cnblogs.com/feiyun126/p/4778556.html,表示感谢! package com.yuanchuangyun.cyb.manage ...

  2. [转帖]台积电近10万片晶圆报废,但7nm工艺将成2019营收主力

    台积电近10万片晶圆报废,但7nm工艺将成2019营收主力 2019年02月18日 13:19 1784 次阅读 稿源:Expreview超能网 0 条评论 https://www.cnbeta.co ...

  3. Angular $scope和$rootScope

    <!DOCTYPE html><html ng-app='myModule'><head lang="en"> <meta charset ...

  4. idea编译器光标变为insert状态

    idea鼠标变成inset状态,不能复制.粘贴使用快捷键 1.打开设置 点击 plugins 输入ideavim  把 这个勾去掉!这个是插件的配置问题. 2.如果上面的不管用,那么检查editor- ...

  5. python之hasattr、getattr和setattr函数

    hasattr函数使用方法 # hasattr函数使用方法 # hasattr(object,attr) # 判断一个对象里是否有某个属性或方法,返回布尔值,有为True,否则False class ...

  6. selenium之批量执行测试用例生成HTML结果文件

    使用HTMLTestRunner运行测试套件,自动生成html测试报告: import unittest, HTMLTestRunner, sendmail_html import time, os ...

  7. MT【13】三角函数求范围

    解答:AB显然正确,C中$a$取0时,解为三个,C 错误.我们主要看一下D 评:这里提供了一个处理$sin^2xcosx$的常见方法:平方,单变量后用算术几何不等式.

  8. 自学工业控制网络之路1.2-典型的现场总线介绍PROFIBUS

    返回 自学工业控制网络之路 自学工业控制网络之路1.2-典型的现场总线介绍PROFIBUS 目前看来,现场总线标准不会统一,多标准并存现象将会持续. 现场总线国家标准: 中国的DeviceNet和AS ...

  9. 2019.3.28&2019.3.30考试

    2019.3.28 : 肥肠爆芡,因为这场考试的题太屑了,所以我咕咕了 Upd on 2019.3.30 压进来一篇(因为都没啥意义) 2019.3.30 : 全机房读错题+没有大样例=T2全体爆炸 ...

  10. 转载:C++类内存分布

    本文转自:http://www.cnblogs.com/jerry19880126/p/3616999.html,原文写的非常好,从中学到了虚继承的概念,也学会了用VS查看内存分布. 说下C++内存分 ...