The longest ascending subsequence. Although the data can be directly N-party but another Nlogn is written.
Transfer: F[i]=max (f[j]+1) (A[j]<a[i])
O (n^2)
#include <iostream>#include <cstdio>using namespaceStdConst intn=5005;intN,a[n],f[n],ans;intRead () {intR=0, f=1;CharP=getchar (); while(p>' 9 '|| p<' 0 ') {if(p=='-') f=-1; P=getchar (); } while(p>=' 0 '&&p<=' 9 ') {r=r*Ten+p-48; P=getchar (); }returnR*f;}intMain () {n=read (); for(intI=1; i<=n;i++) A[i]=read (); for(intI=1; i<=n;i++) { for(intj=0; j<i;j++)if(A[j]<a[i]&&f[j]+1>f[i]) F[i]=f[j]+1; Ans=max (Ans,f[i]); } printf ("%d\n", ans);return 0;}
O (Nlogn) tree-like array +hash
#include <iostream>#include <cstdio>#include <map>#include <algorithm>using namespaceStdConst intn=5005;intn,a[n],f[n],ans,g[n],t[n];map<int,int>mp;intRead () {intR=0, f=1;CharP=getchar (); while(p>' 9 '|| p<' 0 ') {if(p=='-') f=-1; P=getchar (); } while(p>=' 0 '&&p<=' 9 ') {r=r*Ten+p-48; P=getchar (); }returnR*f;}inline intlbintx) {returnx& (-X);}voidUpdateintXintV) { for(intI=X;I<=N;I+=LB (i)) T[i]=max (t[i],v);}intQues (intx) {intRe=0; for(inti=x;i>=1; i-=lb (i)) Re=max (Re,t[i]);returnRe;}intMain () {n=read (); for(intI=1; i<=n;i++) A[i]=g[i]=read (); Sort (g+1G+1+N); for(intI=1; i<=n;i++) mp[g[i]]=i; for(intI=1; i<=n;i++) A[i]=mp[a[i]]; for(intI=1; i<=n;i++) {f[i]=ques (A[i]-1)+1; Update (a[i],f[i]); Ans=max (Ans,f[i]); } printf ("%d\n", ans);return 0;}
Bzoj 1669: [Usaco2006 oct]hungry cows hungry cows dp+ tree-like array +hash "