Nested Dolls (单调递增子序列 + 二分)
2016-05-06 16:20
113 查看
Description
Dilworth is the world’s most prominent collector of Russian nested dolls: he literally has thousands of them! You know, the wooden hollow dolls of different sizes of which the smallest doll is contained in the second smallest, and
this doll is in turn contained in the next one and so forth. One day he wonders if there is another way of nesting them so he will end up with fewer nested dolls? After all, that would make his collection even more magnificent! He unpacks each nested doll
and measures the width and height of each contained doll. A doll with width w1 and height h1 will fit in another doll of width w2 and height h2 if and only if w1 < w2 and h1 < h2. Can you help him calculate the smallest number of nested dolls possible to assemble
from his massive list of measurements?
Input
On the first line of input is a single positive integer 1 <= t <= 20 specifying the number of test cases to follow. Each test case begins with a positive integer 1 <= m <= 20000 on a line of itself telling the number of dolls in the
test case. Next follow 2m positive integers w1, h1,w2, h2, . . . ,wm, hm, where wi is the width and hi is the height of doll number i. 1 <= wi, hi <= 10000 for all i.
Output
For each test case there should be one line of output containing the minimum number of nested dolls possible.
Sample Input
Sample Output
题目大意:
就是有一堆的长和宽知道的玩偶,小的玩偶可以嵌套在大的里面,只有当长和宽都比它要嵌套在里面的小的时候才可以(长宽不能互换)问多有嵌套完毕后,还有多有个玩偶(最少的)。
解题思路:
要求出最多的能嵌套的个数,如果用贪心思想,势必会TLE,必然要寻求别的方法,因为长或宽相同的不能嵌套,那么如果对长按照从大到小的顺序排列,对于长相等的按照从小到大的顺序排列,那么对宽求单调递增子序列即可。不管什么情况,都可以求得最优解。
一个重要的是要用二分求解最长公共子序列获取最优解,主要是为了优化时间。
代码如下:
Dilworth is the world’s most prominent collector of Russian nested dolls: he literally has thousands of them! You know, the wooden hollow dolls of different sizes of which the smallest doll is contained in the second smallest, and
this doll is in turn contained in the next one and so forth. One day he wonders if there is another way of nesting them so he will end up with fewer nested dolls? After all, that would make his collection even more magnificent! He unpacks each nested doll
and measures the width and height of each contained doll. A doll with width w1 and height h1 will fit in another doll of width w2 and height h2 if and only if w1 < w2 and h1 < h2. Can you help him calculate the smallest number of nested dolls possible to assemble
from his massive list of measurements?
Input
On the first line of input is a single positive integer 1 <= t <= 20 specifying the number of test cases to follow. Each test case begins with a positive integer 1 <= m <= 20000 on a line of itself telling the number of dolls in the
test case. Next follow 2m positive integers w1, h1,w2, h2, . . . ,wm, hm, where wi is the width and hi is the height of doll number i. 1 <= wi, hi <= 10000 for all i.
Output
For each test case there should be one line of output containing the minimum number of nested dolls possible.
Sample Input
4 3 20 30 40 50 30 40 4 20 30 10 10 30 20 40 50 3 10 30 20 20 30 10 4 10 10 20 30 40 50 39 51
Sample Output
1 2 3 2
题目大意:
就是有一堆的长和宽知道的玩偶,小的玩偶可以嵌套在大的里面,只有当长和宽都比它要嵌套在里面的小的时候才可以(长宽不能互换)问多有嵌套完毕后,还有多有个玩偶(最少的)。
解题思路:
要求出最多的能嵌套的个数,如果用贪心思想,势必会TLE,必然要寻求别的方法,因为长或宽相同的不能嵌套,那么如果对长按照从大到小的顺序排列,对于长相等的按照从小到大的顺序排列,那么对宽求单调递增子序列即可。不管什么情况,都可以求得最优解。
一个重要的是要用二分求解最长公共子序列获取最优解,主要是为了优化时间。
代码如下:
#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include <queue> #include <map> #include <cmath> #include <string> #define INF 0x3f3f3f3f using namespace std; struct Data { int w, h; }s[20010]; bool cmp(Data A, Data B) { if(A.w != B.w) return A.w > B.w; else return A.h < B.h; } int main() { int t, i, j, sum, n; scanf("%d",&t); while(t--) { scanf("%d",&n); for(i = 1; i <= n; i++) { scanf("%d%d",&s[i].w,&s[i].h); } sort(s + 1, s + n + 1, cmp); // 对长从大到小排序,宽从小到大排序 int b[20010],dp[20010]; memset(b, 0, sizeof(b)); memset(dp, 0, sizeof(dp)); sum = -1; b[0] = -1; dp[0] = 0; int top; for(i = 1, top = 0; i <= n; i++)//利用二分法对宽求最长公共子序列 { if(s[i].h >= b[top]) { b[++top] = s[i].h;//b数组记录最长公共子序列 dp[i] = top;//记录当前点的最长公共子序列的元素个数 } else { int l = 1, r = top; while(l <= r)//二分 { int mid = (l + r) / 2; if(s[i].h >= b[mid]) { l = mid + 1; } else r = mid - 1; } b[l] = s[i].h; dp[i] = l; } if(dp[i] > sum) sum = dp[i]; } printf("%d\n",sum); } return 0; }
相关文章推荐
- HDU - 4135 Co-prime
- Android导包导致java.lang.NoClassDefFoundError
- MFC为控件添加消息响应函数(事件)
- kerberos认证原理---讲的非常细致,易懂
- OkHttp3.1以上信任所有证书
- 用户体验的分类----产品经理深入浅出课程
- php 7.0 编译安装
- 浅谈四轴飞行器的力原理
- python中sort和sorted函数小结
- Hibernate Validation各注解的用法Demo
- C#中String和string的区别
- nodejs 数据库操作,消息的发送和接收,模拟同步
- perl的foreach循环的坑
- Android屏幕适配全攻略(最权威的官方适配指导)
- 从源码角度看一个apk的启动过程和一个activity的启动过程
- Android ViewPager滑动事件讲解
- 使用SharedApplication进行传值
- c# windows服务如何获取自己的安装路径
- 如何设计和维护好一个大项目
- 安卓面试题