题意:给你一些数找到最长递减子序列
思路:跟nyoj 214 差不多,只不过这儿是单调递减
#include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> using namespace std; #define maxN 40000 int f[maxN],so[maxN],key[maxN],n; bool cmp(int &x,int &y) { return x>y; } int low_bun(int l,int r,int x) { if(l==r)return l; int mid=(l+r)/2; if(so[mid]<=x)return low_bun(l,mid,x); else return low_bun(mid+1,r,x); } int low(int x){return x&(-x);} int get_max(int x) { int maxx=0; while(x!=0){ maxx=max(maxx,key[x]); x-=low(x); } return maxx; } void update(int x) { int m=x; while(x<n+1){ if(key[m]>=key[x]) key[x]=key[m]; else break; x+=low(x); } } int main() { int k=1; while(scanf("%d",&f[1])&&f[1]!=-1) { memset(key,0,sizeof(key)); n=2; so[0]=0x0fffff,so[1]=f[1]; while(scanf("%d",&f[n])&&f[n]!=-1) so[n]=f[n++]; sort(so,so+n,cmp); int maxx=0; for(int i=1;i<n;i++) { int m=low_bun(1,n-1,f[i]); key[m]=get_max(m-1)+1; maxx=max(maxx,key[m]); update(m); } printf("Test #%d:\n maximum possible interceptions: %d\n\n",k++,maxx); } }