CSU-ACM2019寒假集训 DP初步-C - Longest Ordered Subsequence

依旧水题,不过开心,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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值