设计一个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];
}