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;
}