【HDU - 1257】最少拦截系统

最少拦截系统

Descriptions:

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

Input

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

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

8 389 207 155 300 299 170 158 65

Sample Output

2

题目链接:

https://vjudge.net/problem/HDU-1257

用导弹的高度来决定拦截系统的高度,然后每次都更新所有的已经存在的拦截系统的高度,如果没有大于等于导弹的高度的拦截系统,就添加一个拦截系统,然后继续遍历,直到遍历完为止

AC代码

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#define mod 1000000007
#define eps 1e-6
#define ll long long
#define INF 0x3f3f3f3f
#define ME0(x) memset(x,0,sizeof(x))
using namespace std;
int n;
int a[100005];//更新拦截系统能够拦截导弹的最高高度
int main()
{
    while(cin>>n)
    {
        ME0(a);
        int sum=0;//拦截系统的个数
        while(n--)
        {
            int x;
            cin >> x;
            int i;
            for(i=0; i<=sum; ++i)
            {
                if(x<=a[i])//小于当前拦截系统的拦截最高高度就更新高度
                {
                    a[i]=x;
                    break;
                }
            }
            if(i>sum)//如果拦截系统都不能够拦截,就只能够再重新用一个拦截系统了
                a[++sum]=x;
            //每次都将操作系统的能够拦截的最高高度进行一次排序
            sort(a,a+sum);
//            for(int i=1; i<=sum; i++)//从sum=1开始计数的
//                cout<<a[i]<<" ";
//            cout<<endl;
        }
        cout<<sum<<endl;
    }
}

猜你喜欢

转载自www.cnblogs.com/sky-stars/p/11043608.html