luogu P2659 美丽的序列
题目背景
GD是一个热衷于寻求美好事物的人,一天他拿到了一个美丽的序列。
题目描述
为了研究这个序列的美丽程度,GD定义了一个序列的“美丽度”和“美丽系数”:对于这个序列的任意一个区间[l,r],这个区间的“美丽度”就是这个区间的长度与这个区间的最小值的乘积,而整个序列的“美丽系数”就是它的所有区间的“美丽度”的最大值。现在GD想要你帮忙计算这个序列的“美丽系数”。
输入输出格式
输入格式:
第一行一个整数n,代表序列中的元素个数。 第二行n个整数a1、a2„an,描述这个序列。
输出格式:
一行一个整数,代表这个序列的“美丽系数”。
输入输出样例
说明
样例解释 选取区间[2,3],可以获得最大“美丽系数”为2*2=4。 数据范围 对于20%的数据,n<=2000; 对于60%的数据,n<=200000; 对于100%的数据,1<=n<=2000000,0<=ai<=2000000。 提示 你可能需要一个读入优化。
用线段树+维护区间最小值,构造一颗笛卡尔树+卡时可以过
#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;
#define LL long long
inline int read() {
int x=;
char c=getchar();
while(c<''||c>'')c=getchar();
while(c<=''&&c>='')x=x*+c-'',c=getchar();return x;
}
const int INF = 0x7fffffff;
const int maxn = ;
int a[maxn];
int n;LL ans=;
struct node{
int w,pos;
}tree[maxn<<];
inline void update(int rt) {
tree[rt].pos=tree[rt<<].w < tree[rt<<|].w ? tree[rt<<].pos :tree[rt<<|].pos;
tree[rt].w=min(tree[rt<<].w,tree[rt<<|].w);
}
void build(int l,int r,int rt) {
if(l==r) {
tree[rt].w=a[l]=read();tree[rt].pos=l;return;
}
int mid=(l+r)>>;
build(l,mid,rt<<);
build(mid+,r,rt<<|);
update(rt);
}
int query(int l,int r,int rt,int tl,int tr) {
if(tl<=l&&tr>=r) return tree[rt].pos;
int a1,m=0x7fffffff;
int mid=(l+r)>>;
if(tl<=mid){
int tmp=query(l,mid,rt<<,tl,tr);
if(m>a[tmp])m=a[tmp],a1=tmp;
}
if(tr>mid) {
int tmp=query(mid+,r,rt<<|,tl,tr);
if(m>a[tmp])m=tree[tmp].w,a1=tmp;
}
return a1;
}
int cnt=;
void dfs(int p,int l,int r) {
++cnt;
if(cnt==) {
printf("%lld\n",ans);exit();
}
if(l==r) {
ans=a[l]>ans?a[l]:ans;return;
}
ans=ans<(LL)(r-l+)*a[p]?(LL)(r-l+)*a[p]:ans;
if(p>l)dfs(query(,n,,l,p-),l,p-);
if(p<r)dfs(query(,n,,p+,r),p+,r);
}
int main() {
n=read();int minn=INF,pos;
build(,n,);
dfs(tree[].pos,,n);
printf("%lld\n",ans);
return ;
}
线段树
维护两个单调队列,求出当每个点为最小值是向左右扩展的最大距离
#include<cstdio>
#include<algorithm>
using namespace std;
#define LL long long
const int maxn= ;
#ifdef WIN32
#define lld "I64d"
#else
#define lld "lld"
#endif
inline int read() {
int x=;
char c=getchar();
while(c<''||c>'')c=getchar();
while(c<=''&&c>='')x=x*+c-'',c=getchar();return x;
}
int a[maxn],b[maxn];
int l[maxn],r[maxn],tmp[maxn],q[maxn];
int n,m;
void work(int c[] ,int d[]) {
q[]=c[]; tmp[]=;
int head=,tail=;
for(int i=;i<=n;++i) {
while(head<=tail&&q[tail]>c[i]) d[tmp[tail--]]=i-;
q[++tail]=c[i];
tmp[tail]=i;
}
while(head<=tail) d[tmp[head++]]=n;
}
int main() {
LL ans=;
n=read();
for(int i=;i<=n;++i)
a[i]=read(),b[n-i+]=a[i];
work(a,r);
work(b,l);
for(int i=;i<=n;++i) tmp[i]=l[i];
for(int i=;i<=n;++i) l[n-i+]=n-tmp[i]+;
for(int i=;i<=n;++i) ans=max(ans,1ll*a[i]*(r[i]-l[i]+));
printf("%lld\n",ans);
return ;
}
单调队列
luogu P2659 美丽的序列的更多相关文章
- 洛谷P2659 美丽的序列 单调栈模板
P2659 美丽的序列 题目链接 https://www.luogu.org/problemnew/show/P2659 题目描述 为了研究这个序列的美丽程度,GD定义了一个序列的"美丽度& ...
- P2659 美丽的序列
P2659 美丽的序列对于当前的最小值,找到最大的左右边界,然后更新答案.用单调队列确定左右边界,O(n)做法. #include<iostream> #include<cstdio ...
- 洛谷 P2659 美丽的序列 解题报告
P2659 美丽的序列 题目背景 GD是一个热衷于寻求美好事物的人,一天他拿到了一个美丽的序列. 题目描述 为了研究这个序列的美丽程度,GD定义了一个序列的"美丽度"和" ...
- 洛谷——P2659 美丽的序列
P2659 美丽的序列 单调栈维护区间最小值,单调递增栈维护区间最小值, 考虑当前数对答案的贡献,不断加入数,如果加入的数$>$栈顶,说明栈顶的元素对当前数所在区间是有贡献的,同时加入当前的数. ...
- 笛卡尔树-P2659 美丽的序列
P2659 美丽的序列 tag 笛卡尔树 题意 找出一个序列的所有子段中子段长度乘段内元素最小值的最大值. 思路 我们需要找出所有子段中贡献最大的,并且一个子段的贡献为其长度乘区间最小值. 这--不就 ...
- 洛谷P2659 美丽的序列
题目 该题目可以用辅助数组l[i], r[i]来指向以data[i]为最小值的左端点和右端点.然后最后枚举每个data[i]寻找每个data[i]的美丽值的最大值. 然后辅助数组可以用单调栈求出. # ...
- P2659 美丽的序列 (单调栈)
题目链接 Solution 直接考虑单调栈处理出每一个点作为最小值的区间长度. 然后 \(O(n)\) 找一遍最大值即可. 记得开 long long,以及要注意 \(0\) 的问题. Code #i ...
- [Luogu 2023] AHOI2009 维护序列
[Luogu 2023] AHOI2009 维护序列 恕我冒昧这和线段树模板二有个琴梨区别? #include <cstdio> int n,m; long long p; class S ...
- [Luogu 1963] NOI2009 变换序列
[Luogu 1963] NOI2009 变换序列 先%Dalao's Blog 什么?二分图匹配?这个确定可以建图? 「没有建不成图的图论题,只有你想不出的建模方法.」 建图相当玄学,不过理解大约也 ...
随机推荐
- 洛谷 P1032 字串变换 (BFS)
题目传送门 我即使是死了,钉在棺材里了,也要在墓里,用这腐朽的声带喊出 STL大法好 这题最麻烦的其实是处理字符串,真正的搜索部分我个人认为也就只有橙题或黄题的难度.而处理字符串,正如前面所说,STL ...
- 快速幂&&矩阵快速幂
快速幂 题目链接:https://www.luogu.org/problemnew/show/P1226 快速幂用了二分的思想,即将\(a^{b}\)的指数b不断分解成二进制的形式,然后相乘累加起来, ...
- iBeacon技术
声明:部分资料来源自互联网 前言 iBeacon 最早推出是在今年的苹果 WWDC 大会上.作为 iOS 7 的一部分,它吸引人的一点是,iBeacon 是一种开发标准——绝大多数智能手机支持蓝牙 4 ...
- bzoj3545 [ONTAK2010]Peaks、bzoj3551 [ONTAK2010]Peaks加强版
题目描述: bzoj3545,luogu bzoj3551 题解: 重构树+线段树合并. 可以算是板子了吧. 代码(非强制在线): #include<cstdio> #include< ...
- leepcode作业解析 - 5-20
22.缺失数字 给定一个包含 0, 1, 2, ..., n 中 n 个数的序列,找出 0 .. n 中没有出现在序列中的那个数. 示例 1: 输入: [3,0,1] 输出: 2 示例 2: 输入: ...
- Python之路-迭代器 生成器 推导式
迭代器 可迭代对象 遵守可迭代协议的就是可迭代对象,例如:字符串,list dic tuple set都是可迭代对象 或者说,能被for循环的都是可迭代对象 或者说,具有对象.__iter__方法的都 ...
- pycharm添加wordcloud模块时报错:error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": http://landinghub.visualstudio.com/visual-cpp-build-tools
windows 7 32bit python3.6.3 32bit pycharm2018社区版 32bit 问题说明: 添加wordcloud模块时报错:error: Microsoft Visua ...
- python模块--random
random主要用于生成随机字符串等,例如登录页面上随机字符串验证. random常用方法: import random print(random.randrange(1, 10)) # 返回1-10 ...
- Knockout v3.4.0 中文版教程-14-控制文本内容和外观-style绑定
5. style绑定 目的 style绑定用来给关联的DOM元素添加或移除一个或多个样式值.在如下情况很有用,比如,当某些值为负时,高亮显示,或者设置容器元素的宽度来匹配数值的改变. (注意:如果你不 ...
- hdu3594 Cactus
仙人掌入门简单题. 先看一篇文档. #include <iostream> #include <cstring> #include <cstdio> using n ...