POJ 2533 Longest Ordered Subsequence

简介: DescriptionA numeric sequence of ai is ordered if a1 < a2 < … < aN. Let the subsequence of the given numeric sequence (a1, a2, …, aN) be ...

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

用动态规划做,每次从后面对前面判断
用dd[k]表示以df[k]作为终点的最大上升子序列
则:
dd[1] = 1;
dd[k] = Max (dd[i]:1 <= i < k 且 df[i ]< df[k] 且 k != 1) + 1.
也就是第k+1前面一个不大于df[k]的数的dd[ ]的值;
n:7
i :0 1 2 3 4 5 6
df :1 7 3 5 9 4 8
dd[0]:1;
dd[1]:dd[0]+1=2;
dd[2]:dd[0]+1=2;
dd[3]:dd[2]+1=3;
dd[4]:因为df[0],df[1],df[2],df[3]都小于df[4],但是dd[3]最大,
所以,dd[4]=dd[3]+1=4;
dd[5]:dd[2]+1=3;
dd[6]:dd[5]+1=4;
…………………………………………………………

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define Maxn 1020
int df[Maxn],dd[Maxn];

int cmp(const void *x,const void *y){
    return (*(int *)y-*(int *)x);
    /*快速排序,从大到小排序*/
}
int main(){
    int n;
    while(scanf("%d",&n)==1){
        for(int i=0;i<n;i++){
            scanf("%d",&df[i]);
        }
        dd[0]=1;
        for(int i=1;i<n;i++){
                int t=0;
            for(int j=0;j<i;j++){
                if(df[i]>df[j]){
                    if(t<dd[j]){
                        t=dd[j];
                    }
                }
            }
            dd[i]=t+1;
            //此时的t是dd[i]之前的最大增子序列的个数
        }
        qsort(dd,n,sizeof(int),cmp);
        printf("%d\n",dd[0]);
    }
    return 0;
}
目录
相关文章
|
JSON 自然语言处理 Java
es索引、类型(mapping)、文档、ik分词器
es索引、类型(mapping)、文档、ik分词器
262 1
|
弹性计算 负载均衡 Kubernetes
slb的LoadBalancer
slb的LoadBalancer
220 2
|
JavaScript
Vue项目中强制刷新页面的方法
Vue项目中强制刷新页面的方法
680 0
|
5月前
|
Kubernetes 安全 应用服务中间件
IngressNightmare:Ingress Nginx 再曝5个安全漏洞,可接管你的 K8s 集群
是否还记得 2022 年 K8s Ingress Nginx 披露了的 3 个高危安全漏洞(CVE-2021-25745, CVE-2021-25746, CVE-2021-25748),并在那一年宣布停止接收新功能 PR,专注修复并提升稳定性。
|
弹性计算 应用服务中间件 Linux
阿里云ECS服务器快速搭建Docker环境
阿里云ECS服务器快速搭建Docker环境
883 0
阿里云ECS服务器快速搭建Docker环境
|
编译器 Linux C语言
ARM64上开启MTE
ARM64上开启MTE
|
安全 网络安全 PHP
RCE攻击绕过WAF详解
RCE攻击绕过WAF详解
195 3
阿里云怎么注册商标?(附详细商标注册申请操作流程)
阿里云商标注册分为商标智能注册申请、商标顾问注册申请和商标安心注册申请,本文阿小云以商标智能注册申请为例来详细说下阿里云商标申请图文操作流程:
6248 0
阿里云怎么注册商标?(附详细商标注册申请操作流程)
|
敏捷开发 Java 测试技术
阿里云云效产品使用合集之流水线、应用和项目集该如何迁移
云效作为一款全面覆盖研发全生命周期管理的云端效能平台,致力于帮助企业实现高效协同、敏捷研发和持续交付。本合集收集整理了用户在使用云效过程中遇到的常见问题,问题涉及项目创建与管理、需求规划与迭代、代码托管与版本控制、自动化测试、持续集成与发布等方面。
|
NoSQL 大数据 数据处理
MongoDB聚合框架与复杂查询优化:技术深度解析
【4月更文挑战第30天】本文深入探讨了MongoDB的聚合框架和复杂查询优化技术。聚合框架包含$match、$group、$sort和$project阶段,用于数据处理和分析,提供灵活性和高性能。优化查询涉及创建合适索引、使用聚合框架、简化查询语句、限制返回结果数、避免跨分片查询、只查询所需字段及使用$inc操作符。理解这些技术有助于提升MongoDB在大数据和复杂查询场景下的性能。