首页 > 代码库 > 【单调栈】最长不下降子序列变式
【单调栈】最长不下降子序列变式
题目大意:
给定序列列 a[],最少修改多少个位置可以令其变成最长上升子序列
分析:
这道题看似非常奇怪,然而细想一下很容易发现我们可以通过令 a’[i] = a[i] - i
对 a’[i] 求最长上升子序列,可以得到最多有多少个位置保持不不变
然后用n去减它,就是要修改的个数
代码如下:
#include<cstdio> #include<iostream> #include<algorithm> using namespace std; int n,top=1,h[1000101],a[1000101]; void add(int x) { int l=1,r=top,m; while(l<=r) { m=(l+r)>>1; if(x>=h[m]) l=m+1; else r=m-1; } h[l]=x; } int main() { scanf("%d",&n); for(int i=1;i<=n;++i) { scanf("%d",&a[i]); a[i]-=i; } for(int i=1;i<=n;++i) { if(a[i]>=h[top]&&i!=1) h[++top]=a[i]; else add(a[i]); } printf("%d",n-top); }
【单调栈】最长不下降子序列变式
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。