UVA 1619 Feel Good 感觉不错 (扫描法)
Feel Good
Time Limit: 3000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
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 day was. 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 input will contain several test cases, each of them as described below. Consecutive test cases are separated by a single blank line.
The first line of the input file contains n<tex2html_verbatim_mark> - the number of days of Bill's life he is planning to investigate (1n100000)<tex2html_verbatim_mark> . The rest of the file contains n<tex2html_verbatim_mark> integer numbers a1, a2,..., an<tex2html_verbatim_mark> ranging from 0 to 106<tex2html_verbatim_mark> - the emotional values of the days. Numbers are separated by spaces and/or line breaks.
Output
For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.
On the first line of the output file print the greatest value of some period of Bill's life.
On the second line print two numbers l<tex2html_verbatim_mark> and r<tex2html_verbatim_mark> such that the period from l<tex2html_verbatim_mark> -th to r<tex2html_verbatim_mark> -th day of Bill's life (inclusive) has the greatest possible value. If there are multiple periods with the greatest possible value, then print the shortest one. If there are still several possibilities, print the one that occurs first..
Sample Input
6
3 1 6 4 5 2
Sample Output
60
3 5
对于某个最小值ai来说,所选的区间应该尽量大,直到再选就不能保证ai是最小值的时候停止。
在扫描过程中维护一个向前延伸的最大位置,扩展的时候注意传递性,如果前面一个元素比它小,那么前面一个元素能延伸到的位置,
当前元素也可以延伸到,然后类似链表往前找的同时延伸链即可。向后找的时候类似。区间和用一个前缀和来处理。
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+;
typedef long long ll;
int a[maxn],l[maxn],r[maxn],n;
ll sum[maxn]; int main()
{
int T = ; sum[] = ; a[] = -;
while(~scanf("%d",&n)){
if(T++) putchar('\n');
a[n+] = -;
for(int i = ; i <= n; i++)
scanf("%d",a+i),sum[i] = sum[i-]+a[i],l[i]=r[i]=i; for(int i = ; i <= n; i++){
while(a[i]<=a[l[i]-]) l[i] = l[l[i]-];
} for(int i = n; i >= ; i--){
while(a[i]<=a[r[i]+]) r[i] = r[r[i]+];
}
ll Max = (ll)a[]*a[];
int L = ,R = ;
for(int i = ; i <= n; i++){
ll tmp = (sum[r[i]]-sum[l[i]-])*a[i];
if(tmp > Max || (tmp == Max && R - L > r[i] - l[i] )){
Max = tmp;
L = l[i]; R = r[i];
}
}
printf("%lld\n%d %d\n",Max,L,R);
}
return ;
}
最新文章
- 【.net 深呼吸】细说CodeDom(6):方法参数
- AFNetworking 3.0 源码解读(十一)之 UIButton/UIProgressView/UIWebView + AFNetworking
- 偶遇this之坑
- HDU 3584 Cube (三维 树状数组)
- hosts manager——hosts配置管理工具
- maven操作
- datatable绑定comboBox显示数据[C#]
- 启动hbase时出现HMaster Aborted错误
- javascript雪花效果 注释版
- [terry笔记]Flashback
- 批量安装操作系统之cobbler
- 数据库的优化tips
- 使用@class和#import的细节问题
- ASP.NET Core + Angular 2 Template for Visual Studio
- [Swift]LeetCode893. 特殊等价字符串组 | Groups of Special-Equivalent Strings
- CSS盒模型深入理解
- python笔记1——关于文件的打开与读写
- 四、Python-元组
- 获取请求的ip工具类
- [android] soundpool简介
热门文章
- Json.net的常用语句JsonConvert.SerializeObject(对象)
- MVC用户验证
- MATLAB实现回归分析
- openinstall的免费服务对App推广有哪些帮助?
- Java基础--常用API--字符串相关API
- Maven入门 项目的生命周期&;pom.xml配置&;仓库
- Codeforces 161D(树形dp)
- response.setContentType() 作用及参数用法
- NET Core &; Entity Framework Core
- 《深入理解java虚拟机》笔记(8)类的加载机制