POJ 2533 Longest Ordered Subsequence(最长路径 dp)
2017-02-24 18:02
302 查看
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
题意很简单,求最长递增子序列的个数;
这题我用的方法是: 未给定起点和终点的最长路解法(DAG上的动态规划),算法复杂度比(nlogn算法 稍微差点,因为hdu1025、1950用的这种方法解,本题就换了种做法)。用到了记忆化搜索 避免超时。
AC代码如下:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1000+10;
int d[maxn];
int v[maxn];
int n;
int dp(int i){
int& ans=d[i];
if(ans>0)return ans;
ans=1;
for(int j=i+1;j<n;j++){
if(v[i]<v[j])
ans=max(ans,dp(j)+1);
}
return ans;
}
int main(){
while(scanf("%d",&n)==1){
for(int i=0;i<n;i++)
scanf("%d",&v[i]);
memset(d,0,sizeof(d));
int ans=0;
for(int i=0;i<n;i++){
ans=max(ans,dp(i));
}
printf("%d\n",ans);
}
return 0;
}
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 49925 | Accepted: 22167 |
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
题意很简单,求最长递增子序列的个数;
这题我用的方法是: 未给定起点和终点的最长路解法(DAG上的动态规划),算法复杂度比(nlogn算法 稍微差点,因为hdu1025、1950用的这种方法解,本题就换了种做法)。用到了记忆化搜索 避免超时。
AC代码如下:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1000+10;
int d[maxn];
int v[maxn];
int n;
int dp(int i){
int& ans=d[i];
if(ans>0)return ans;
ans=1;
for(int j=i+1;j<n;j++){
if(v[i]<v[j])
ans=max(ans,dp(j)+1);
}
return ans;
}
int main(){
while(scanf("%d",&n)==1){
for(int i=0;i<n;i++)
scanf("%d",&v[i]);
memset(d,0,sizeof(d));
int ans=0;
for(int i=0;i<n;i++){
ans=max(ans,dp(i));
}
printf("%d\n",ans);
}
return 0;
}
相关文章推荐
- POJ 2533 Longest Ordered Subsequence(DP最长上升子序列O(n^2)&&O(nlogn))
- POJ 2533 Longest Ordered Subsequence【最长递增子序列】【DP思想】
- poj2533--Longest Ordered Subsequence(dp:最长上升子序列)
- POJ 2533 Longest Ordered Subsequence (DP,最长递增序列)
- POJ 2533-Longest Ordered Subsequence(dp_最长上升子序列)
- poj 2533 Longest Ordered Subsequence (DP:最长上升子序列)
- POJ-2533 Longest Ordered Subsequence (线性dp 最长上升子序列)
- POJ_2533 Longest Ordered Subsequence【DP】【最长上升子序列】
- POJ 2533 - Longest Ordered Subsequence(dp 最长递增子序列)
- 【dp-LIS】牛客网 --最长上升子序列 POJ 2533--Longest Ordered Subsequence(LIS模板题)
- POJ 2533 Longest Ordered Subsequence (最长上升子序列 DP优化)
- poj2533--Longest Ordered Subsequence(dp:最长上升子序列)
- 【POJ 2533】Longest Ordered Subsequence(dp求最长增序列的长度)
- Longest Ordered Subsequence【POJ 2533 dp 最长不降子序列】
- POJ 2533 Longest Ordered Subsequence 最长上升子序列
- POJ2533——Longest Ordered Subsequence(简单的DP)
- POJ 2533-Longest Ordered Subsequence(DP:LIS)
- POJ 2533-Longest Ordered Subsequence(DP)
- POJ 2533 Longest Ordered Subsequence(dp LIS)
- poj 2533 Longest Ordered Subsequence(最长递增子序列)