POJ3636Nested Dolls
2016-05-05 16:35
459 查看
Nested Dolls
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 h= 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
Source
Nordic 2007
题意:
输入n个长x,宽y的矩阵,当两个矩阵满足该矩阵的长和宽小于另一个矩阵时,则两个矩阵可以嵌套,判断嵌套以后还剩下最少有多少组。
贪心题目:题目中给出的数据太水,注意这一组数据
1
5
10 20
10 30
5 20
6 30
10 30
当长相同的时候,要对宽进行从大到小排序,至于原因,代码中有说明,还有就是长或者宽相同的时候不可以嵌套
Time Limit: 1000MS | Memory Limit: 65536KB | 64bit IO Format: %I64d & %I64u |
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 h= 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
Source
Nordic 2007
题意:
输入n个长x,宽y的矩阵,当两个矩阵满足该矩阵的长和宽小于另一个矩阵时,则两个矩阵可以嵌套,判断嵌套以后还剩下最少有多少组。
贪心题目:题目中给出的数据太水,注意这一组数据
1
5
10 20
10 30
5 20
6 30
10 30
当长相同的时候,要对宽进行从大到小排序,至于原因,代码中有说明,还有就是长或者宽相同的时候不可以嵌套
<pre name="code" class="html">#include <iostream> #include <algorithm> #include <stdio.h> #include <string.h> using namespace std; struct data { int x; ///长 int y; ///宽 } a[20010],cur; bool b[20010];///标记数组 bool comp(const data &a,const data &b) { if(a.x == b.x) return a.y > b.y;//长相同的时候从大到小 return a.x < b.x;///长从小到大排序 } int main() { int T,n; scanf("%d",&T); while(T --) { scanf("%d",&n); for(int i = 0; i < n; i ++) scanf("%d%d",&a[i].x,&a[i].y); sort(a,a + n,comp); memset(b,0,sizeof(b)); int sum = 0; for(int i = 0; i < n; i ++) { if(b[i])///优化 continue; ++ sum;//统计数量 b[i] = 1; cur.x = a[i].x; cur.y = a[i].y; for(int j = i+1; j < n; j ++) { ///cur.y < a[j].y,这里能够体现为什么宽要从大到小排序,因为这两个长已经相同了,不能够嵌套 if(!b[j] && cur.y < a[j].y) { b[j] = true; cur.x = a[j].x; cur.y = a[j].y; } } } printf("%d\n",sum); } return 0; }
相关文章推荐
- Centos 安装docker报错
- SVN迁移
- Words Reciting 2016.5.2
- Scrapy中遇到的坑
- centos7下使用yum安装mysql
- Words Reciting 2016.5.3
- Android 双向Data Binding
- React Native API模块之Alert弹出框详解及使用
- 反向代理与web容器对比
- 内存溢出
- 如何制作一个漂亮网页
- Android开发-线程池
- 深刻的理解Fragment生命周期 都在做什么,fragment生命周期
- poj 1860
- 冒泡排序
- Android Context 是什么?
- iOS 同一个View识别单击和双击手势
- Fragment下listview item设置fragment,
- Linux下查看Nginx安装目录、版本号信息?
- android studio 界面xml创建问题