POJ2533:Longest Ordered Subsequence
2015-06-11 13:37
447 查看
Longest Ordered Subsequence
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
Sample Output
这道题自己一开始就是这个思路,只不过就在想时间会不会超。
比较好理解的一道动态规划。题意是求一个序列中最长的递增序列的长度,就是逐渐输入逐渐比对,如果value[j]>value[i]的话,dp[j]=max(dp[0],dp[1].....dp[j-1])+1。
代码:
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 37454 | Accepted: 16463 |
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 8
Sample Output
4
这道题自己一开始就是这个思路,只不过就在想时间会不会超。
比较好理解的一道动态规划。题意是求一个序列中最长的递增序列的长度,就是逐渐输入逐渐比对,如果value[j]>value[i]的话,dp[j]=max(dp[0],dp[1].....dp[j-1])+1。
代码:
#include <iostream> using namespace std; int value[1005]; int dp[1005]; int main() { int num; int i,j,max; cin>>num; for(i=0;i<num;i++) { cin>>value[i]; dp[i]=1; max=1; for(j=0;j<i;j++) { if(value[i]>value[j]) { if(dp[j]+1>max) { max=dp[j]+1; } } } dp[i]=max; } max=1; for(i=0;i<num;i++) { if(dp[i]>max) { max=dp[i]; } } cout<<max<<endl; return 0; }
相关文章推荐
- UIImagePickerController 的基本用法 - iOS - UI基础知识总结14
- 【转载】How to build and run your first deep learning network
- [LeetCode] Implement Stack using Queues 用队列来实现栈
- UIControl-IOS发展
- #leetcode#Longest Consecutive Sqeuence
- UI设计
- iOS开发- UICollectionView详解+实例
- UEditor上传图片被压缩得模糊的解决方法
- java 对map中的 value 排序
- Easyui 翻页不保存选中记录
- 优化UITableViewCell高度计算的那些事
- NGUI的UISprite动态染色的一种方法
- leetcode--Unique Binary Search Trees
- Android基础系列-----------GUI系统之WindowManagerService
- Android基础系列-----------GUI系统之Android的UI显示概述
- UIAlertView消息文本左对齐操作
- java.sql.SQLException: Incorrect string value
- easyui-ueditor
- [LeetCode] Implement Stack using Queues
- 51nod 1126 求递推序列的第N项 && hdu - 1005 Number Sequence (求周期)