Problem Description

There is a pile of n wooden sticks. The length and weight of each stick are known in advance. The sticks are to is processed by a woodworking machine into one by one fashion. It needs some time, called setup time, for the machine to prepare processing a stick. The setup times are associated with cleaning operations and changing tools and shapes in the machine. The setup times of the woodworking machine are given as follows:

(a) The setup time for the "a" wooden stick is 1 minute. (b) Right after processing a stick of length L and weight W, the machine'll need no setup time for a stick of length l ' and weight W ' if l<=l ' and W<=w '. Otherwise, it'll need 1 minute for setup.

You are are to find the minimum setup time to process a given pile of n wooden sticks. For example, if to you have five sticks whose pairs of length and weight are (4,9), (5,2), (2,1), (3,5), and (1,4), then the Minimum setup time should was 2 minutes since there is sequence of pairs (1,4), (3,5), (4,9), (2,1).

Input

The input consists of T test cases. The number of test cases (T) is given in the the the input file. Each test case consists of two lines:the A-has an integer n, 1<=n<=5000, that represents the number of Wo Oden sticks in the "test Case" and the second line contains n 2 positive integers L1, W1, L2, W2, ..., LN, WN, each of Magn Itude at most 10000, where Li and wi are the length and weight of the i th wooden stick, respectively. The 2n integers are delimited by one or more spaces.

Output

The output should contain the minimum setup time in minutes.

Sample Input

3 5 4 9 5 2 2 1 3 5 1 4 3 2 2 1 1 2 2 3 1 3 2 2 the 3 1

Sample Output

2 1 3

__Copy Code__ code as follows:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define N 5000;

struct node

{

int l;

int W;

int flag;

}STICKS[5000];

int cmp (const void *p,const void *q)

{

struct node *a = (struct node *) p;

struct node *b = (struct node *) q;

if (A->l > B->l) return 1;

else if (A->l < b->l) return-1;

else return a->w > b->w? 1:-1;

}

int main ()

{

int T,N,CNT,CL,CW;

int i,j;

scanf ("%d", &t);

while (t--)

{

memset (sticks,0,sizeof (sticks));

scanf ("%d", &n);

for (i = 0; i < n; i++)

scanf ("%d%d", &STICKS[I].L,&STICKS[I].W);

Qsort (Sticks,n,sizeof (sticks[0]), CMP);

Sticks[0].flag = 1;

CL = STICKS[0].L;

CW = STICKS[0].W;

CNT = 1;

for (j = 1; j < N; j + +)

{

for (i = j; i < n; i++)

{

if (!STICKS[I].FLAG&&STICKS[I].L>=CL&&STICKS[I].W>=CW)

{

CL = STICKS[I].L;

CW = STICKS[I].W;

Sticks[i].flag = 1;

}

}

i = 1;

while (Sticks[i].flag)

i++;

j = i;

if (j = = n) break;

CL = STICKS[J].L;

CW = STICKS[J].W;

Sticks[j].flag = 1;

cnt++;

}

printf ("%d\n", CNT);

}

return 0;

}

Meaning

We have to deal with some sticks, the first time is 1 minutes, the other, in the length of L, the length of the root behind the stick is L ', weight w ', as long as L <=l ' and W <= W ', do not need time, otherwise need 1 minutes, how to arrange to handle the order of the stick, so that the time to spend the least.

Ideas:

Greedy algorithm. First, these sticks are arranged in the order of length and weight from small to large. CL and CW are the length and weight of the first root, in order to compare the following is not larger than the current CL,CW, is the case of the logo flag set to 1, and with the new CL,CW. After the comparison, and then the previous scan, to find the first sign bit 0, as is the second batch of the smallest one, counter plus one. Compare its length and weight as the current CL,CW, and then cycle back. Until all of them have been dealt with.