hdu 1257 最少拦截系统(简单dp+贪心)
2017-05-13 18:20
471 查看
题目链接:点击打开链接
思路:每次读取一个数据,判断是否能接到之前的拦截系统上,若是不能,就新开一个拦截系统,如果能的话,要接到那个能满足的且当前高度最小的拦截系统上(贪心思想),因为要尽可能的使当前的拦截系统的高度保住,以后才能拦截更多的导弹。
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
using namespace std;
int dp[100000];
int main(){
int n,x;
int flag,temp,index,top;
while(scanf("%d",&n)!=EOF){
top=-1;
for(int i=1;i<=n;i++){
scanf("%d",&x);
flag=0;temp=0x3f3f3f3f;
for(int j=0;j<=top;j++){
if(x<=dp[j]){
flag=1;
if(dp[j]<temp){
temp=dp[j];
index=j;
}
}
}
if(!flag) dp[++top]=x;
else dp[index]=x;
}
printf("%d\n",top+1);
}
return 0;
}
思路:每次读取一个数据,判断是否能接到之前的拦截系统上,若是不能,就新开一个拦截系统,如果能的话,要接到那个能满足的且当前高度最小的拦截系统上(贪心思想),因为要尽可能的使当前的拦截系统的高度保住,以后才能拦截更多的导弹。
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
using namespace std;
int dp[100000];
int main(){
int n,x;
int flag,temp,index,top;
while(scanf("%d",&n)!=EOF){
top=-1;
for(int i=1;i<=n;i++){
scanf("%d",&x);
flag=0;temp=0x3f3f3f3f;
for(int j=0;j<=top;j++){
if(x<=dp[j]){
flag=1;
if(dp[j]<temp){
temp=dp[j];
index=j;
}
}
}
if(!flag) dp[++top]=x;
else dp[index]=x;
}
printf("%d\n",top+1);
}
return 0;
}
相关文章推荐
- HDU 1257 最少拦截系统(基础DP,贪心)
- HDU 1257最少拦截系统(简单贪心,每次遇到这个题都会纠结)
- hdu 1257 最少拦截系统(贪心)(简单)
- 【HDU】-1257-最少拦截系统-(贪心||DP)
- HDU 1257 最少拦截系统 (DP || 贪心)
- hdu 1257 最少拦截系统【贪心 || DP——LIS】
- 最少拦截系统 - HDU 1257 贪心 或 dp
- hdu 1257 最少拦截系统【贪心 || DP——LIS】
- HDU:1257 最少拦截系统(贪心+动态规划DP||LIS?)
- POJ 1065 Wooden Sticks / hdu 1257 最少拦截系统 DP 贪心
- HDU 1257 最少拦截系统【贪心】【DP】
- HDU 1257 最少拦截系统(贪心 || dp)
- HDU-1257 最少拦截系统 贪心/DP 最长上升子序列的长度==最长不上升子序列的个数?
- hdu 1257 最少拦截系统(简单贪心)
- hdu 1257 最少拦截系统(贪心,dp)
- hdu 1257 最少拦截系统(dp)
- hdu 1257 最少拦截系统 (DP)
- hdu 1257 最少拦截系统(贪心)
- 最少拦截系统(杭电1257)(DP)+(贪心)
- hdu 1257 最少拦截系统 (DP)