题目大意就是,给出一个板子的长宽,如果一个板子可以放到另外一个上面(长宽均小于此块),那么可以认为此板子加工时候不话费时间
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).
根据题意,把板子按照l的值进行从小到达排序,就是求l的做大递增序列的最小个数,也就等于求出l长度的最大递减序列,则得到的就是所得结果,因为最大递减序列里的每一个数都是一个递增序列的第一个数
另外在#include <algorithm>头文件里又得知一个函数*max_element(b, b + n),返回数组b里面最大的元素
由此用常规的O(n^2)的方法很容易写出,这里再给出一个求最大XX序列的一个O(nlgn)的算法,用的是dp+二分,需要慢慢理解
O(nlogn)算法
代码中,b[k]=m数组存放的是长度为k的最长上升子序列中最小的数字值m,请看下篇poj2631有此方法的详解

1 for(int i = 1; i != n; ++i)
2 {
3 left = 1;
4 right = len;
5 while(left <= right)
6 {
7 mid = (left + right) >> 1;
8 if(a[i] < b[mid]) //如果求递增序列,则为b[mid]<a[i]
9 left = mid + 1;
10 else
11 right = mid - 1;
12 }
13 b[left] = st[i].w;
14 if(left > len)
15 ++len;
16 }
17 cout << len << endl;
O(n^2)的算法

1 int b[SIZE];
2 b[0] = 1;
3 for(int i = 1; i != n; ++i)
4 {
5 int max = 1;
6 for(int j = 0; j < i; ++j)
7 if(st[i].w < st[j].w && b[j] + 1 > max)
8 max = b[j] + 1;
9 b[i] = max;
10 }
11 cout << *max_element(b, b + n) << endl;
最后贴上转过来的别人ac的代码

1 #define SIZE 20001
2
3 #include<iostream>
4 #include<algorithm>
5 using namespace std;
6
7 struct Sticks
8 {
9 int l;
10 int w;
11 };
12
13 int cmp(struct Sticks x, struct Sticks y)
14 {
15 if(x.l == y.l)
16 return x.w < y.w;
17 else
18 return x.l < y.l;
19 }
20
21 int main()
22 {
23 int T;
24 cin >> T;
25 while(T--)
26 {
27 int n, x, y;
28 Sticks st[SIZE];
29 cin >> n;
30 for(int i = 0; i != n; ++i)
31 {
32 cin >> x >> y;
33 st[i].l = x;
34 st[i].w = y;
35 }
36
37 sort(st, st + n, cmp);
38 int b[SIZE];
39 b[0] = st[0].w;
40 int len=1;
41 for(int i = 1; i != n; ++i)
42 {
43 int r=len,l=1;
44 int mid=(r+l)>>1;
45 while(l<=r)
46 {
47 if(st[i].w<b[mid])
48 l=mid+1;
49 else
50 r=mid-1;
51 }
52 b[l]=st[i].w;
53 len=len<l?l:len;
54 }
55 cout<<len<<endl;
56 //cout << *max_element(b, b + n) << endl;
57 }
58 return 0;
59 }