依旧水题,不过开心,O(∩_∩)O,毕竟通过这三道题让自己的信心还是回来一点的啦~
题目:
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
日常翻译:╮(╯▽╰)╭
如果a1 <a2 <… <aN,则排序ai的数字序列。 令给定数字序列(a1,a2,…,aN)的子序列为任何序列(ai1,ai2,…,aiK),其中1 <= i1 <i2 <… <iK <= N 例如,序列(1,7,3,5,9,4,8)具有有序的子序列,例如。 g。,(1,7),(3,4,8)和许多其他人。 所有最长有序的子序列长度为4,e。 g。,(1,3,5,8)。
当给定数字序列时,您的程序必须找到其最长有序子序列的长度。
输入
输入文件的第一行包含序列N的长度。第二行包含序列的元素 - N个整数,范围从0到10000,每个用空格分隔。 1 <= N <= 1000
输出
输出文件必须包含一个整数 - 给定序列的最长有序子序列的长度。
样本输入
7
1 7 3 5 9 4 8
样本输出
4
思路:这道题的思路很简单啦,将每个位置的长度首先初始化为1,然后dp,从第一个开始,如果a[i]>a[j]的话,那就将a[j]这个数的位置的长度先赋给a[i],如果在位置[1,j-1]之间遇到的比a[j]小的数字这个位置的长度最大的话,就将其赋给a[j]并加1(因为a[j]>a[i]呀,还要再加一个长度)
好啦,就是这样滴~
代码如下:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=10005;
int a[maxn];
int f[maxn];
void init()//初始化处理,讲所有位置长度定位1
{
for(int i=1;i<=maxn;i++)
f[i]=1;
memset(a,0,sizeof(a));
}
int main()
{
init();
int T;
scanf("%d",&T);
for(int i=1;i<=T;i++)
scanf("%d",&a[i]);
for(int i=2;i<=T;i++)
{
for(int j=1;j<i;j++)
{
if(a[i]>a[j])//这两个判断就是求出比a[j]小的且长度最大的啦
{
if(f[j]+1>f[i])
f[i]=f[j]+1;
}
}
}
sort(f,f+T+1);//排序一下下~
printf("%d\n",f[T]);//最后将最大的输出即可啦~
return 0;
}