zoukankan      html  css  js  c++  java
  • HDU1257 最少拦截系统

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1257

    感觉这题有些扯淡,难道要在敌军导弹发射后发现自己的拦截系统够不到的时候再去搞一套导弹拦截系统?提前准备多套导弹系统的花销是少不了的。只能是在使用的时候,在保证所有导弹被成功拦截的前提下,出动最少的数量导弹拦截系统。

     1 #include <iostream>
     2 #include <cstring>
     3 using namespace std;
     4 //导弹拦截系统最大数目,假定为1000 
     5 #define N 1000
     6 int main(){
     7     int n;
     8     //存放第i号拦截导弹系统能达到的高度 
     9     int daodan[N];
    10     //初始化高度为30005 ,因为敌军导弹高度不超过30000 
    11     memset(daodan,30005,sizeof(daodan));
    12     while(cin>>n){
    13         //height 存储当前敌军导弹所在的高度 
    14         //num 要出动的导弹拦截系统个数 
    15         int height=0; 
    16         int num=0;
    17         for(int i=0;i<n;i++){
    18             cin>>height;
    19             //下面开始贪心策略
    20             //选择离该敌军导弹高度最近的拦截导弹系统
    21             //mingap 为最小间隔
    22             //index 存储最适合的导弹拦截系统编号 
    23             int mingap=30005,index=-1;
    24             for(int j=1;j<=num;j++){
    25                 //gap 当前导弹系统与当前敌军导弹的高度差 
    26                 int gap=daodan[j]-height;
    27                 //大于0 说明能拦截到,小于零说明拦截不到 
    28                 if(gap>0){
    29                     if(mingap>gap){
    30                         mingap=gap;
    31                         index=j;
    32                     }
    33                 }
    34             }
    35             //index==-1 说明当前出动的导弹,都拦截不到该敌军导弹
    36             //这种情况下就需要出动新一台导弹拦截系统 
    37             if(index!=-1)
    38                 daodan[index]=height;
    39             else{
    40                 num++;
    41                 daodan[num]=height;
    42             }
    43         }
    44         cout<<num<<endl;
    45     } 
    46     return 0;
    47 } 
  • 相关阅读:
    1.Mybatis的全局配置mybatis-config.xml
    01淘淘商城项目:项目Maven工程搭建
    Connection timed out: connect; Communications link failure
    启动maven项目的配置
    PLSQL 触发器概念
    Git 概念&常用命令
    Git与svn的区别 & Git的工作流程
    Redis 是如何存储的
    Redis 概念,常用命令
    idea 快捷键
  • 原文地址:https://www.cnblogs.com/vmoor2016/p/6611595.html
Copyright © 2011-2022 走看看