码迷,mamicode.com
首页 > 其他好文 > 详细

【HDU1257】最少拦截系统(贪心)

时间:2015-06-18 21:50:12      阅读:96      评论:0      收藏:0      [点我收藏+]

标签:

 

最少拦截系统

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


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

 

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

 

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

 

Sample Input
8 389 207 155 300 299 170 158 65
 

 

Sample Output
2
 

题解:贪心 从当前拦截系统中找出高度差最小的用来拦截 如果高度大于所有拦截系统则增加新的拦截系统

代码实现:

技术分享
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 
 6 const int inf = 0x3f3f3f;
 7 const int maxn = 30011;
 8 int dp[maxn];
 9 
10 int main(){
11     int a, b, n;
12     int ans;
13     bool flag;
14     while(scanf("%d", &n)!=EOF){
15         ans = 0;
16         while(n--){
17             a = inf;
18             scanf("%d", &b);
19             flag = false;
20             int temp;
21             for(int i = 0; i < ans; ++i){       //如果可以选出高度差最小的拦截系统
22                 if(b <= dp[i] && a > dp[i]-b){
23                     a = dp[i] - b;
24                     temp = i;
25                     flag = true;
26                 }
27             }
28 
29             if(!flag){          //更新拦截系统
30                 dp[ans] = b;
31                 ++ans;
32             }
33             else{
34                 dp[temp] = b;
35             }
36         }
37 
38         printf("%d\n", ans);
39     }
40     return 0;
41 }
Alpha

 

【HDU1257】最少拦截系统(贪心)

标签:

原文地址:http://www.cnblogs.com/auguralpha/p/4586974.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!