题目
Description
A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence ( a1, a2, ..., aN) be any sequence ( ai1, ai2, ..., aiK), where 1 <= i1 < i2 < ... < iK <= N. For example, sequence (1, 7, 3, 5, 9, 4, 8) has ordered subsequences, e. g., (1, 7), (3, 4, 8) and many others. All longest ordered subsequences are of length 4, e. g., (1, 3, 5, 8).
Your program, when given the numeric sequence, must find the length of its longest ordered subsequence.
Input
The first line of input file contains the length of sequence N. The second line contains the elements of sequence - N integers in the range from 0 to 10000 each, separated by spaces. 1 <= N <= 1000
Output
Output file must contain a single integer - the length of the longest ordered subsequence of the given sequence.
Sample Input
7
1 7 3 5 9 4 8Sample Output
4
题解
模板题,最长上升子序列
套用模板即可
代码
/* By:OhYee Github:OhYee HomePage:http://www.oyohyee.com Email:oyohyee@oyohyee.com Blog:http://www.cnblogs.com/ohyee/ かしこいかわいい? エリーチカ! 要写出来Хорошо的代码哦~ */ #include <cstdio> #include <algorithm> #include <cstring> #include <cmath> #include <string> #include <iostream> #include <vector> #include <list> #include <queue> #include <stack> #include <map> using namespace std; //DEBUG MODE #define debug 0 //循环 #define REP(n) for(int o=0;o<n;o++) const int maxn = 1005; int n; int a[maxn]; class LIS_stack { private: static const int SIZE = maxn; int len;//长度 int Stack[SIZE]; public: LIS_stack() { len = 0; memset(Stack,0,sizeof(Stack)); } void push(int num) { if(len == 0 || Stack[len - 1] < num) { Stack[len++] = num; } else { for(int i = 0;i < len;i++) { if(Stack[i] > num) { Stack[i] = num; break; } } } } int lenth() { return len; } }; int LIS(int *a,int len) { LIS_stack s; for(int i = 0;i < len;i++) s.push(a[i]); return s.lenth(); } bool Do() { if(scanf("%d",&n) == EOF) return false; REP(n) scanf("%d",&a[o]); printf("%d\n",LIS(a,n)); return true; } int main() { while(Do()); return 0; }