POJ 2533 Longest Ordered Subsequence【最长递增子序列】【DP思想】

3 篇文章 0 订阅
2 篇文章 0 订阅

Longest Ordered Subsequence

Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 6   Accepted Submission(s) : 1
Problem 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
 

Source
PKU




要求长度为i的序列的Ai{a1,a2,……,ai}最长递增子序列,需要先求出序列Ai-1{a1,a2,……,ai-1}中以各元素(a1,a2,……,ai-1)作为最大元素的最长递增序列,然后把所有这些递增序列与ai比较,如果某个长度为m序列的末尾元素aj(j<i)比ai要小,则将元素ai加入这个递增子序列,得到一个新的长度为m+1的新序列,否则其长度不变,将处理后的所有i个序列的长度进行比较,其中最长的序列就是所求的最长递增子序列


该算法的思想十分简单,如果要得出ai序列的最长递增子序列,需要计算出ai-1的所有元素的最长递增子序列 依次地推出ai-2,ai-3,·····,把这个过程倒过来,就可得到地推算法,依次推出a1,a2,a3····,直到推出ai为止。


 
翻译:
给出一个数n
输入n个数
找出这n个数的最长递增子序列

当i=5时
以1 7 3 5 9 4 分别为最后一个数,各个最长子序列为:
1
1 7
1 3
1 3 5 
1 3 5 9
1 3 4

i=6时,加入了数字8
序列为:
1 8
1 7 8
1 3 8
1 3 5 8
1 3 5 9 X
1 3 4 8
所以4 是MAX长度 且不止一个

//标准LIS最长递增子序列写法
#include<stdio.h>
#include<string.h>
int main (void)
{
    int n;
    int a[10100],liss[10100];
    while(~scanf("%d",&n))
    {
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
        }
        int max=0;

        //18-19   直接把liss[]全部刷为1 正确
        for(int i=0;i<n;i++)
            liss[i]=1;


        for(int i=1;i<n;i++)
        {
            for(int j=0;j<i;j++)
            {
                if(a[i]>a[j]&&liss[j]+1>liss[i])
                {
                    liss[i]=liss[j]+1;
                }
            }
            if(max<liss[i])
            {
                max=liss[i];
            }
        }
        printf("%d\n",max);
    }
    return 0;
}



//WA写法
#include<stdio.h>
#include<string.h>
int main (void)
{
    int n;
    int a[10100],liss[10100];
    while(~scanf("%d",&n))
    {
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
        }
        /*
        65-69行 这样写是wrong的 WA了很多次
        因为不能每次改变liss[0]成为正确的数值


        或者把liss[0]写到for循环里面 但是循环的初始i改成0 也是正确的
        */


        liss[0]=1;
        int max=0;
        for(int i=1;i<n;i++)//注意i的循环初始值是1
        {
            liss[i]=1;
            for(int j=0;j<i;j++)
            {
                if(a[i]>a[j]&&liss[j]+1>liss[i])
                {
                    liss[i]=liss[j]+1;
                }
            }
            if(max<liss[i])
            {
                max=liss[i];
            }
        }
        printf("%d\n",max);
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值