【动态规划初级】longest increasing subsequence

一个序列有N个数:A[1],A[2],A[3]……A[N],求最长非降子序列的长度。

最重要的是要找出所谓的“状态”,本题目中是d[i],初始化最长长度为自己本身即一个单位长度。

看是否要加入第i个元素,如果第i个元素a[i]比当前序列的最后一个元素a[j]大的话,那么加入,同时d[i]=d[j]+1;体会d[j]+1>d[i]
,比如,对于序列【5,3,4,8,6,7】当i=3,即a[i]=8的时候,j=0,8比5大,d[3]=d[0]+1=2;j=1,8比3大,但是d[1]+1=2不比d[3]大就不能加一;j=2,8>4,d[2]+1=3>d[3],所以d[3]=d[2]+1;len保存的是前i个元素中的最大非降子序列的长度。

怎么能好好体会其中的思想而不去死记硬背,勉强自己去接受这一想法呢!

#include <iostream>
using namespace std;

int lis(int
a[],int n){
    int
d[n];
    int len=1;
    for(int i=0;i<n;i++){
   
    d[i]=1;
        for(int j=0;j<i;j++){
   
        if(a[j]<=a[i]&&d[j]+1>d[i]){
           
    d[i]=d[j]+1;
     
      }
            if(d[i]>len)len=d[i];

        }
 
  }
    return
len;
}
int main()
{
 
  int a[]={5,3,4,8,6,7};
    cout<<lis(a,6)<<endl;
 
  return 0;
}

LIS拓展










 Problem Statement for ZigZag





































































Problem
Statement

    

A sequence of numbers is called
zig-zag
sequence
 if the differences between successive numbers
strictly alternate between positive and negative. The first
difference (if one exists) may be either positive or negative. A
sequence with fewer than two elements is trivially a zig-zag
sequence.

For example, 1,7,4,9,2,5 is a zig-zag
sequence because the differences (6,-3,5,-7,3) are alternately
positive and negative. In contrast, 1,4,7,2,5 and 1,7,4,5,5
are not zig-zag
sequences, the first because its first two differences are positive
and the second because its last difference is zero.

Given a sequence of integers, sequence,
return the length of the longest subsequence of sequence that
is a zig-zag sequence. A subsequence is obtained by deleting some
number of elements (possibly zero) from the original sequence,
leaving the remaining elements in their original order.

 

Definition

    


















Class: ZigZag
Method: longestZigZag
Parameters: int[]
Returns: int
Method signature: int longestZigZag(int[] sequence)
(be sure your method is
public)
    
 

Constraints

- sequence contains
between 1 and 50 elements, inclusive.
- Each element of sequence is
between 1 and 1000, inclusive.
 

Examples

0)
    











{ 1, 7, 4, 9, 2, 5
}

Returns: 6





The entire sequence is a
zig-zag
sequence.
1)
    











{ 1, 17, 5, 10, 13, 15, 10, 5, 16, 8
}

Returns: 7





There are several
subsequences that achieve this length. One is
1,17,10,13,10,16,8.
2)
    











{ 44 }

Returns: 1

3)
    











{ 1, 2, 3, 4, 5, 6, 7, 8, 9
}

Returns: 2

4)
    











{ 70, 55, 13, 2, 99, 2, 80, 80, 80, 80, 100, 19, 7,
5, 5, 5, 1000, 32, 32 }

Returns: 8

5)
    











{ 374, 40, 854, 203, 203, 156, 362, 279, 812, 955,

600, 947, 978, 46, 100, 953, 670, 862, 568, 188,

67, 669, 810, 704, 52, 861, 49, 640, 370, 908,

477, 245, 413, 109, 659, 401, 483, 308, 609, 120,

249, 22, 176, 279, 23, 22, 617, 462, 459, 244
}

Returns: 36



This problem statement is the exclusive and
proprietary property of TopCoder, Inc. Any unauthorized use or
reproduction of this information without the prior written consent of
TopCoder, Inc. is strictly prohibited. (c)2010, TopCoder, Inc. All rights
reserved.

