【贪心】HDU 1257

时间:2023-03-09 03:53:29
【贪心】HDU 1257

HDU 1257 最少拦截系统

题意:中文题不解释。

思路:网上有说贪心有说DP,想法就是开一个数组存每个拦截系统当前最高能拦截的导弹高度。输入每个导弹高度的时候就开始处理,遍历每一个拦截系统,一旦最高拦截高度比它高,就把当前拦截系统的高度调整为它的高度,如果访问到末尾都没有能拦截的系统,那么拦截系统加一。

P.S:听说这题杭电数据有点水

/**
Sample Input
8 389 207 155 300 299 170 158 65 Sample Output
2
**/ #include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 300005;
int dp[maxn];
int n;
int main()
{
while(~scanf("%d",&n)){
memset(dp,0,sizeof(dp)); //仅仅需要dp[0] = 0就可以
int flag; //标记是否能被当前已有的拦截系统拦截
int x;
int res = 0;
for(int i=0;i<n;i++){
scanf("%d",&x);
flag = 1;
for(int j=0;j<=res;j++){
if(dp[j]>x){
dp[j] = x;
flag = 0;
break;
}
}
if(flag){
res++;
dp[res] = x;
}
}
printf("%d\n",res);
}
return 0;
}