Feel Good(两遍单调栈维护区间+前缀和)
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 10 6 - 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
代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
const int maxn=1e5+5;
typedef long long ll;
using namespace std;
ll a[maxn];
ll sum[maxn];
ll L[maxn];
ll L2[maxn];
int main()
{
int n;ll mn=0x3f3f3f3f;
while(cin>>n)
{
for(int t=1;t<=n;t++)
{
scanf("%lld",&a[t]);
mn=min(mn,a[t]);
}
memset(sum,0,sizeof(sum));
for(int t=1;t<=n;t++)
{
sum[t]=sum[t-1]+a[t];
}
stack<int> S1,S2;
while(!S1.empty())
{
S1.pop();
}
while(!S2.empty())
{
S2.pop();
}
for(ll t=1;t<=n;t++)
{
while(S1.size() && a[S1.top()] >= a[t]) S1.pop();
if(S1.empty()) L[t] = 0;
else L[t] = S1.top();
S1.push(t);
}
for(ll t=n;t>=1;t--)
{
while(S2.size() && a[S2.top()] >= a[t]) S2.pop();
if(S2.empty()) L2[t] = n+1;
else L2[t] = S2.top();
S2.push(t);
}
ll maxnn=n*mn;//注意这个初始化,很细节
int j=1,k=n;
for(int t=1;t<=n;t++)
{
if((sum[L2[t]-1]-sum[L[t]])*a[t]>maxnn)
{
maxnn=(sum[L2[t]-1]-sum[L[t]])*a[t];
j=L[t]+1;
k=L2[t]-1;
}
}
cout<<maxnn<<endl;
cout<<j<<" "<<k<<endl;
}
return 0;
}
Feel Good(两遍单调栈维护区间+前缀和)的更多相关文章
- 【bzoj5089】最大连续子段和 分块+单调栈维护凸包
题目描述 给出一个长度为 n 的序列,要求支持如下两种操作: A l r x :将 [l,r] 区间内的所有数加上 x : Q l r : 询问 [l,r] 区间的最大连续子段和. 其中,一 ...
- bzoj1007: [HNOI2008]水平可见直线 单调栈维护凸壳
在xoy直角坐标平面上有n条直线L1,L2,...Ln,若在y值为正无穷大处往下看,能见到Li的某个子线段,则称Li为可见的,否则Li为被覆盖的.例如,对于直线:L1:y=x; L2:y=-x; L3 ...
- [CSP-S模拟测试]:A(单调栈维护凸包+二分答案)
题目传送门(内部题150) 输入格式 第一行两个整数$N,Q$. 接下来的$N$行,每行两个整数$a_i,b_i$. 接下来的$Q$行,每行一个整数$x$. 输出格式 对于每个询问,输出一行一个整数表 ...
- LOJ #2769 -「ROI 2017 Day 1」前往大都会(单调栈维护斜率优化)
LOJ 题面传送门 orz 斜率优化-- 模拟赛时被这题送走了,所以来写篇题解( 首先这个最短路的求法是 trivial 的,直接一遍 dijkstra 即可( 重点在于怎样求第二问.注意到这个第二问 ...
- CF1137E Train Car Selection(单调栈维护凸函数)
首先本题的关键是一次性加0操作只有第一个0是有用的.然后对于1 k操作,其实就是把之前的所有数删除.对于其他的情况,维护一次函数的和,将(i,a[i])看成平面上的一个点,用单调栈维护一下. #inc ...
- 翻转长方形 (不知名oj中一道个人私题)--单调栈维护最大子矩形
怎么分析这道题呢? 首先 ,我们注意到一点: 不管怎么操作,任意一个2*2方格中的 "#"个数的奇偶性是不变的. 所以,如果一个2*2方格中有奇数个"#",这个 ...
- Lost My Music:倍增实现可持久化单调栈维护凸包
题目就是求树上每个节点的所有祖先中(ci-cj)/(dj-di)的最小值. 那么就是(ci-cj)/(di-dj)的最大值了. 对于每一个点,它的(ci,di)都是二维坐标系里的一个点 要求的就是祖先 ...
- 【单调栈】【前缀和】【二分查找】8.28题解-long
long 题目描述 AP神牛准备给自己盖一座很华丽的宫殿.于是,他看中了一块N*M的矩形空地.空地中每个格子都有自己的海拔高度.AP想让他的宫殿的平均海拔在海平面之上(假设海平面的高度是0,平均数都会 ...
- Q - Queue HDU - 5493(树状树组维护区间前缀和 + 二分找预留空位)
Q - Queue HDU - 5493 Problem Description NNN people numbered from 1 to NNN are waiting in a bank for ...
随机推荐
- 使用Dom4j操作XML数据
--------------siwuxie095 dom4j 是一个非常优秀的 Java XML 的 API, 用来读写 XML 文件 和操作 ...
- SqlServer-truncate && delete && drop 的区别
有些人在删除表的所有记录的时候,喜欢这样来——不给DELETE 语句提供WHERE 子句,表中的所有记录都将被删除.但这种方法是不可取的,正确的应该使用 TRUNCATE TABLE tb_name ...
- IDEA小技巧:添加代码快捷方式
非常怀恋eclipse的的代码快捷方式tryc,今天给IDEA也添加了一个
- 类操作,removeClass&addClass
// 添加类 function addClass(node,className){ var reg=new RegExp("\\b"+classNa ...
- Linux下安装memcache PHP扩展
[root@centos memcache-2.2.4]# wget http://pecl.php.net/get/memcache-2.2.4.tgz [root@centos memcache- ...
- nmap 笔记
本文由阿德马翻译自国外网站,请尊重劳动成果,转载请注明出处,谢谢 1.初级用法: 教程 Nmap使用不同的技术来执行扫描,包括:TCP的connect()扫描,TCP反向的ident扫描,FTP反 ...
- Terminologies in MVC: Part 2 (Razor Engine Syntax vs Web Form)
By Abhishek Jaiswal :) on Mar 21, 2015 In this article we learn about Razor Engine Syntax vs Web For ...
- leetcode Submission Details
代码: #include<iostream> #include<vector> using namespace std; struct ListNode { int val; ...
- FreeMarker的实例通俗理解
1.把包lib/freemarker.jar拷贝到项目中 2. 在WEB-INF下新建文件夹templates 在templates下新建test.ftl文件 内容为: <html> ...
- Newtonsoft.Json.Linq
var json = "{\"name\":\"ok1\",\"sex\":\"man\"}"; / ...