POJ3636 Nested Dolls ACM解题报告(暴力贪心(也可用二分查找降低复杂度))
来源:互联网 发布:男士冬天睡衣 知乎 编辑:程序博客网 时间:2024/05/16 06:10
这题是给出m个娃娃,然后如果一个的w和h都大于另一个即可将其套进去,求最少能套成多少组。
这题就是暴力贪心,先升序排w,然后降序排h,遍历m个娃娃,开个team数组存组数,如果没有符合可以套的,就存进去,如果找到第一个可以套的,那么就更新那个组的w和h的值。
这个贪心可以给出简单的证明,如果j排序在k的前面,i比j和k都大,如果i套在k上,也许后面会有一个娃娃h,它不能套在j上也不能套在i上,但是可以套在k上,如果i套在k上组数就要增加,不是最优解,如果h也不能套在k上,那么i套在j上也不会比套在k上更坏。所以应该是按照顺序遍历套上去即可。
下面给出暴力贪心的代码,547MS;
#include<iostream>#include<cstdio>#include<cctype>#include<cstdlib>#include<cmath>#include<algorithm>#include<cstring>#include<string>#include<vector>#include<queue>#include<map>#include<set>#include<sstream>#include<stack>using namespace std;#define MAX 105typedef long long LL;const double pi=3.141592653589793;const int INF=1e9;const double inf=1e20;struct doll{ int w,h;}p[20005],team[20005];bool cmp(struct doll a,struct doll b){ if(a.w!=b.w) return a.w<b.w; else return a.h>b.h;}int main(){ int t; scanf("%d",&t); while(t--) { int m; scanf("%d",&m); for(int i=0;i<m;i++) { scanf("%d%d",&p[i].w,&p[i].h); } sort(p,p+m,cmp); int count=0; for(int i=0;i<m;i++) { int flag=0; for(int j=0;j<count;j++) { if(p[i].w>team[j].w&&p[i].h>team[j].h) { team[j]=p[i]; flag=1; break; } } if(!flag) team[count++]=p[i]; } printf("%d\n",count); } return 0;}下面送上我自己写的二分代码,可能比较难看,157MS
int count=0;for(int i=0; i<m; i++){ int low=0,high=count-1; while(low<=high) { int mid=(low+high)/2; if(team[mid].w<p[i].w&&team[mid].h<p[i].h) high=mid-1;//mid满足条件,所以搜索它的左面,如果左面都不满足的话,最后low会等于这个mid,注意这个等号, 我就这个等号WA了一次,可以仔细想想为什么 else low=mid+1;//mid不满足,搜索他的右边。 } if(high==count-1) team[count++]=p[i];//这边写挫了,但是不造怎么写的漂亮一点 else team[low]=p[i];}
0 0
- POJ3636 Nested Dolls ACM解题报告(暴力贪心(也可用二分查找降低复杂度))
- 【贪心】poj3636 nested dolls
- 【POJ3636】Nested Dolls Dilworth定理(偏序集定理二)
- poj3636 Nested Dolls
- hdu1677 Nested Dolls(贪心)
- POJ1065 Wooden Sticks ACM解题报告(暴力贪心)
- hdu1677--Nested Dolls(贪心+LIS)
- HDOJ 1677 Nested Dolls(贪心+LIS)
- HDU 1677 Nested Dolls (贪心)
- hdu1677—Nested Dolls(贪心)
- HDU 1677 Nested Dolls(排序+二分)
- UVA 11368 & POJ 3636 & HDU 1677 Nested Dolls(贪心 + 二分LIS)
- Nested Dolls (单调递增子序列 + 二分)
- POJ 3636 俄罗斯Nested Dolls 贪心二分 动态规划
- Nested Dolls(hdu1677(LIS))
- poj 3636 Nested Dolls 动态更新表的二分查找
- POJ2726 Holiday Hotel ACM解题报告(贪心)
- ***POJ1029 False coin ACM解题报告(贪心难题)
- YT14-HDU-叠加的卡片
- ps查看手机进程相关信息
- iOS开发的一些奇巧淫技 .
- Python join() 函数 连接字符串
- 编写shellcode测试工具
- POJ3636 Nested Dolls ACM解题报告(暴力贪心(也可用二分查找降低复杂度))
- 桥连接模式
- python
- SQLite
- url
- 2014年被业称为“机器人元年”
- YT14-HDU-取余
- MyEclipse创建Maven工程
- Sicily 2010 H Number