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