poj2533(DP)(与杭电1257一模一…

来源:互联网 发布:手机怎样看淘宝的积分 编辑:程序博客网 时间:2024/05/01 06:49
Longest Ordered Subsequence
Time Limit: 2000MSMemory Limit: 65536KTotal Submissions: 22202Accepted: 9565

Description

A numeric sequence ofai is ordered if a1< a2 < ...< aN. Let the subsequence of thegiven 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 manyothers. All longest ordered subsequences are of length 4, e. g.,(1, 3, 5, 8).

Your program, when given the numeric sequence, must find the lengthof its longest ordered subsequence.

Input

The first line of input file containsthe length of sequence N. The second line contains the elements ofsequence - N integers in the range from 0 to 10000 each, separatedby spaces. 1 <= N <= 1000

Output

Output file must contain a singleinteger - the length of the longest ordered subsequence of thegiven sequence.

Sample Input

71 7 3 5 9 4 8

Sample Output

4

Source

Northeastern Europe 2002, Far-Eastern Subregion
这个代码就是杭电1257最少拦截系的 代码。。一点都没有改过。。poj2533(DP)(与杭电1257一模一样)
#include<stdio.h>
int a[10010],b[10010];
int main()
{
 int i,j,n,max;
 while(scanf("%d",&n)!=EOF)
 {
  for(i=0;i<n;i++)
  scanf("%d",&a[i]);//输入数据
  b[0]=1;//初始化,以a[0]结尾的最长递增子序列长度为1
 for(i=1;i<n;i++)//i从0开始和从1开始关系不大(是一样的)
  {
   b[i]=1;//b[i]最小值为1
  for(j=0;j<i;j++)//从j到i进行比较(j<i哦)
   if(a[i]>a[j]&&b[j]+1>b[i])//记得这边b[j]+1>b[i]不能少,我试了。
    b[i]+=1;
  }
 for(max=i=0;i<n;i++)//求出整个序列的最长递增子序列的长度
  if(b[i]>max)
   max=b[i];
   printf("%d\n",max);
 }
 return 0;
}
 
原创粉丝点击