HDU_1257 最少拦截系统 (dp)

来源:互联网 发布:淘宝商城墙纸 编辑:程序博客网 时间:2024/06/05 15:48

题目请点我
题解:
这道题是一道很简单的题目,但是比赛的时候卡了很久。发现自己总是在比赛的时候不会快速的去快速建模,卡死到一道题上面。这次比赛最终竟只做出了这一道题,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 longusing 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 longusing 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 longusing 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 0
原创粉丝点击