#include <bits/stdc++.h>
using namespace std;
int n,a[100001],f[100001],b[100001],res=0,resf,resg,maxx=1;
int g[1000005];
void nhap()
{
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
}
int bs_f(int x,int l,int r)
{
int res=0,m;
while(l<=r)
{
m=(l+r)/2;
if(a[b[m]]>=x)
{
res=m;
l=m+1;
}
else r=m-1;
}
return res;
}
int bs_g(int x,int l,int r)
{
int res=0,m;
while(l<=r)
{
m=(l+r)/2;
if(a[b[m]]<=x)
{
res=m;
l=m+1;
}
else r=m-1;
}
return res;
}
void tim_f()
{
int res,k;
f[1]=1;res=1;b[1]=1;
for(int i=2;i<=n;i++)
{
k=bs_f(a[i],1,f[res]);
f[i]=k+1;
b[f[i]]=i;
//t[i]=b[k];
if(f[i]>f[res]) res=i;
}
//for(int i=1;i<=res;i++) cout<<b[i]<<" ";
resf=f[res];
}
void tim_g()
{
int res,k;
f[1]=1;res=1;b[1]=1;
for(int i=2;i<=n;i++)
{
k=bs_g(a[i],1,g[res]);
g[i]=k+1;
b[g[i]]=i;
//t[i]=b[k];
if(g[i]>g[res]) res=i;
}
//for(int i=1;i<=res;i++) cout<<b[i]<<" ";
resg=g[res];
}
void giai()
{
tim_f();
tim_g();
int res=min(n-resf,n-resg);
cout<<res;
}
void xuat()
{
res=maxx;
for (int i=n;i>=1;i--)
{
if (res==f[i])
{
a[res]=i;
res--;
}
if (res==0) break;
}
cout<<maxx<<endl;
for (int i=1;i<=maxx;i++) cout<<a[i]<<" ";
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
freopen("egroup.inp","r",stdin);
freopen("egroup.out","w",stdout);
//xuli();
//xuat();
nhap();
giai();
}
* Chuyên dạy lập trình ONLINE cho học sinh THCS, THPT *.
Mọi giao lưu, trao đổi, xin liên hệ: Lê Quang Vinh - zalo: 037.803.8755.
Page: Lớp học Code Sky
Group FB1: Ôn thi HSG9 - THTB - TS10 chuyên tin
Group FB2: Học Scratch - Ôn thi Tin học trẻ bảng A
Thứ Ba, 3 tháng 5, 2022
EGROUP C++
Đăng ký:
Đăng Nhận xét (Atom)
Không có nhận xét nào:
Đăng nhận xét
Lưu ý: Chỉ thành viên của blog này mới được đăng nhận xét.