美丽的序列
2015-10-23 11:53
253 查看
题目背景 Background
GD是一个热衷于寻求美好事物的人,一天他拿到了一个美丽的序列。
题目描述 Description
为了研究这个序列的美丽程度,GD定义了一个序列的“美丽度”和“美丽系数”:对于这个序列的任意一个区间[l,r],这个区间的“美丽度”就是这个区间的长度与这个区间的最小值的乘积,而整个序列的“美丽系数”就是它的所有区间的“美丽度”的最大值。现在GD想要你帮忙计算这个序列的“美丽系数”。
输入输出格式 :Input/output
输入格式:
第一行一个整数n,代表序列中的元素个数。
第二行n个整数a1、a2…an,描述这个序列。
输出格式:
一行一个整数,代表这个序列的“美丽系数”。 输入输出样例 Sample input/output 样例测试点#1 输入样例:
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小的值的位置,注意是小于,等于不算!!!
第二行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--; } }
相关文章推荐
- POJ 3974 - Palindrome(manacher)
- 横向滚动条并且隐藏竖向滚动条
- html简介
- springmvc 之 Controller
- MySQL存储引擎(storage engine)
- 推荐一个能跑的Android4.4 能跑的Android GPS HAL驱动源码
- leetcode: Rotate List
- SharedPreferences 详解
- Android Studio中常用设置与快捷键,自用
- Java数据类型中String、Integer、int相互间的转换
- 38元天价虾背后的移动互联网革命
- 快排分区函数--进军硅谷
- springmvc 之 SpringMVC视图解析器
- 将远程图片抓取到本地
- OneAPM 技术公开课:北京,北京!
- PostgreSQL数据库 OLTP高并发请求性能优化
- Deferred 异步模型
- Facebook内部高效工作PPT指南
- myeclipse 修改 svn用户名
- SIGPIPE信号详解