最长递增子序列问题
2015-12-03 18:03
302 查看
#include<stdio.h>
#include<string.h>
int a[110];
int dp[110];
int main()
{
int n;
while(~scanf("%d",&n))
{
memset(dp,0,sizeof(dp));
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
int ans=1;
dp[0]=1;
for(int i=1;i<n;i++)
{
int max=0;
for(int j=0;j<n;j++)
{
if(a[i]>a[j]&&max<dp[j])max=dp[j];
}
dp[i]=max+1;//无论有没有大于的内个步骤 都要更新dp的值.
if(dp[i]>=ans)ans=dp[i];
}
printf("%d\n",ans);
}
}
最长递增子序列 | ||||||
| ||||||
Description | ||||||
给出一个数字序列求其最长的递增子序列例如序列(1,7,3,5,9,4,8). (1,7)和(3,4,8)是其递增子序列但其最长的递增子序列是(1,3,5,8)。 | ||||||
Input | ||||||
本题有多组测试数据,对于每组测试数据第一行是一个整数n(n<=100)代表序列长度。 第二行是n个整数。 | ||||||
Output | ||||||
最长递增子序列长度 | ||||||
Sample Input | ||||||
7 1 7 3 5 9 4 8 | ||||||
Sample Output | ||||||
4 |
#include<string.h>
int a[110];
int dp[110];
int main()
{
int n;
while(~scanf("%d",&n))
{
memset(dp,0,sizeof(dp));
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
int ans=1;
dp[0]=1;
for(int i=1;i<n;i++)
{
int max=0;
for(int j=0;j<n;j++)
{
if(a[i]>a[j]&&max<dp[j])max=dp[j];
}
dp[i]=max+1;//无论有没有大于的内个步骤 都要更新dp的值.
if(dp[i]>=ans)ans=dp[i];
}
printf("%d\n",ans);
}
}
相关文章推荐
- 队列的链式存储实现
- 清晰理解Objective-C元类
- Ajax跨域请求失败
- Light OJ 1214 - Large Division 【同余定理】
- 字典树模版
- UITableView的contentSize
- 杭电1159Common Subsequence
- 迅为iTOP-4412开发板Ubuntu操作系统烧写方法
- POJ2828---线段树与逆序数&&DUTOJ1210---逆序对构造排列
- 字典树模版
- 如何应对居高不下的离职率?
- keepalived高级应用
- cryptico.js使用技巧
- QT 控件基础
- How can I install mySQL on CentOS without being root/su?
- 杭电1257 最少拦截系统
- Android 检测 手机硬件状态 的Utils
- 转----详解IOS开发应用之并发Dispatch Queues
- js基础篇——encodeURI 和encodeURIComponent
- Handling unhandled exceptions and signals