最长上升子序列 O(nlogn)

来源:互联网 发布:我的淘宝主页打不开 编辑:程序博客网 时间:2024/06/05 22:59

https://oj.jdfz.com.cn/oldoj/problem.php?id=2157

2157: Increasing
Description
数列A1,A2,……,AN,修改最少的数字,使得数列严格单调递增。

Input
第1 行,1 个整数N
第2 行,N 个整数A1,A2,……,AN

Output
1 个整数,表示最少修改的数字

Sample Input
3
1 3 2

Sample Output
1

HINT
• 对于50% 的数据,N <= 10^3
• 对于100% 的数据,1 <= N <= 10^5, 1 <= Ai <= 10^9

/*
b[pos] 代表长度为 pos 的 最长上升子序列 的 最后一个数的最小值
可知 b[ ] 为单调递增的
于是 upper_bound 二分 一下
*/

#include<stdio.h>#include<iostream>#include<algorithm>using namespace std;int n;int a[100005];int b[100005],cnt;int main(){    scanf("%d",&n);    int i,j;    for(i=1;i<=n;i++)    {        scanf("%d",&a[i]);    }    b[1]=a[1];    cnt=1;    for(i=1;i<=n;i++)    {        if(a[i]>b[cnt]) b[++cnt]=a[i];        else        {            int pos=upper_bound(b+1,b+cnt+1,a[i])-b;            b[pos]=a[i];        }    }    printf("%d",n-cnt);    return 0;}
0 0