关闭

POJ 2533

标签: poj
1019人阅读 评论(0) 收藏 举报
分类:

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

题意: 给一队排列整齐的数列ai,找到数列ai中最长上升子序列。输出最长最长上升子序列的长度

思路:用动态规划做。用dp[k]表示以array[k]作为终点的最大上升子序列则:dp[1] = 1;dp[k] = Max (dp[i]:1 <= i < k 且 a[i ]< a[k] 且 k != 1) + 1.


import java.util.Scanner;

public class Main{
    private static int num = 0;

    private static int[] array = new int[10000];

    private static int[] dp = new int[10000];

    public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    while (scanner.hasNext()) {
    num = scanner.nextInt();
    for (int i = 1; i <= num; i++) {
        array[i] = scanner.nextInt();
    }
    dp[1] = 1;
    for (int i = 2; i <= num; i++) {
        int temp = 0;
        for (int j = 1; j < i; j++) {
        if (array[i] > array[j]) {
            if (temp < dp[j]) {
            temp = dp[j];
            }
        }
        }
        dp[i] = temp + 1;
    }
    int maxlen = 0;
    for (int i = 1; i <=num; i++) {
        System.out.println(dp[i]);
    }
    for (int i = 1; i <= num; i++) {
        maxlen = maxlen >= dp[i] ? maxlen : dp[i];
    }
    System.out.println(maxlen);
    }
    scanner.close();
    }
}
0
0
查看评论

POJ2533(Longest Ordered Subsequence 最长公共子序列 DP或单调队列+二分)

Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 34454   Accepted: 15135 Description A num...
  • u013021513
  • u013021513
  • 2014-11-16 20:20
  • 840

poj2533DP最长上升序列

Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 43875   Accepted:&...
  • sinat_19628145
  • sinat_19628145
  • 2016-04-15 17:48
  • 135

poj 2533(最长上升子序列)(n^2 ) 和 nlogn的算法

题目链接:点击打开链接 题目大意:略 题目分析:1.用n平方的做法,枚举以data【i】为结尾的子序列,向前查找,比data【i】小并且dp【j】比dp【i】小的 新手代码: #include #include #include #include using namespace std; cons...
  • u011613321
  • u011613321
  • 2013-08-26 15:27
  • 751

poj 2533(简单动态规划)

Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 31276   Accepted:&...
  • my_acm
  • my_acm
  • 2014-05-12 11:00
  • 421

poj2533 简单DP LIS

最长上升子序列(LIS) dp[ i ]以序列中第i个元素结尾的最长上升子序列的长度 那么状态转移方程为:if (a[i] > a[j]) dp[i] = MAX (dp[i], dp[j] + 1); #include #include using namespace std;...
  • Non_Cease
  • Non_Cease
  • 2012-02-29 21:32
  • 2503

POJ 2533 O(nlogn)解最长递增子序列(构造法)

Longest Ordered Subsequence Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 41083 Accepted: 18109 DescriptionA numeric seque...
  • qq_22902423
  • qq_22902423
  • 2015-11-19 19:22
  • 241

poj--2533

简单dp 思路见下面
  • acm_lkl
  • acm_lkl
  • 2014-08-12 20:18
  • 255

poj2533解题报告

题目描述请见:http://acm.pku.edu.cn/JudgeOnline/problem?id=2533基本思想:DP犯过的错误:最开始定义状态dp[i]是从第一个元素到第i个元素为止的最长子序列长度,但这样做无法确定从第一个元素到第i个元素为止的最长子序列中,最大元素的位置,也就无法与后续...
  • silencelee
  • silencelee
  • 2007-08-10 21:29
  • 409

poj3983 算24点

#include #include using namespace std; char cf[4]={'+','-','*','/'}; double f(double a,double b,int i){ if(i==0) ...
  • u010738033
  • u010738033
  • 2014-02-26 19:35
  • 876

D - Longest Ordered Subsequence POJ - 2533

A numeric sequence of ai is ordered if a1 a2 aN. Let the subsequence of the given numeric sequence ( a1, a2, ...,...
  • gtuif
  • gtuif
  • 2017-07-26 19:40
  • 74
    个人资料
    • 访问:112184次
    • 积分:1387
    • 等级:
    • 排名:千里之外
    • 原创:41篇
    • 转载:3篇
    • 译文:0篇
    • 评论:32条
    博客专栏
    最新评论