zoukankan      html  css  js  c++  java
  • [kuangbin带你飞]专题十二 基础DP1 I

    I - 最少拦截系统 HDU - 1257

    题目链接:https://vjudge.net/contest/68966#problem/I

    题目:

    某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能超过前一发的高度.某天,雷达捕捉到敌国的导弹来袭.由于该系统还在试用阶段,所以只有一套系统,因此有可能不能拦截所有的导弹.
    怎么办呢?多搞几套系统呗!你说说倒蛮容易,成本呢?成本是个大问题啊.所以俺就到这里来求救了,请帮助计算一下最少需要多少套拦截系统.

    Input输入若干组数据.每组数据包括:导弹总个数(正整数),导弹依此飞来的高度(雷达给出的高度数据是不大于30000的正整数,用空格分隔)
    Output对应每组数据输出拦截所有导弹最少要配备多少套这种导弹拦截系统.
    Sample Input
    8 389 207 155 300 299 170 158 65
    Sample Output
    2
    思路:dp[i]为当前导弹的拦截系统,当a[j]<a[i](j<i)时,dp[i]当前的值就为原来的值和dp[j]+1的值得最大值,因为这时候不满足那个拦截系统的高度范围了,其实相当于求最长上升子序列

    //
    // Created by hanyu on 2019/8/8.
    //
    #include <algorithm>
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <queue>
    #include <set>
    #include<math.h>
    #include<map>
    using namespace std;
    typedef long long ll;
    const int maxn=1e5+7;
    #define MAX 0x3f3f3f3f
    int main()
    {
        int dp[maxn];
        int a[maxn];
        int n;
        while(~scanf("%d",&n))
        {
            memset(dp,0,sizeof(dp));
            memset(a,0,sizeof(a));
            for(int i=0;i<n;i++) {
                scanf("%d", &a[i]);
                dp[i] = 1;
            }
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<i;j++)
                {
                    if(a[j]<a[i])
                        dp[i]=max(dp[i],dp[j]+1);
                }
            }
            int maxx=-1;
            for(int i=0;i<n;i++)
            {
                maxx=max(maxx,dp[i]);
            }
            printf("%d
    ",maxx);
        }
        return 0;
    }
    如果该题再继续问你一套系统最多可以拦截多少问导弹,相当于求最长下降子序列:
    //
    // Created by hanyu on 2019/8/8.
    //
    #include <algorithm>
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <queue>
    #include <set>
    #include<math.h>
    #include<map>
    using namespace std;
    typedef long long ll;
    const int maxn=1e5+7;
    #define MAX 0x3f3f3f3f
    int main()
    {
        int dp[maxn];
        int a[maxn];
        int n;
        while(~scanf("%d",&n))
        {
            memset(dp,0,sizeof(dp));
            memset(a,0,sizeof(a));
            for(int i=0;i<n;i++) {
                scanf("%d", &a[i]);
                dp[i] = 1;
            }
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<i;j++)
                {
                    if(a[j]>a[i])
                        dp[i]=max(dp[i],dp[j]+1);
                }
            }
            int maxx=-1;
            for(int i=0;i<n;i++)
            {
                maxx=max(maxx,dp[i]);
            }
            printf("%d
    ",maxx);
        }
        return 0;
    }

  • 相关阅读:
    ehcache memcache redis 三大缓存男高音
    tomcat启用压缩的方式
    Linux rpm 命令参数使用详解[介绍和应用]
    rpm常用命令及rpm参数介绍
    RPM 命令大全
    BZOJ2298: [HAOI2011]problem a(带权区间覆盖DP)
    BZOJ2037: [Sdoi2008]Sue的小球(区间DP)
    HDU3507 Print Article(斜率优化DP)
    线性代数学习笔记(几何版)
    HDU 2065 "红色病毒"问题(生成函数)
  • 原文地址:https://www.cnblogs.com/Vampire6/p/11320813.html
Copyright © 2011-2022 走看看