题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1051
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

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 be processed by a woodworking machine in 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 first wooden stick is 1 minute. 
(b) Right after processing a stick of length l and weight w , the machine will need no setup time for a stick of length l' and weight w' if l<=l' and w<=w'. Otherwise, it will need 1 minute for setup. 

You are to find the minimum setup time to process a given pile of n wooden sticks. For example, if 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 be 2 minutes since there is a sequence of pairs (1,4), (3,5), (4,9), (2,1), (5,2).

Input

The input consists of T test cases. The number of test cases (T) is given in the first line of the input file. Each test case consists of two lines: The first line has an integer n , 1<=n<=5000, that represents the number of wooden sticks in the test case, and the second line contains n 2 positive integers l1, w1, l2, w2, ..., ln, wn, each of magnitude 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, one per line.

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 3 1

Sample Output

2
1
3

Problem solving report:

Description: 处理木块前需要对机器进行设置,如果后面放入的木块l和w都不大于前面的木块就不用重新设置,否则重新设置,每设置一次需要花费一分钟。实际上就是给出包含两个关键字的一个序列,求最少能够组成多少个两个关键字都为非递增的序列。
Problem solving: 这道题可以用贪心直接写出来,我们可以先对木块进行非递增排序(l不等时按l排,l相等时按w排)。我们可以先找到一个起点,然后从这个起点开始,把后面非递增的元素都标记出来,统计一下有多少个这样的起点。有多少个起点,就有多少个非递增的序列,就设置多少次机器。

#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
struct edge {
    int left, right;
}e[5010];
bool cmp(edge a, edge b)
{
    if (a.left != b.left)
        return a.left > b.left;
    return a.right > b.right;
}
int main()
{
    int t, n, ans, right, vis[5010];
    cin >> t;
    while (t--)
    {
        ans = 0;
        cin >> n;
        memset(vis, 0, sizeof(vis));
        for (int i = 0; i < n; i++)
            cin >> e[i].left >> e[i].right;
        sort(e, e + n, cmp);
        for (int i = 0; i < n; i++)
        {
            if (vis[i])//已经标记过就不需要再继续访问
                continue;
            ans++;//找到一个起点,设置一次机器
            vis[i] = 1;
            right = e[i].right;//以i为起点
            for (int j = i + 1; j < n; j++)
            {
                if (!vis[j] && e[j].right <= right)//满足非递增
                {
                    right = e[j].right;//更新right,以便找到后面的元素
                    vis[j] = 1;//把找到的元素都标记
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}