首页 > 其他分享 >单调栈--Feel Good

单调栈--Feel Good

时间:2022-10-26 20:41:34浏览次数:71  
标签:life Good -- Bill period value st Feel int


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


#include<iostream>
#include<stack>
#include<vector>

using namespace std;
typedef long long ll;

const int N=100005;
int n,L[N],R[N];
ll s[N],a[N];
stack<ll>st;

signed main()
{
while(scanf("%d",&n)!=EOF){
s[0]=0;
while(!st.empty())st.pop();
for(int i = 1;i <= n;i ++)
{
scanf("%lld",&a[i]);s[i]=s[i-1]+a[i];
}
for(int i=1;i<=n;i++){
while(!st.empty()&&a[st.top()]>=a[i])st.pop();
if(st.empty())L[i]=0;
else L[i]=st.top();
st.push(i);
}
while(!st.empty())st.pop();
for(int i=n;i>=1;i--){
while(!st.empty()&&a[st.top()]>=a[i])st.pop();
if(st.empty())R[i]=n;
else R[i]=st.top()-1;
st.push(i);
}
ll res=0;
int k;
for(int i = 1;i <= n;i ++){
ll tmp=(ll)a[i]*(s[R[i]]-s[L[i]]);
if(tmp>=res){
res=tmp;
k=i;
}
}
printf("%lld\n%d %d\n",res,L[k]+1,R[k]);
}
return 0;
}

 

标签:life,Good,--,Bill,period,value,st,Feel,int
From: https://blog.51cto.com/u_13121994/5798367

相关文章

  • 并查集--翻译机的个数(顺丰2020年笔试)
    某学术会议上,一共有n个人参加,现在已知每个人会的语言(一个人可能不会任何语言)。现在有一种学习机,每一个学习机可以在会议期间使一个人暂时掌握一种自己不会的语言,问要使得任......
  • 最长非递减子序列--顺丰2020校招笔试题
    n的范围是[0,100000]DP版本(O(n^2))时间复杂度(LTE):#include<cstdio>#include<iostream>#include<algorithm>usingnamespacestd;#defineN100intmain(){intA[N],dp[N......
  • 字符串--移除k个数使得剩下的数最大
    有一十进制正整数,移除其中的K个数,使剩下的数字是所有可能中最大的。假设:字符串的长度一定大于等于K字符串不会以0开头 输入描述:一行由正整数组成的数字字符串,和......
  • DP--爬楼梯1
    题目描述前几个月放映的头号玩家简直火得不能再火了,作为一个探索终极AI的研究人员,月神自然去看了此神剧。由于太过兴奋,晚上月神做了一个奇怪的梦,月神梦见自己掉入了一个被施......
  • DP--字符串变换
    给定两个字符串,已知可以使用三种方式进行变换1.插入一个字符2.删除一个字符3.更改一个字符请设计一个算法,找到两个字符串之间的经历几次最小变换,可以字符串1转换成字......
  • DFS--同一个方向找出所有子字符串的个数
     字符迷阵是一种经典的智力游戏。玩家需要在给定的矩形的字符迷阵中寻找特定的单词。在这题的规则中,单词是如下规定的:1.在字符迷阵中选取一个字符作为单词的开头;2.选取......
  • map记录下标
    题目描述小云正在参与开发一个即时聊天工具,他负责其中的会话列表部分。会话列表为显示为一个从上到下的多行控件,其中每一行表示一个会话,每一个会话都可以以一个唯一正整数id......
  • DFS(全排列)--相同序号不能相邻
     小多想在美化一下自己的庄园。他的庄园毗邻一条小河,他希望在河边种一排树,共M棵。小多采购了N个品种的树,每个品种的数量是Ai(树的总数量恰好为M)。但是他希望任意......
  • N-叉树--遍历N-叉树所有从顶点到叶子节点的路径
    Shopee的OrangeDayShopee每个月都有属于大家的节日,每到这个时候,大家都会穿着橙色的T恤,吃着水果蛋糕,做着游戏。瞧,今天又是OrangeDay了,吃货小虾同学早早的来到现场,看看有没......
  • N-叉树--给定顶点求N叉树的最大深度
    #include<iostream>#include<vector>usingnamespacestd;vector<int>Vec[100005];intResult;voiddfs(intChild,intParent,intPathLength){for(inti=0;i<Vec......