最长上升子序列
来源:互联网 发布:汽车报价大全软件下载 编辑:程序博客网 时间:2024/06/07 23:20
Problem 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
71 7 3 5 9 4 8
Sample Output
4
#include<iostream>#include<malloc.h>using namespace std;struct td{ int num; // 数 int l; // 长度} *st;int main(){ int n, i, j, max; while(cin >> n) { st = (struct td *)malloc(sizeof(struct td) * n); for(i = 0; i < n; i++) { cin >> st[i].num; st[i].l = 1; // 初值为1 } for(i = 0; i < n; i++) { max = 0; for(j = 0; j <= i; j++) if(st[i].num > st[j].num && st[j].l > max) // 下标为i的数比较下标为j的数&&找到长度最长的 { max = st[j].l; // max=最长的长度 } st[i].l = max + 1; //还要加上本身 1 } for(i = 0; i < n; i++) if(st[i].l > max) max = st[i].l; // 找出最大值 cout << max << endl; free(st); } return 0;}
0 0
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- UVA 10474 - Where is the Marble?
- cmakelists
- Java 二分法查找
- 栈的应用举例——迷宫求解
- 看雪论坛ios反编译网址记录
- 最长上升子序列
- 更新sdk
- ubunt & win7 双系统,如何互相访问对方硬盘分区?
- ltib选择“FSL gnome release package”编译错误
- JS 字符串与日期的互转
- AndroidUI组件之ActionBar
- SQL truncate 、delete与drop区别
- POJ 2411 状压DP&&DFS
- CString与string之间的转换