http://www.rqnoj.cn/Problem_217.html
#include<cstring> #include<cstdlib> #include<cstdio> const int MAXN = 1005; int s[MAXN], top, t[MAXN]; int cnt, hight[MAXN], cur; int N; int main() { while( scanf( "%d", &N) == 1) { cnt = 1, top = 1; for( int i = 1; i <= N; i ++) { scanf( "%d", &hight[i]); if( i == 1) { t[cnt] = hight[i]; } bool flag = false; for( int j = 1; j <= cnt; j ++) { if( t[j] > hight[i]){ t[j] = hight[i]; flag = true; break; } } if( !flag) t[cnt ++] = hight[i]; } s[top ++] = hight[1]; for( int i = 2; i <= N; i ++) { if( hight[i] < s[top - 1]) { s[top ++] = hight[i]; } else { int l, r, m; l = 1, r = top - 1; while( l <= r){ m = (l + r) >> 1; if( s[m] <= hight[i]) r = m - 1; else l = m + 1; } s[l] = hight[i]; } } printf( "%d %d\n", top - 1, cnt - 1); } return 0; }
最长下降子序列