POJ 2533 Longest Ordered Subsequence(DP)
2018-03-18 19:05
423 查看
题目链接:http://poj.org/problem?id=2533
题目大意:找出最长上升子序列的最大长度
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.InputThe 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 <= 1000OutputOutput file must contain a single integer - the length of the longest ordered subsequence of the given sequence.Sample Input7
1 7 3 5 9 4 8Sample Output4
思路:裸的最长上升子序列,套板子即可,板子:http://blog.csdn.net/baodream/article/details/79105545
代码:#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<queue>
#include<stack>
#include<map>
using namespace std;
#define FOU(i,x,y) for(int i=x;i<=y;i++)
#define FOD(i,x,y) for(int i=x;i>=y;i--)
#define MEM(a,val) memset(a,val,sizeof(a))
#define PI acos(-1.0)
const double EXP = 1e-9;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const ll MINF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9+7;
const int N = 1e6+5;
int dp[1005];
int a[1005];
int n;
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
std::ios::sync_with_stdio(false);
while(~scanf("%d",&n))
{
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
int len=1;
dp[1]=a[1];
for(int i=2;i<=n;i++)
{
if(a[i]>dp[len])
dp[++len]=a[i];
else
{
int pos=lower_bound(dp+1,dp+1+len,a[i])-dp;
dp[pos]=a[i];
}
}
printf("%d\n",len);
}
return 0;
}
题目大意:找出最长上升子序列的最大长度
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.InputThe 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 <= 1000OutputOutput file must contain a single integer - the length of the longest ordered subsequence of the given sequence.Sample Input7
1 7 3 5 9 4 8Sample Output4
思路:裸的最长上升子序列,套板子即可,板子:http://blog.csdn.net/baodream/article/details/79105545
代码:#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<queue>
#include<stack>
#include<map>
using namespace std;
#define FOU(i,x,y) for(int i=x;i<=y;i++)
#define FOD(i,x,y) for(int i=x;i>=y;i--)
#define MEM(a,val) memset(a,val,sizeof(a))
#define PI acos(-1.0)
const double EXP = 1e-9;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const ll MINF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9+7;
const int N = 1e6+5;
int dp[1005];
int a[1005];
int n;
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
std::ios::sync_with_stdio(false);
while(~scanf("%d",&n))
{
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
int len=1;
dp[1]=a[1];
for(int i=2;i<=n;i++)
{
if(a[i]>dp[len])
dp[++len]=a[i];
else
{
int pos=lower_bound(dp+1,dp+1+len,a[i])-dp;
dp[pos]=a[i];
}
}
printf("%d\n",len);
}
return 0;
}
相关文章推荐
- POJ-2533 Longest Ordered Subsequence ( DP )
- poj2533--Longest Ordered Subsequence(dp:最长上升子序列)
- poj 2533 Longest Ordered Subsequence 经典DP
- poj入门水题--动态规划(DP)2533 Longest Ordered Subsequence 1088滑雪
- POJ 2533 Longest Ordered Subsequence DP(LIS)
- POJ 2533-Longest Ordered Subsequence(dp_最长上升子序列)
- poj 2533 Longest Ordered Subsequence (DP:最长上升子序列)
- 文章标题 POJ 2533 : Longest Ordered Subsequence (DP)
- POJ 2533-Longest Ordered Subsequence(DP:LIS)
- poj2533--Longest Ordered Subsequence(dp:最长上升子序列)
- DP-POJ-2533-Longest Ordered Subsequence
- POJ 2533 - Longest Ordered Subsequence(dp 最长递增子序列)
- [DP] POJ-2533 Longest Ordered Subsequence
- POJ 2533 Longest Ordered Subsequence(最长路径 dp)
- Longest Ordered Subsequence POJ - 2533 【经典DP】
- POJ 2533 Longest Ordered Subsequence(DP动态规划)
- POJ 2533 Longest Ordered Subsequence(dp LIS)
- POJ 2533 Longest Ordered Subsequence(基础DP)
- 【POJ 2533】Longest Ordered Subsequence(dp求最长增序列的长度)
- POJ 2533 Longest Ordered Subsequence (DP,最长递增序列)