POJ 2533 Longest Ordered Subsequence

举报
谙忆 发表于 2021/05/28 07:26:10 2021/05/28
【摘要】 Description 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 ...

Description

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

用动态规划做,每次从后面对前面判断
用dd[k]表示以df[k]作为终点的最大上升子序列
则:
dd[1] = 1;
dd[k] = Max (dd[i]:1 <= i < k 且 df[i ]< df[k] 且 k != 1) + 1.
也就是第k+1前面一个不大于df[k]的数的dd[ ]的值;
n:7
i :0 1 2 3 4 5 6
df :1 7 3 5 9 4 8
dd[0]:1;
dd[1]:dd[0]+1=2;
dd[2]:dd[0]+1=2;
dd[3]:dd[2]+1=3;
dd[4]:因为df[0],df[1],df[2],df[3]都小于df[4],但是dd[3]最大,
所以,dd[4]=dd[3]+1=4;
dd[5]:dd[2]+1=3;
dd[6]:dd[5]+1=4;
…………………………………………………………

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define Maxn 1020
int df[Maxn],dd[Maxn];

int cmp(const void *x,const void *y){ return (*(int *)y-*(int *)x); /*快速排序,从大到小排序*/
}
int main(){ int n; while(scanf("%d",&n)==1){ for(int i=0;i<n;i++){ scanf("%d",&df[i]); } dd[0]=1; for(int i=1;i<n;i++){ int t=0; for(int j=0;j<i;j++){ if(df[i]>df[j]){ if(t<dd[j]){ t=dd[j]; } } } dd[i]=t+1; //此时的t是dd[i]之前的最大增子序列的个数 } qsort(dd,n,sizeof(int),cmp); printf("%d\n",dd[0]); } return 0;
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

文章来源: chenhx.blog.csdn.net,作者:谙忆,版权归原作者所有,如需转载,请联系作者。

原文链接:chenhx.blog.csdn.net/article/details/49429825

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。