动态规划之单调递增最长子序列

设计一个O(n2)时间的算法,找出由n个数组成的序列的最长单调递增子序列。

输入格式:

输入有两行: 第一行:n,代表要输入的数列的个数 第二行:n个数,数字之间用空格格开

输出格式:

最长单调递增子序列的长度

输入样例:

在这里给出一组输入。例如:

5
1 3 5 2 9

输出样例:

在这里给出相应的输出。例如:

4

Code:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <list> 
#include <sstream>
const int INF=0x3f3f3f3f;
typedef long long LL;
const int mod=1e9+7;
using namespace std;
#define ARR_SIZE 10001

void output(int i);
int x[ARR_SIZE],y[ARR_SIZE];
 
int main()
{
	int i,j;
	cin>>i;
	for(j=0;j<i;j++)
		cin>>y[j];
	output(i);
 
}
 
void output(int i)
{
	int arr[ARR_SIZE];
	int j,k,max;
	arr[0]=1;
	x[0]=-1;
	for(j=1;j<i;j++)
	{
		max=0;
		x[j]=-1;
		for(k=0;k<j;k++)
		{
			if(arr[k]>max&&y[k]<y[j])
			{
				x[j]=k;
				max=arr[k];                   
			}
		}
		arr[j]=max+1;
	}
	j=0;
	for(k=1;k<i;k++)
	{
		if(arr[k]>arr[j])
		{
			j=k;
		}
	}	
	cout<<arr[j];
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值