POJ3636Nested Dolls
来源:互联网 发布:学美发的软件 编辑:程序博客网 时间:2024/05/21 16:22
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
4320 30 40 50 30 40420 30 10 10 30 20 40 50310 30 20 20 30 10410 10 20 30 40 50 39 51
Sample Output
1232
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;}
- POJ3636Nested Dolls
- Nested Dolls
- poj_3636Nested Dolls
- Nested Dolls
- HDU4160--Dolls
- Russian Dolls
- Russian Dolls
- Nested Dolls
- hd 1677 Nested Dolls
- zoj 2882 Nested Dolls
- hdoj 4160 Dolls
- poj3636 Nested Dolls
- 【贪心】poj3636 nested dolls
- hdu 4160 Dolls
- POJ 3636 Nested Dolls
- HDU1677 Nested Dolls
- hdu 4160 Dolls
- hdu 4160 Dolls
- 第一次记录问题, 集成环信easeui库的地图问题
- remote-desktop-clients的项目RDP客户端编译(一)环境准备
- POJ1966 Cable TV Network
- Java中unicode增补字符(辅助平面)相关用法简介
- wireshark通过TCP抓包进行确认
- POJ3636Nested Dolls
- 排序算法(归并排序, 快速排序, 堆排序)
- 大型网站的负载均衡器、db proxy和db
- photoshop制作黑色风格的网页按钮教程
- github error:src refspec master does not match any的解决办法
- go排序示例
- Camera Roll API + Upload From Camera Roll Module
- NGINX引入线程池 性能提升9倍
- js复制内容到剪贴板代码,js复制代码