題意:
給定一個序列A,刪除任意個數的數,使得生成的新序列B={a,b,c,d….}與序列C={1,2,3,4…..}一一對應的數的個數最多,求最多能對應多少個數。
解析:
不妨列一下樸素的DP方程
複雜度O(n^2),顯然不可過,所以需要進一步優化。
觀察三個限定條件。
容易發現已知
而
而
所以我們不妨把
所以我們只需要按照
需要注意的是,如果
代碼:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 100100
#define M 1000100
using namespace std;
struct node
{
int x,y;
friend bool operator < (node a,node b)
{
if(a.x==b.x)return a.y<b.y;
return a.x<b.x;
}
}b[N];
int d[M],a[N];
int n,ma,tot;
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
if(i-a[i]<0)continue;
b[++tot].x=i-a[i],b[tot].y=a[i];
}
sort(b+1,b+tot+1);
memset(d,0x3f,sizeof(d));
for(int i=1;i<=tot;i++)
{
int l=1,r=ma,ans=0;
while(l<=r)
{
int mid=(l+r)>>1;
if(b[i].y>d[mid])ans=mid,l=mid+1;
else r=mid-1;
}
ma=max(ma,ans+1);
d[ans+1]=min(d[ans+1],b[i].y);
}
printf("%d\n",ma);
}