来源: <http://community.topcoder.com/stat?c=problem_statement&pm=1259>

#include<stdio.h>
#include<iostream>
using namespace std;

#define LEN 100

int
a[LEN];
int d[LEN];
int
lis(int n){
    if(n==1){return 0;}
 
  int len=0;
    for(int i=0;i<n;i++){
   
    d[i]=1;
    }
    for(int i=0;i<n;i++){
   
    for(int j=0;j<i;j++){
   
        if(j>0&&i>0&&(a[j-1]-a[i-1])*(a[j]-a[i])<0 && d[j]+1 >d[i])
           
    d[i]=d[j]+1;
     
      if(len<d[i])len=d[i];
        }
   
}
    return len;
}
int main(){
    int n;
    freopen("in.txt","r",stdin);
   while(cin>>n){

    for(int i=0;i<n;i++)
   
    cin>>a[i];
   
    cout<<lis(n)+1<<endl;
 
 }
return 0;
}

来自为知笔记(Wiz)

时间: 2024-11-06 07:28:30

【动态规划初级】longest increasing subsequence的相关文章

Dynamic Programming | Set 3 (Longest Increasing Subsequence)

在 Dynamic Programming | Set 1 (Overlapping Subproblems Property) 和 Dynamic Programming | Set 2 (Optimal Substructure Property) 中我们已经讨论了重叠子问题和最优子结构性质,现在我们来看一个可以使用动态规划来解决的问题:最长上升子序列(Longest Increasing Subsequence(LIS)). 最长上升子序列问题,致力于在一个给定的序列中找到一个最长的子序列

[LeetCode][JavaScript]Longest Increasing Subsequence

Longest Increasing Subsequence Given an unsorted array of integers, find the length of longest increasing subsequence. For example,Given [10, 9, 2, 5, 3, 7, 101, 18],The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Not

LeetCode 300. Longest Increasing Subsequence

300. Longest Increasing Subsequence Description Submission Solutions Add to List Total Accepted: 64115 Total Submissions: 170859 Difficulty: Medium Contributors: Admin Given an unsorted array of integers, find the length of longest increasing subsequ

【LeetCode从零单刷】Longest Increasing Subsequence

题目: Given an unsorted array of integers, find the length of longest increasing subsequence. For example, Given [10, 9, 2, 5, 3, 7, 101, 18], The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Note that there may be more

LintCode刷题笔记--Longest Increasing Subsequence

动态规划 描述: Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return the length of the LIS. Clarification What's the definition of longest increasing subsequence? The longest increasing subsequence problem is t

【Leetcode】Longest Increasing Subsequence

题目链接:https://leetcode.com/problems/longest-increasing-subsequence/ 题目: Given an unsorted array of integers, find the length of longest increasing subsequence. For example, Given [10, 9, 2, 5, 3, 7, 101, 18], The longest increasing subsequence is [2,

leetcode 之 Longest Increasing Subsequence

题目描述: Given an unsorted array of integers, find the length of longest increasing subsequence. For example, Given [10, 9, 2, 5, 3, 7, 101, 18], The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Note that there may be mor

中南OJ1551: Longest Increasing Subsequence Again(分块+离散化线段树)

1551: Longest Increasing Subsequence Again Time Limit: 2 Sec  Memory Limit: 256 MB Submit: 29  Solved: 15 [Submit][Status][Web Board] Description Give you a numeric sequence. If you can demolish arbitrary amount of numbers, what is the length of the

300. Longest Increasing Subsequence

Problem statement: Given an unsorted array of integers, find the length of longest increasing subsequence. For example,Given [10, 9, 2, 5, 3, 7, 101, 18],The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Note that there

SPOJ LIS2 Another Longest Increasing Subsequence Problem 三维偏序最长链 CDQ分治

Another Longest Increasing Subsequence Problem Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=19929 Description Given a sequence of N pairs of integers, find the length of the longest incre