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 ;
}
POJ 2796 / UVA 1619 Feel Good 扫描法的更多相关文章
- POJ 2796[UVA 1619] Feel Good
Feel Good Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 16786 Accepted: 4627 Case T ...
- 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 ...
- uva 1619 - Feel Good || poj 2796 单调栈
1619 - Feel Good Time limit: 3.000 seconds Bill is developing a new mathematical theory for human ...
- UVA 1619 Feel Good 感觉不错 (扫描法)
Feel Good Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Bill is deve ...
- UVA - 1619 Feel Good(扫描法)
题目: 思路: 预处理出a[i]在哪个范围区间内是最小的,然后直接遍历a数组求答案就可以了. 这个预处理的技巧巧妙的用了之前的处理结果.(大佬tql) 代码: #include <bits/st ...
- UVA 1619 Feel Good(DP)
Bill is developing a new mathematical theory for human emotions. His recent investigations are dedic ...
- POJ 1002 UVA 755 487--3279 电话排序 简单但不容易的水题
题意:给你许多串字符串,从中提取电话号码,输出出现复数次的电话号码及次数. 以下是我艰难的AC历程:(这题估计是我刷的题目题解次数排前的了...) 题目不是很难理解,刚开始想到用map,但stl的ma ...
- [poj 2796]单调栈
题目链接:http://poj.org/problem?id=2796 单调栈可以O(n)得到以每个位置为最小值,向左右最多扩展到哪里. #include<cstdio> #include ...
- POJ 1011 / UVA 307 Sticks
中文题 (一般都比较坑) 思路:DFS (感谢学长的幻灯片) 这破题把我折腾惨了!!!搞了n天 // by Sirius_Ren #include <cstdio> #include &l ...
随机推荐
- [codeforces contest 1119 F] Niyaz and Small Degrees 解题报告 (树形DP+堆)
interlinkage: http://codeforces.com/contest/1119/problem/F description: 有一颗$n$个节点的树,每条边有一个边权 对于一个$x$ ...
- js例子
1.子菜单下拉 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www ...
- Liunx搜索命令行
1.grep grep(General Regular Expression Parser,通用规则表达式分析程序)是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来. 它的使 ...
- lua math.random()
math.random([n [,m]]) 用法:1.无参调用,产生[0, 1)之间的浮点随机数. 2.一个参数n,产生[1, n]之间的整数. 3.两个参数,产生[n, m]之间的整数. math. ...
- 运行Tomcat闪退问题,报的错误:Unsupported major.minor version 51.0
在MyEclipse中运行tomcat,tomcat闪退并且报以下错误. java.lang.UnsupportedClassVersionError: org/apache/catalina/sta ...
- Python 中文注释报错解决方法
代码中一旦有了中文注释便会报错. 原因 如果文件里有非ASCII字符,需要在第一行或第二行指定编码声明. 解决方法 在第一行或是第二行加入这么一句# -- coding: utf-8 -- 完美解决
- Wireshark抓本地回环
最近正好要分析下本机两个端口之间通信状况.于是用wireshark抓包分析.对于本地回环要进行一些特殊的设置. 1.通过“运行”---“cmd” 输入“route add [本机IP]mask 255 ...
- SciPy和Numpy处理能力
1.SciPy和Numpy的处理能力: numpy的处理能力包括: a powerful N-dimensional array object N维数组: advanced array slicing ...
- 使用PCL::GPU::遇到问题
一:使用GPU进行点云分割,理论上可以极大地加快分割速度: 于是对PCL1.7.1进行了编译,回到32位系统,重设QT,编译成功(时间好漫长,一定要配置仔细,否则编译一次又一次浪费更多时间): 使用时 ...
- 关于 Windows 10 如何扩展分区与合并分区
前言 相信大部分人都遇见磁盘不够用的问题吧,然后都在后悔当初为什么就给 x 盘分了 10G 的容量吧. 不过没关系,自从 Windows 7 开始( xp 我也不知道有毛有),Windows 自带的磁 ...