poj1065 Wooden Sticks

题目:

      http://poj.org/problem?id=1065

 或 http://acm.hdu.edu.cn/showproblem.php?pid=1051

 

贪心+最长递增子序列+最少不覆盖

PS: 相当于简单的导弹拦截问题

 

#include <stdio.h> #include <string.h> #include <algorithm> using namespace std; struct po{ int l,w; //l长度,w重量 }f[5555]; bool hash[5555]; //按长度排序 bool cmp(const po &a,const po &b){ if(a.l!=b.l) return a.l<b.l; return a.w<b.w; } int main() { int t,n; int i,j; scanf("%d",&t); while(t--) { scanf("%d",&n); for(i=1;i<=n;i++) scanf("%d%d",&f[i].l,&f[i].w); sort(f+1,f+1+n,cmp); memset(hash,0,sizeof(hash)); int cnt=0; // 记录花费时间 int num=0; // 记录已经标记的个数 for(i=1;i<=n;i++){ if(hash[i]==0){ hash[i]=1; cnt++; num++; //44~49 相当于求最大非降序列 int s=f[i].w; for(j=i+1;j<=n;j++){ if(hash[j]==0&&s<=f[j].w){ num++; hash[j]=1; s=f[j].w; } } } if(num==n) break; } printf("%d/n",cnt); } }  

你可能感兴趣的:(poj1065 Wooden Sticks)