解析:node
代碼:c++
#include<bits/stdc++.h> using namespace std; #define ll long long #define rint register int inline int read(){ int x=0,f=0;char ch=getchar(); while(!isdigit(ch)) f=(ch==45),ch=getchar(); while( isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar(); return f?(~x+1):x; } #define man 1000050 int n,k,ans; int a[man],f[man]; int main(){ n=read();k=read(); for (int i=1;i<=n;i++) a[i]=read(); for (int i=1;i<=n;i++){ for (int j=n-k;j>=1;j--){ f[j]=max(f[j],f[j-1]+(j==a[i])); } } printf("%d",f[n-k]); return 0; }
100分:git
#include<bits/stdc++.h> using namespace std; #define ll long long #define rint register int inline int read(){ int x=0,f=0;char ch=getchar(); while(!isdigit(ch)) f=(ch==45),ch=getchar(); while( isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar(); return f?(~x+1):x; } #define man 1000050 int a[man],n,k; struct node{ int x,y;}c[man]; int b[man],ans=0; inline void update(int x,int val){ while(x<=n){ b[x]=max(b[x],val); x+=(x&(-x)); } } inline int query(int x){ int maxn=0; while(x>0){ maxn=max(maxn,b[x]); x-=(x&(-x)); } return maxn; } int cmp(node a,node b){ if(a.x<b.x) return 1; if(a.x==b.x&&a.y<b.y) return 1; return 0; } int main(){ n=read();k=read(); for(rint i=1;i<=n;i++){ a[i]=read(); c[i]=(node){i-a[i],a[i]}; } sort(c+1,c+1+n,cmp); for(rint i=1;i<=n;i++){ if(c[i].x<0||c[i].y>n-k||c[i].x>k) continue; int cnt=query(c[i].y-1)+1; ans=max(ans,cnt); update(c[i].y,cnt); } printf("%d\n",min(ans,n-k)); return 0; }