poj 2533:Longest Ordered Subsequence 求最长递增序列长度

这道题是动态规划入门题目,这里有两个思路

题目如下:

总时间限制:
2000ms
内存限制:
65536kB

描述
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.
输入
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 file must contain a single integer - the length of the longest ordered subsequence of the given sequence.

题目:输入一个n和n个数字
输出:最长的递增子序列的长度

样例输入
7
1 7 3 5 9 4 8

样例输出
4

第一种,用其他的值更新自己的方法

c++版

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
    int n,t;
    vector<int> n_v;
    vector<int> n_r;
    cin>>n;
    for (int i = 0; i < n; ++i) {
        cin>>t;
        n_v.push_back(t);
        n_r.push_back(1);
    }

    for (int i = 1; i < n; ++i) {
        int maxs=0;
        for (int j = 0; j < i; ++j) {
            //产生第i个数字的最长递增自序列
            if(n_v[i]>n_v[j]){
                //对于第j个值,有可能更新i的最长子序列
                maxs = max(maxs, n_r[j]);
            }
        }
        n_r[i] = maxs+1;
    }
    int x = 0;
    for (int k = 0; k < n; ++k) {
        if(n_r[k]>x) x = n_r[k];
    }
    cout<<x<<endl;
    return 0;
}
/*

7
1 7 3 5 9 4 8
 */

这里写图片描述

java版

import java.util.*;

public class Main {

    public static void main(String args[]) {
        ArrayList n_v = new ArrayList();
        ArrayList r_v = new ArrayList();
        int n, t;
        Scanner s = new Scanner(System.in);
        n = s.nextInt();
        for (int i = 0; i < n; i++) {
            r_v.add(1);
            n_v.add(s.nextInt());
        }
        for (int i = 1; i < n; i++) {
            for (int j = 0; j < i; j++) {
                if((int)n_v.get(i)>(int)n_v.get(j)){
                    int maxs = Math.max((int)r_v.get(j)+1, (int)r_v.get(i));
                    r_v.set(i, maxs);
                }
            }
        }
        t = 0;
        for (int i = 0; i < n; i++) {
            if ((int)r_v.get(i)>t) t = (int)r_v.get(i);
        }
        System.out.println(t);
    }
}
/*

7
1 7 3 5 9 4 8
*/

这里写图片描述

第二种方法:用自身更新别的值:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
    int n,t;
    vector<int> n_v;
    vector<int> n_r;
    cin>>n;
    for (int i = 0; i < n; ++i) {
        cin>>t;
        n_v.push_back(t);
        n_r.push_back(1);
    }

    for (int i = 0; i < n; ++i) {
        for (int j = i; j < n; ++j) {
            if(n_v[i]<n_v[j]){
                n_r[j] = max(n_r[j], n_r[i]+1);
            }
        }
    }

    int x = 0;
    for (int k = 0; k < n; ++k) {
        if(n_r[k]>x) x = n_r[k];
    }
    cout<<x<<endl;
    return 0;
}
/*

7
1 7 3 5 9 4 8
 */

这里写图片描述

java版

import java.util.*;

public class Main {

    public static void main(String args[]) {
        ArrayList n_v = new ArrayList();
        ArrayList r_v = new ArrayList();
        int n, t;
        Scanner s = new Scanner(System.in);
        n = s.nextInt();
        for (int i = 0; i < n; i++) {
            r_v.add(1);
            n_v.add(s.nextInt());
        }
        for (int i = 0; i < n; i++) {
            for (int j = i+1; j < n; j++) {
                if ((int)n_v.get(j)>(int)n_v.get(i)){
                    int maxs = Math.max((int)r_v.get(j), (int)r_v.get(i)+1);
                    r_v.set(j, maxs);
                }
            }
        }
        t = 0;
        for (int i = 0; i < n; i++) {
            if ((int)r_v.get(i)>t) t = (int)r_v.get(i);
        }
        System.out.println(t);
    }
}
/*

7
1 7 3 5 9 4 8
*/

这里写图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值