【算法练习】动态规划 DP 百炼 poj2945:拦截导弹 poj1836:Alignment

题目地址:http://bailian.openjudge.cn/practice/2945/

2945:拦截导弹

总时间限制: 

1000ms

内存限制: 

65536kB

描述

某国为了防御敌国的导弹袭击,开发出一种导弹拦截系统。但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能高于前一发的高度。某天,雷达捕捉到敌国的导弹来袭,并观测到导弹依次飞来的高度,请计算这套系统最多能拦截多少导弹。拦截来袭导弹时,必须按来袭导弹袭击的时间顺序,不允许先拦截后面的导弹,再拦截前面的导弹。

输入

输入有两行,
第一行,输入雷达捕捉到的敌国导弹的数量k(k<=25),
第二行,输入k个正整数,表示k枚导弹的高度,按来袭导弹的袭击时间顺序给出,以空格分隔。

输出

输出只有一行,包含一个整数,表示最多能拦截多少枚导弹。

样例输入

8
300 207 155 300 299 170 158 65

样例输出

6

理解题意:其实就是在写下面的题的时候看了《王道》的LIS,最长上升子序列和最长不增子序列~

设dp[i]表示前i个数字中最长不增子序列的长度

dp[1]=1

dp[ i ] = max(1,dp[ j ]+1)   j<i ,aj≥ai

AC代码如下

#include <stdio.h>
#include <math.h>
using namespace std;
const int maxn=35;
//最长不下降子序列
//
int k;
int a[maxn],dp[maxn];
int main(){
    scanf("%d",&k);
    dp[0]=0;
    for(int i=1;i<=k;i++){
        scanf("%d",&a[i]);
        dp[i]=1;
    }
    for(int i=1;i<=k;i++){
        for(int j=1;j<i;j++){
            if(a[j]>=a[i] && dp[j]+1>dp[i]){
                dp[i]=dp[j]+1;
            }
        }
    }
    int ans=1;
    for(int i=1;i<=k;i++){
        if(dp[i]>ans)
            ans=dp[i];
    }
    printf("%d",ans);
    return 0;
}

 

题目地址:http://bailian.openjudge.cn/practice/1836

1836:Alignment

总时间限制: 

1000ms

内存限制: 

65536kB

描述

In the army, a platoon is composed by n soldiers. During the morning inspection, the soldiers are aligned in a straight line in front of the captain. The captain is not satisfied with the way his soldiers are aligned; it is true that the soldiers are aligned in order by their code number: 1 , 2 , 3 , . . . , n , but they are not aligned by their height. The captain asks some soldiers to get out of the line, as the soldiers that remain in the line, without changing their places, but getting closer, to form a new line, where each soldier can see by looking lengthwise the line at least one of the line's extremity (left or right). A soldier see an extremity if there isn't any soldiers with a higher or equal height than his height between him and that extremity.

Write a program that, knowing the height of each soldier, determines the minimum number of soldiers which have to get out of line.

输入

On the first line of the input is written the number of the soldiers n. On the second line is written a series of n floating numbers with at most 5 digits precision and separated by a space character. The k-th number from this line represents the height of the soldier who has the code k (1 <= k <= n).

There are some restrictions:
• 2 <= n <= 1000
• the height are floating numbers from the interval [0.5, 2.5]

输出

The only line of output will contain the number of the soldiers who have to get out of the line.

样例输入

8
1.86 1.86 1.30621 2 1.4 1 1.97 2.2

样例输出

4

理解题意:英文题面大概意思就是说求去掉的最少人数,让剩下的数组升序/降序或者前升序后降序。

我的理解,每个士兵都能看到左或者右边最端点的位置就只有上述几种排列可能。

样例就是

1.86 1.86 1.30621 2 1.4 1 1.97 2.2

去掉四个元素之后就是上升子序列

但是没有什么确实可行的思路:(

看了一下blog,其实思路就是求这个序列的上升子序列dp1  和下降子序列dp2(下降就是反着求上升,也就是 a[n]..a[i]的最长上升子序列)

然后dp1 + dp2

就是数组a[1,...i]最长上升子序列长度加上a[i+1,...n]最长降低子序列长度的最大值!妙啊

AC代码:

#include<stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;
const int maxn=1010;
int n;
double a[maxn];
int dp1[maxn],dp2[maxn];
int main(){
    scanf("%d",&n);
    memset(dp1,0,sizeof(dp1));
    memset(dp2,0, sizeof(dp2));

    for(int i=1;i<=n;i++){
        scanf("%lf",&a[i]);
    }

    //1-n 最长上升子序列
    for(int i=1;i<=n;i++){
        dp1[i]=1;
        for(int j=1;j<i;j++){
            if(a[j]<a[i] && dp1[i]<dp1[j]+1){
                dp1[i]=dp1[j]+1;
            }
        }
    }

    //n-1 最长上升子序列    dp2[i]i->n的最长下降子序列
    for(int i=n;i>=1;i--){
        dp2[i]=1;
        for(int j=n;j>i;j--){
            if(a[j]<a[i] && dp2[i]<dp2[j]+1){
                dp2[i]=dp2[j]+1;
            }
        }
    }

    int ans=1;
    for(int i=1;i<=n;i++){
        for(int j=i+1;j<=n;j++){
            ans=max(ans,dp1[i]+dp2[j]);
        }
    }
    printf("%d\n",n-ans);


    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值