POJ - 2533 Longest Ordered Subsequence与HDU - 1257 最少拦截系统 DP+贪心(最长上升子序列及最少序列个数)(LIS)...

Longest Ordered Subsequence

A numeric sequence of  ai is ordered if  a1 <  a2 < ... <  aN. Let the subsequence of the given numeric sequence (  a1a2, ...,  aN) be any sequence (  ai1ai2, ...,  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


LIS最长上升子序列问题。(可加二分优化O(nlogn))f[i]记录以当前值作为最后一个数时的最大长度。每枚举一个数都找前面比他小且f[i]最大的状态+1。f[i]=max(f[j])+1

#include<stdio.h>
#include<string.h>

int f[1005],a[1005];

int max(int x,int y)
{
    return x>y?x:y;
}

int main()
{
    int n,i,j;
    scanf("%d",&n);
    memset(a,0,sizeof(a));
    memset(f,0,sizeof(f));
    for(i=1;i<=n;i++){
        scanf("%d",&a[i]);
    }
    f[1]=1;
    for(i=2;i<=n;i++){
        for(j=1;j<i;j++){
            if(a[j]<a[i]){
                f[i]=max(f[i],f[j]);
            }
        }
        f[i]++;
    }
    int ans=0;
    for(i=1;i<=n;i++){
        ans=max(ans,f[i]);
    }
    printf("%d\n",ans);
    return 0;
}
View Code
 

最少拦截系统

某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能超过前一发的高度.某天,雷达捕捉到敌国的导弹来袭.由于该系统还在试用阶段,所以只有一套系统,因此有可能不能拦截所有的导弹. 
怎么办呢?多搞几套系统呗!你说说倒蛮容易,成本呢?成本是个大问题啊.所以俺就到这里来求救了,请帮助计算一下最少需要多少套拦截系统. 
 
Input
输入若干组数据.每组数据包括:导弹总个数(正整数),导弹依此飞来的高度(雷达给出的高度数据是不大于30000的正整数,用空格分隔) 
Output
对应每组数据输出拦截所有导弹最少要配备多少套这种导弹拦截系统. 
 
Sample Input
8 389 207 155 300 299 170 158 65
Sample Output
2

贪心解决最少序列个数问题。数组记录每个序列的最小值,出现比他大的数存入新下标,数组长度即为个数。

#include<stdio.h>
#include<string.h>

int a[10005];

int main()
{
    int n,x,c,i,j;
    while(~scanf("%d",&n)){
        c=1;
        memset(a,0,sizeof(a));
        a[1]=30001;
        for(i=1;i<=n;i++){
            scanf("%d",&x);
            for(j=1;j<=c;j++){
                if(a[j]>=x){
                    a[j]=x;
                    break;
                }
                if(j==c){
                    a[++c]=x;
                    break;
                }
            }
        }
        printf("%d\n",c);
    }
    return 0;
}
View Code
 

转载于:https://www.cnblogs.com/yzm10/p/7367975.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值