您的位置:首页 > 其它

美丽的序列

2015-10-23 11:53 253 查看
题目背景 Background   GD是一个热衷于寻求美好事物的人,一天他拿到了一个美丽的序列。    题目描述 Description   为了研究这个序列的美丽程度,GD定义了一个序列的“美丽度”和“美丽系数”:对于这个序列的任意一个区间[l,r],这个区间的“美丽度”就是这个区间的长度与这个区间的最小值的乘积,而整个序列的“美丽系数”就是它的所有区间的“美丽度”的最大值。现在GD想要你帮忙计算这个序列的“美丽系数”。   输入输出格式 :Input/output   输入格式:   第一行一个整数n,代表序列中的元素个数。
  第二行n个整数a1、a2…an,描述这个序列。
输出格式:
  一行一个整数,代表这个序列的“美丽系数”。   输入输出样例 Sample input/output 样例测试点#1 输入样例:

3
1 2 3

输出样例:
4   说明 description 样例解释
   选取区间[2,3],可以获得最大“美丽系数”为2*2=4。
数据范围
   对于20%的数据,n<=2000;
   对于60%的数据,n<=200000;
   对于100%的数据,1<=n<=2000000,0<=ai<=2000000。
提示
   你可能需要一个读入优化。 题解   开两个单调队列,last[i]表示上一个比i小的值的位置,next[i]表示下一个比i小的值的位置,注意是小于,等于不算!!!
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long LL;
const LL inf=1e12;
inline LL read(){
LL x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
LL N,a[2000001];
LL last[2000001],next[2000001];
LL q1[2000001],pos1[2000001],h1,t1;
LL q2[2000001],pos2[2000001],h2,t2;
inline void calc1();
inline void calc2();
LL maxx;
int main(){
N=read();
for(int i=1;i<=N;i++) a[i]=read();
calc1();
calc2();
for(int i=1;i<=N;i++){
LL tmp=(next[i]-last[i]-1)*a[i];
if(maxx<tmp) maxx=tmp;
}
cout<<maxx;
return 0;
}
inline void calc1(){
q1[1]=a[1];pos1[1]=1; h1=t1=1;
int now=2;
while(now<=N){
while(t1>=h1&&q1[t1]>=a[now]){
t1--;
}
t1++;
q1[t1]=a[now];
pos1[t1]=now;
last[now]=pos1[t1-1];
now++;
}
}
inline void calc2(){
q2
=a
; pos2
=N; h2=t2=N,next
=N+1;
int now=N-1;
while(now>=1){
while(t2<=h2&&q2[t2]>=a[now]){
t2++;
}
t2--;
q2[t2]=a[now];
pos2[t2]=now;
next[now]=pos2[t2+1];
if(next[now]==0) next[now]=N+1;
now--;
}
}

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: