B - B
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit Status Practice HDU 1257
Description
某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能超过前一发的高度.某天,雷达捕捉到敌国的导弹来袭.由于该系统还在试用阶段,所以只有一套系统,因此有可能不能拦截所有的导弹.
怎么办呢?多搞几套系统呗!你说说倒蛮容易,成本呢?成本是个大问题啊.所以俺就到这里来求救了,请帮助计算一下最少需要多少套拦截系统.
Input
输入若干组数据.每组数据包括:导弹总个数(正整数),导弹依此飞来的高度(雷达给出的高度数据是不大于30000的正整数,用空格分隔)
Output
对应每组数据输出拦截所有导弹最少要配备多少套这种导弹拦截系统.
Sample Input
8 389 207 155 300 299 170 158 65
Sample Output
2
其实这道题有很多种解法,可以用dp,但是比较起来还是贪心容易写一些。
应为给出的导弹是按顺序的,每个导弹都要被拦住,这里要求最小的拦截系统的个数,所以我们每次拦截导弹时要遍历所有的拦截系统找出导弹和拦截系统高度差最小的那个系统,不能拦截了就把当前的系统放在数组里面,重新开一个拦截系统。
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <stdio.h>
#define LL long long
#define ULL unsigned long long
#define mod 1000000007
#define INF 0x7ffffff
using namespace std;
const int MAXN = 2000005;
const int N = 15;
int dp[MAXN];
int main()
{
int n;
while(scanf("%d",&n)!=EOF){
int sum=0;
while(n--){
int x;
scanf("%d",&x);
int Min=INF,flag=0,cot;
int i;
for(i=0;i<sum;i++){
if(x<=dp[i]&&Min>dp[i]-x){
Min=dp[i]-x;
flag=1;
cot=i;
}
}
if(flag==0){
dp[sum]=x;
sum++;
}
else dp[cot]=x;
}
printf("%d\n",sum);
}
return 0;
}