HDU6592 Beauty Of Unimodal Sequence
Beauty Of Unimodal Sequence
给一个序列,在满足单调递增或者单调递减或者先增后减的最长子序列集合里找到下标字典序最大以及最小的两个子序列,输出这两个子序列里元素的下标。
n≤3×105
moomhxy的题解
先正着求一遍LIS,再反着求一遍LIS,求出每个点作为上升子序列结尾的最大长度和每个点作为下降子序列开头的最大长度。
我们可以枚举这个单峰序列的峰顶是什么,这样最长长度就找到了。
然后考虑怎么构造解。
求字典序最小的话,首先找到第一个顶峰,然后往前找递减的序列中下标较小的,往后就依次找,这样能保证字典序最小。
如何找这个下标较小的呢?显然我们希望每种结尾长度的点都越靠前越好。所以用单调栈维护即可。
最大的话找到最后一个顶峰,往前是依次找,往后是找LIS中下标大的。维护方法类似。
时间复杂度 O(n log n),瓶颈在于求LIS。
CO int N=300000+10;
int a[N],dp[N],up[N],down[N];
int h[N],st[N],ans[N];
void real_main(int n){
fill(dp,dp+n+1,INT_MAX),dp[0]=0;
for(int i=1;i<=n;++i){
read(a[i]);
up[i]=lower_bound(dp+1,dp+n+1,a[i])-dp;
dp[up[i]]=a[i];
}
fill(dp,dp+n+1,INT_MAX),dp[0]=0;
for(int i=n;i;--i){
down[i]=lower_bound(dp+1,dp+n+1,a[i])-dp;
dp[down[i]]=a[i];
}
// minimum lexicographic order
int tot=0;
int peak=1,height=up[1]+down[1];
for(int i=2;i<=n;++i)
if(up[i]+down[i]>height) peak=i,height=up[i]+down[i];
int top=0;
h[up[peak]]=a[peak];
for(int i=peak-1;i;--i){
if(a[i]>=h[up[i]+1]) continue;
while(top and up[i]>=up[st[top]]) --top;
st[++top]=i;
h[up[i]]=a[i];
}
for(;top;--top) ans[++tot]=st[top];
ans[++tot]=peak;
for(int i=peak+1;i<=n;++i)
if(down[i]==down[ans[tot]]-1 and a[i]<a[ans[tot]]) ans[++tot]=i;
for(int i=1;i<=tot;++i) printf("%d%c",ans[i]," \n"[i==tot]);
// maximum lexcographic order
tot=0;
peak=1,height=up[1]+down[1];
for(int i=2;i<=n;++i)
if(up[i]+down[i]>=height) peak=i,height=up[i]+down[i];
top=0;
st[++top]=peak;
for(int i=peak-1;i;--i)
if(up[i]==up[st[top]]-1 and a[i]<a[st[top]]) st[++top]=i;
for(;top;--top) ans[++tot]=st[top];
h[down[peak]]=a[peak];
for(int i=peak+1;i<=n;++i){
if(a[i]>=h[down[i]+1]) continue;
while(tot and down[i]>=down[ans[tot]]) --tot;
ans[++tot]=i;
h[down[i]]=a[i];
}
for(int i=1;i<=tot;++i) printf("%d%c",ans[i]," \n"[i==tot]);
}
int main(){
for(int n;~scanf("%d",&n);) real_main(n);
return 0;
}
HDU什么时候开始支持<bits/stdc++.h>了……
HDU6592 Beauty Of Unimodal Sequence的更多相关文章
- 2019年杭电多校第二场 1002题Beauty Of Unimodal Sequence(LIS+单调栈)
题目链接 传送门 思路 首先我们对\(a\)正反各跑一边\(LIS\),记录每个位置在前一半的\(LIS\)中应该放的位置\(ans1[i]\),后一半的位置\(ans2[i]\). 对于字典序最小的 ...
- 2019DX#2
Solved Pro.ID Title Ratio(Accepted / Submitted) 1001 Another Chess Problem 8.33%(1/12) 1002 Beau ...
- 2019 Multi-University Training Contest 2
2019 Multi-University Training Contest 2 A. Another Chess Problem B. Beauty Of Unimodal Sequence 题意 ...
- hdu-5496 Beauty of Sequence(递推)
题目链接: Beauty of Sequence Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java ...
- HDU 5496 Beauty of Sequence
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5496 Beauty of Sequence Problem Description Sequence ...
- HDU 5496——Beauty of Sequence——————【考虑局部】
Beauty of Sequence Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- hdu5496 Beauty of Sequence
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission ...
- CF&&CC百套计划3 Codeforces Round #204 (Div. 1) D. Jeff and Removing Periods
http://codeforces.com/problemset/problem/351/D 题意: n个数的一个序列,m个操作 给出操作区间[l,r], 首先可以删除下标为等差数列且数值相等的一些数 ...
- SoundHound Inc. Programming Contest 2018
A - F Time Limit: 2 sec / Memory Limit: 1024 MB Score : 100100 points Problem Statement You are give ...
随机推荐
- POJ-动态规划-典型问题模板
动态规划典型问题模板 一.最长上升子序列(Longest increasing subsequence) 状态(最关键):f[N]为动规数组,f[i]表示从第一个字符开始,以a[i]为最后一个字符的序 ...
- python抓取每期双色球中奖号码,用于分析
获取每期双色球中奖号码,便于观察,话不多说,代码如下 # -*- coding:utf-8 -*- # __author__ :kusy # __content__:获取每期双色球中奖号码 # __d ...
- Elasticsearch Transport 模块创建及启动分析
Elasticsearch 通信模块的分析从宏观上介绍了ES Transport模块总体功能,于是就很好奇ElasticSearch是怎么把服务启动起来,以接收Client发送过来的Index索引操作 ...
- Flink 源码解析 —— 项目结构一览
Flink 源码项目结构一览 https://t.zsxq.com/MNfAYne 博客 1.Flink 从0到1学习 -- Apache Flink 介绍 2.Flink 从0到1学习 -- Mac ...
- 《C程序设计语言》(K&R)中文高清非扫描件
<C程序设计语言>(K&R)中文高清非扫描件(带书签目录) 对于某下载东西都要C币的网站无爱了.好不容易找了一个,发出来看会不会帮到别人 附上addr:https://pan. ...
- .Net 如何使用Nlog
NLog是一个简单灵活的.NET日志记录类库,NLog的API非常类似于log4net,且配置方式非常简单.通过使用NLog,我们可以在任何一种.NET语言中输出带有上下文的调试信息,根据项目需求配置 ...
- 红黑树和AVL树的区别(转)
add by zhj: AVL树和红黑树都是平衡二叉树,虽然AVL树是最早发明的平衡二叉树,但直接把平衡二叉树等价于AVL树,我认为非常不合适. 但很多地方都在这么用.两者的比较如下 平衡二叉树类型 ...
- 【LeetCode】387. First Unique Character in a String
Difficulty:easy More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/first-unique-cha ...
- 获取Excel
默认Excel文档为 代码如下 需要下载 "EPPlus.Core" var file = Directory.GetCurrentDirectory() + "\ ...
- python数据分析三剑客之: pandas操作
pandas的操作 pandas的拼接操作 # pandas的拼接操作 级联 pd.concat , pd.append 合并 pd.merge , pd.join 一丶pd.concat()级联 # ...