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的更多相关文章

  1. 2019年杭电多校第二场 1002题Beauty Of Unimodal Sequence(LIS+单调栈)

    题目链接 传送门 思路 首先我们对\(a\)正反各跑一边\(LIS\),记录每个位置在前一半的\(LIS\)中应该放的位置\(ans1[i]\),后一半的位置\(ans2[i]\). 对于字典序最小的 ...

  2. 2019DX#2

    Solved Pro.ID Title Ratio(Accepted / Submitted)   1001 Another Chess Problem 8.33%(1/12)   1002 Beau ...

  3. 2019 Multi-University Training Contest 2

    2019 Multi-University Training Contest 2 A. Another Chess Problem B. Beauty Of Unimodal Sequence 题意 ...

  4. hdu-5496 Beauty of Sequence(递推)

    题目链接: Beauty of Sequence Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java ...

  5. HDU 5496 Beauty of Sequence

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5496 Beauty of Sequence Problem Description Sequence ...

  6. HDU 5496——Beauty of Sequence——————【考虑局部】

    Beauty of Sequence Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  7. hdu5496 Beauty of Sequence

    Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission ...

  8. CF&&CC百套计划3 Codeforces Round #204 (Div. 1) D. Jeff and Removing Periods

    http://codeforces.com/problemset/problem/351/D 题意: n个数的一个序列,m个操作 给出操作区间[l,r], 首先可以删除下标为等差数列且数值相等的一些数 ...

  9. SoundHound Inc. Programming Contest 2018

    A - F Time Limit: 2 sec / Memory Limit: 1024 MB Score : 100100 points Problem Statement You are give ...

随机推荐

  1. Shell脚本之六 数学计算

    前面一节Shell篇之五 基本运算符介绍了常见的 Shell 算术运算符,这节介绍 Shell 的数学计算.Shell 和其它编程语言不同,Shell 不能直接进行算数运算,必须使用数学计算命令. 下 ...

  2. [转帖]PostgreSQL 参数调整(性能优化)

    PostgreSQL 参数调整(性能优化) https://www.cnblogs.com/VicLiu/p/11854730.html 知道一个 shared_pool 文章写的挺好的 还没仔细看 ...

  3. Java自学-类和对象 类属性

    Java的类属性和对象属性 当一个属性被static修饰的时候,就叫做类属性,又叫做静态属性 当一个属性被声明成类属性,那么所有的对象,都共享一个值 与对象属性对比: 不同对象的 对象属性 的值都可能 ...

  4. Logback获取SkyWalking的全局唯一标识 trace-id 记录到日志中

    为什么要获取trace-id 通过上文Docker-Compose搭建单体SkyWalking我们搭建了SkyWalking服务,我们需要在日志中记录下来每次请求的唯一标识(trace-id),这样就 ...

  5. CF573E Bear and Bowling 贪心、分块、凸包

    传送门 题解搬运工++ 先证明一个贪心做法的正确性:做以下操作若干次,每一次考虑选择没有被选到答案序列中的数加入到答案序列中对答案的贡献,设第\(i\)个位置的贡献为\(V_i\),如果最大的贡献小于 ...

  6. Rdlc Mail Label

    1.创建报表服务器项目RDLML. 2.新建共享数据源DataMailLabel,设置到数据库AdventureWorks的连接,并为报表指定相应的访问凭据. 3.选择Name.Color.Thumb ...

  7. The instance of entity type 'Menu' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked.

    这里记录一个在使用.net core中ef core执行数据库操作时遇到的问题: 我在代码中使用DbContext下的Update方法准备将更改后的数据像这样步到数据库: _context.Menus ...

  8. pycharm_python_flask相关学习心得逐步更新

    2019-10-30: Pycharm的interpreter配置问题对于安装第三方库,如果能够在配置的可视化界面安装成功更好.如果不能可视化安装,则在pycharm的terri..仿cmd下用pip ...

  9. JXL 简单示例

    JXL 1 Overview 1 Overview Home page: http://jexcelapi.sourceforge.net/ JXL 是一个开源的 Excel 开发库,支持 Excel ...

  10. Nginx配置gzip.md

    参考 入门系列之在Nginx配置Gzip gzip是一种流行的数据压缩程序.您可以使用gzip压缩Nginx实时文件.这些文件在检索时由支持它的浏览器解压缩,好处是web服务器和浏览器之间传输的数据量 ...