题目背景

GD是一个热衷于寻求美好事物的人,一天他拿到了一个美丽的序列。

题目描述

为了研究这个序列的美丽程度,GD定义了一个序列的“美丽度”和“美丽系数”:对于这个序列的任意一个区间[l,r],这个区间的“美丽度”就是这个区间的长度与这个区间的最小值的乘积,而整个序列的“美丽系数”就是它的所有区间的“美丽度”的最大值。现在GD想要你帮忙计算这个序列的“美丽系数”。

输入输出格式

输入格式:

第一行一个整数n,代表序列中的元素个数。 第二行n个整数a1、a2„an,描述这个序列。

输出格式:

一行一个整数,代表这个序列的“美丽系数”。

输入输出样例

输入样例#1: 复制

3
1 2 3
输出样例#1: 复制

4

说明

样例解释 选取区间[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 美丽的序列的更多相关文章

  1. 洛谷P2659 美丽的序列 单调栈模板

    P2659 美丽的序列 题目链接 https://www.luogu.org/problemnew/show/P2659 题目描述 为了研究这个序列的美丽程度,GD定义了一个序列的"美丽度& ...

  2. P2659 美丽的序列

    P2659 美丽的序列对于当前的最小值,找到最大的左右边界,然后更新答案.用单调队列确定左右边界,O(n)做法. #include<iostream> #include<cstdio ...

  3. 洛谷 P2659 美丽的序列 解题报告

    P2659 美丽的序列 题目背景 GD是一个热衷于寻求美好事物的人,一天他拿到了一个美丽的序列. 题目描述 为了研究这个序列的美丽程度,GD定义了一个序列的"美丽度"和" ...

  4. 洛谷——P2659 美丽的序列

    P2659 美丽的序列 单调栈维护区间最小值,单调递增栈维护区间最小值, 考虑当前数对答案的贡献,不断加入数,如果加入的数$>$栈顶,说明栈顶的元素对当前数所在区间是有贡献的,同时加入当前的数. ...

  5. 笛卡尔树-P2659 美丽的序列

    P2659 美丽的序列 tag 笛卡尔树 题意 找出一个序列的所有子段中子段长度乘段内元素最小值的最大值. 思路 我们需要找出所有子段中贡献最大的,并且一个子段的贡献为其长度乘区间最小值. 这--不就 ...

  6. 洛谷P2659 美丽的序列

    题目 该题目可以用辅助数组l[i], r[i]来指向以data[i]为最小值的左端点和右端点.然后最后枚举每个data[i]寻找每个data[i]的美丽值的最大值. 然后辅助数组可以用单调栈求出. # ...

  7. P2659 美丽的序列 (单调栈)

    题目链接 Solution 直接考虑单调栈处理出每一个点作为最小值的区间长度. 然后 \(O(n)\) 找一遍最大值即可. 记得开 long long,以及要注意 \(0\) 的问题. Code #i ...

  8. [Luogu 2023] AHOI2009 维护序列

    [Luogu 2023] AHOI2009 维护序列 恕我冒昧这和线段树模板二有个琴梨区别? #include <cstdio> int n,m; long long p; class S ...

  9. [Luogu 1963] NOI2009 变换序列

    [Luogu 1963] NOI2009 变换序列 先%Dalao's Blog 什么?二分图匹配?这个确定可以建图? 「没有建不成图的图论题,只有你想不出的建模方法.」 建图相当玄学,不过理解大约也 ...

随机推荐

  1. chosen选择框加载数据

    1.单选$(select).val($("#id").val());$(select).trigger("chosen:updated"); 2.多选 func ...

  2. Nginx代理tcp端口实现负载均衡

    Nginx代理tcp端口实现负载均衡 1.修改配置文件 vi /etc/nginx/nginx.conf 添加如下配置: stream { ###XXX upstream notify {   has ...

  3. C语言特点_01

    C语言特点: 1.C语言的32个关键字 auto 局部变量(自动储存) break 无条件退出程序最内层循环 case switch语句中选择项 char 单字节整型数据 const 定义不可更改的常 ...

  4. LeetCode 最大子序和

    给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. 示例: 输入: [-2,1,-3,4,-1,2,1,-5,4], 输出: 6 解释: 连续子数组 ...

  5. **没有规则可以创建“XXX”需要的目标“XXX”问题的解决方案

    一.现象 我将之前Redhat9.0编译好的uboot,转到ubuntu12.04环境.在ubuntu环境下对 uboot重新编译提示错误.编译过程如下: root@hailin-virtual-ma ...

  6. python 有4个数字1234,能组成多少个互不相同且无重复的三位数数字。

    def output(): count = 0 for i in range(1,5): for j in range(1, 5): for k in range(1, 5): if i==j or ...

  7. ACM Changchun 2015 A. Too Rich

    You are a rich person, and you think your wallet is too heavy and full now. So you want to give me s ...

  8. “程序设计与算法训练”课程设计:“BP神经网络的实现”(C++类封装实现)

    一 题目: 71 BP神经网络的实现: 利用C++语言实现BP神经网络, 并利用BP神经网络解决螨虫分类问题: 蠓虫分类问题:对两种蠓虫(A与B)进行鉴别,依据的资料是触角和翅膀的长度,已知了9支Af ...

  9. leetcode刷题——动态规划

    知识点 专题-B-动态规划 题目 斐波那契数列 矩阵路径 数组区间 分割整数 最长递增子序列 最大连续子序列和 最长公共子序列 最长回文子序列 最长公共子串 最长回文子串 背包 题解 CS-Notes ...

  10. PC上测试移动端网站和模拟手机浏览器

    一.Chrome*浏览器 chrome模拟手机总共有四种方法,原理都一样,通过伪装User-Agent,将浏览器模拟成Android设备.以下标星的为推荐方法. 1.新建Chrome快捷方式 右击桌面 ...