HDOJ.1257 最少拦截系统 (贪心)

最少拦截系统

点我挑战题目

题意分析

一开始理解错了这道题。这么多个导弹排好序不只需要1个拦截系统吗。后来发现自己真傻。那出这个题还有啥意思,反正都需要一个。(;′⌒`)
给出n个导弹,这n个导弹的顺序是不能改变的。并且对于每个拦截系统来说,他所能打到的高度只能越来越小不能增大(或保持不变)。那么对于每个导弹来说,要先判断当前所有的导弹系统中有没有能够拦截的,如果没有的话,直接新增一个拦截系统;如果有的话,那么看一下能打到的拦截系统中高度最小的那个,把它的拦截数据更新为当前导弹的高度(这是这道题的贪心策略)。最后看看,总共有几个拦截系统即可。

代码总览

/*
    Title:HDOJ.1257
    Author:pengwill
    Date:2016-11-25
*/
#include <iostream>
#include <algorithm>
#include <stdio.h>
#define max 10001
using namespace std;

int main()
{
    int n;
    int a[10001] = {0};
    while(scanf("%d",&n) != EOF){
        int i,j,hight;
        for(i = 0,j = 0;i<n;i++){
            scanf("%d",&hight);
            if(!i){
                a[j] = hight;
                j++;
            }else{
                int k,temp = -1,minh = -1;
                for(k = 0;k<j;k++){
                    if(a[k]>=hight){
                        if(minh == -1||a[k]<minh){
                            temp = k;
                            minh = a[k];
                        }
                    }
                }
                if(temp==-1){
                        a[j] = hight;
                        j++;
                    }else{
                        a[temp] = hight;
                    }
            }
        }
        printf("%d
",j);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/pengwill/p/7367202.html