HDU_1257 最少拦截系统 (dp)

题目请点我
题解:
这道题是一道很简单的题目,但是比赛的时候卡了很久。发现自己总是在比赛的时候不会快速的去快速建模,卡死到一道题上面。这次比赛最终竟只做出了这一道题,really sucksssss. 练的太少了。。。
这道题其实有很多种方法可以解:
1.暴力
每设置一个标杆从头到尾遍历一次,将所有能消灭的导弹打掉,看最后需要遍历几次;
2.贪心
设置一个标杆数组,每次从头判断使用那个标杆去更新,如果不能更新就新设置一个标杆。最终结果为标杆的数目;
3.dp
最长上升子序列;
测试数据:

5 5 4 3 2 1
0
3 1 2 3
4 1 2 1 2
5 1 2 1 2 1
5 2 1 2 1 2
1 2
1 5
10 1 2 3 4 5 6 7 8 9 10
6 3 5 2 4 1 3

代码实现:

1.暴力
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#define INF 30010
#define MAX 100000
#define LL long long

using namespace std;

LL num;
LL result;
int lis[MAX];
int main()
{
    //freopen("in.txt","r",stdin);
    while( scanf("%I64d",&num) != EOF ){
        result = 0;
        int last;
        bool flag;
        for( int i = 0; i < num; i++ ){
            scanf("%d",&lis[i]);
        }
        while( tmp < num ){
            last = -1;
            flag = true;
            for( int i = 0; i < num; i++ ){
                if( lis[i] == -1 ){
                    continue;
                }
                if( lis[i] > last && flag ){
                    last = lis[i];
                    lis[i] = -1;
                    tmp ++;
                    flag = false;
                    result ++;
                }
                else if( lis[i] > last ){
                    continue;
                }
                else if( lis[i] <= last ){
                    last = lis[i];
                    lis[i] = -1;
                    tmp++;
                }
            }
        }
        printf("%I64d\n",result);
    }
    return 0;
}
2.贪心
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#define INF 30010
#define MAX 100000
#define LL long long

using namespace std;

LL num;
LL result;
int tag[MAX];
int main()
{
    //freopen("in.txt","r",stdin);
    while( scanf("%I64d",&num) != EOF ){
        result = 0;
        memset(tag,-1,sizeof(tag));
        for( LL i = 0; i < num; i++ ){
            int tmp;
            bool flag = false;
            scanf("%d",&tmp);
            for( int i = 0 ; i < result; i++ ){
                if( tmp <= tag[i] ){
                    tag[i] = tmp;
                    flag = true;
                    break;
                }
            }
            if( !flag ){
                tag[result] = tmp;
                result++;
            }
        }
        printf("%I64d\n",result);
    }
    return 0;
}
3.dp
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#define INF 30010
#define MAX 100000
#define LL long long

using namespace std;

LL num;
int result;
int dp[MAX];
int lis[MAX];
int main()
{
    //freopen("in.txt","r",stdin);
    while( scanf("%I64d",&num) != EOF ){
        result = 0;
        memset(dp,0,sizeof(dp));
        memset(lis,0,sizeof(lis));
        if( num == 0 ){
            printf("%d\n",0);
            continue;
        }
        for( int i = 0; i < num; i++ ){
            scanf("%d",&lis[i]);
        }
        for( int i = 0 ; i < num; i++ ){
            dp[i] = 1;
            for( int j = 0; j < i; j++ ){
                if( lis[i] > lis[j] ){
                    dp[i] = max(dp[i],dp[j]+1);
                }
            }
            result = max(result,dp[i]);
        }
        printf("%d\n",result);
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值