POJ3636Nested Dolls

来源:互联网 发布:学美发的软件 编辑:程序博客网 时间:2024/05/21 16:22
Nested Dolls
Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u


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 w1h1,w2h2, ... ,wmhm, 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;}




0 0
原创粉丝点击