标签:style blog http color io os ar for sp
给定一整型数列{a1,a2...,an}(0<n<=100000),找出单调递增最长子序列,并求出其长度。
如:1 9 10 5 11 2 13的最长单调递增子序列是1 9 10 11 13,长度为5。
7 1 9 10 5 11 2 13 2 2 -1
5 1
分析:跟hdoj1025原理http://blog.csdn.net/shengweisong/article/details/40347947一样
代码:
#include <stdio.h> #include <string.h> #include <algorithm> #define M 100005 using namespace std; int a[M], d[M]; int bis(int m, int len){ int left = 1, right = len, mid; while(left <= right){ mid = (right+left)>>1; if(d[mid] > m) right = mid-1; else if(d[mid]< m) left = mid+1; else return mid; } return left; } int main(){ int n; while(~scanf("%d", &n) ){ memset(d, 0, sizeof(d)); int i, len; for(i = 0; i < n; i ++){ scanf("%d", &a[i]); } //sort(a, a+n); len = 1; d[len] = a[i]; for(i = 1; i < n; i ++){ int t = bis(a[i], len); d[t] = a[i]; if(t>len) len++; } printf("%d\n", len); } return 0; }
标签:style blog http color io os ar for sp
原文地址:http://blog.csdn.net/shengweisong/article/details/40350125