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 ...
随机推荐
- mysql创建存储过程动态SQL语句
DROP PROCEDURE IF EXISTS x.`wk`; DELIMITER $$ CREATE PROCEDURE `x`.`wk`() BEGIN ); ); SET t = CONCAT ...
- spark 资源动态分配
'spark.shuffle.service.enabled': 'true', 'spark.dynamicAllocation.enabled': 'false', , , , 'spark.sq ...
- element-ui 开发备忘
目录 购物清单的数据结构说明 1. <el-radio> 的 label 属性 2. 在 <el-table> 中放入表单组件 3. 表单验证时填写正确的 prop 属性 总结 ...
- TP-LINK WR703N OpenWrt 无线配网历程
① 创建了两个 Interfaces,名字分别为 lan.wlan0 (可自行设定),一个负责连接 PPPoE,一个负责提供 AP 热点. ② 配置 wlan0 相关 ip 地址,该地址为无线网内网地 ...
- ThreadPoolExecutor源码1
参考:https://www.cnblogs.com/liuyun1995/p/9305273.html ThreadPoolExecutor1 executorService1 = new Thre ...
- Sitecore 内容版本设计
Sitecore内容变化的跟踪显着偏离既定规范.了解Sitecore中版本控制和工作流程的细节,该产品是对这些发布工具的回答. 在出版界,实时跟踪内容变化很常见,可能是由于Microsoft Word ...
- 【C语言】获得数组长度
c语言中,定义数组后可以用sizeof命令获取数组的长度(可容纳元素个数): 如: { int data[5]; int length; length=sizeof(data)/sizeof(data ...
- FPGA控制RGMII接口PHY芯片基础
一.前言 网络通信中的PHY芯片接口种类有很多,之前接触过GMII接口的PHY芯片RTL8211EG.但GMII接口数量较多,本文使用RGMII接口的88E1512搭建网络通信系统.这类接口总线位宽小 ...
- 『保卫王国 树上倍增dp』
保卫王国 Description Z 国有n座城市,n - 1条双向道路,每条双向道路连接两座城市,且任意两座城市 都能通过若干条道路相互到达. Z 国的国防部长小 Z 要在城市中驻扎军队.驻扎军队需 ...
- JS实现文件自动上传
JS引用: <script type="text/javascript" src="~/bootstrap/js/fileinput.min.js"> ...