您的位置:首页 > 其它

最少拦截系统 hdu 1257 最长有序子序列

2013-01-18 10:21 204 查看
http://acm.hdu.edu.cn/showproblem.php?pid=1257

对于这个题目的测试例的解释:当8枚导弹依次飞来时,对于前三枚导弹(300,207,155),用第一套系统即可拦截,每一次更新记录,即:对第一枚导弹直接拦截,第二枚导弹来临时也用第一套系统拦截,记录改变成207,当用第一套系统拦截第三枚后记录修改为155,当第四枚导弹来时按照系统规定,第一套系统无法实现对第四枚导弹的拦截任务,因此,开第二套系统,为了使用最少系统,必须对以后的每一枚导弹贪心,即:拿这枚导弹和以前的导弹最小记录依次做比较,寻找一个和这枚导弹高度最接近的记录并用这枚导弹的高度替换原记录,最后记录数组中的个数就是最少需要的系统数。

#include <iostream>
#include <algorithm>
using namespace std;
#define N 1005
#define MAX 30005

int missile_intercept
;

void arrange(int &top,int missile_height){
int i;
sort(missile_intercept,missile_intercept+top);//sort函数对数组missile_intercept[0]~missile_intercept[top-1]之间的数按从小到大的顺序排序。
for (i=0;i<top;i++)
if (missile_intercept[i]>missile_height){
missile_intercept[i]=missile_height;
break;
}
if (i==top){
missile_intercept[top]=missile_height;
top++;
}
}

int main(){
int n,top,missile_height;
while (scanf("%d",&n)!=EOF){
missile_intercept[0]=MAX;
top=1;
while (n--){
scanf("%d",&missile_height);
arrange(top,missile_height);
}
printf("%d\n",top);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: