Longest Ordered Subsequence

来源:互联网 发布:新闻稿用什么软件 编辑:程序博客网 时间:2024/06/07 22:34

Longest Ordered Subsequence

 

鹏神意外得到了神灯。

  神灯中冒出了灯神,灯神说道:“我将给你一个有序的数列,你可以在保证原有顺序不变的前提下,挑出任意多的数。如果你挑出的数字是严格升序的,那么这段数字的个数就是你女朋友的个数。”

  “妈的智障。”鹏神骂道。

  但是鹏神还是希望自己能有尽可能多的女朋友。所以他求救于你,希望你能帮他算出他最多能有多少女朋友。

Input

  输入包含多组数据。

  第一行是以为整数N,表示灯神给出的数列的长度。(1≤N≤1000)

  第二行包含N个整数,即是灯神给出的序列。

Output

  对于每组输入数据,请输出最终答案,即鹏神最多可以得到的女朋友个数。

Sample Input

7
1 7 3 5 9 4 8

Sample Output

4

Hint

在样例中,鹏神可以挑出1、3、5、9 或者1、3、5、8,都是4个数字。


求递增子序列个数:

#include <cstdio>#include <cstring>const int MAX=1e3+10;int a[MAX];int dp[MAX];int main(){  int n;  while(~scanf("%d",&n))  {     memset(dp, 0 ,sizeof(dp));          for(int i = 1 ; i <= n ; i++)        scanf("%d",&a[i]);  int m ,ans=1;  dp[1] = 1;  for(int i = 2 ; i <= n ; i++)        {              m = 0;          for(int j = 1; j < i ; j++)                   {                        if( dp[j] > m && a[j] < a[i])                               m = dp[j];     }                        dp[i] = m + 1;              if (dp[i] > ans)                 ans = dp[i];}  printf("%d\n",ans);  }}




原创粉丝点击