HDOJ 1257 最少拦截系统 【DP】

HDOJ 1257 最少拦截系统 【DP】

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 50753 Accepted Submission(s): 19895

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

Input
输入若干组数据.每组数据包括:导弹总个数(正整数),导弹依此飞来的高度(雷达给出的高度数据是不大于30000的正整数,用空格分隔)

Output
对应每组数据输出拦截所有导弹最少要配备多少套这种导弹拦截系统.

Sample Input

8 389 207 155 300 299 170 158 65

Sample Output

2

题意
求最大上升子序列

AC代码

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <cstdlib>
#include <ctype.h>
#include <numeric>
#include <sstream>
using namespace std;

typedef long long LL;
const double PI  = 3.14159265358979323846264338327;
const double E   = 2.718281828459;  
const double eps = 1e-6;
const int MAXN   = 0x3f3f3f3f;
const int MINN   = 0xc0c0c0c0;
const int maxn   = 1e4 + 5; 
const int MOD    = 1e9 + 7;
int arr[maxn], dp[maxn];

int main()
{
    int n;
    while (cin >> n)
    {
        for (int i = 0; i < n; i++)
            scanf("%d", &arr[i]);
        memset(dp, 0, sizeof(dp));
        int ans = 0;
        for (int i = 0; i < n; i++)
        {
            dp[i] = 1;
            for (int j= i - 1; j >= 0; j--)
            {
                if (arr[i] > arr[j])
                    dp[i] = max(dp[i], dp[j] + 1);
            }
            if (dp[i] > ans)
                ans = dp[i];
        }
        cout << ans << endl;
    }
}
原文地址:https://www.cnblogs.com/Dup4/p/9433373.html