算法1:类似于筛选法求素数
算法2:类似于求最长上升子序列O(nlogn)的算法 (
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 const int N=5010;
5 struct stick
6 {
7 int l,w;
8 }a[N];
9 int f[N];
10 int cmp(const void *a,const void *b)
11 {
12 stick *a1=(stick*)a;
13 stick *b1=(stick*)b;
14 if (a1->l != b1->l) return a1->l - b1->l;
15 else return a1->w - b1->w;
16 }
17 int main()
18 {
19 int T;
20 scanf("%d",&T);
21 while (T--)
22 {
23 int n,i;
24 scanf("%d",&n);
25 a[0].l=a[0].w=0;
26 for (i=1;i<=n;i++) scanf("%d%d",&a[i].l,&a[i].w);
27 qsort(a+1,n,sizeof(a[0]),cmp);
28 memset(f,0,sizeof(f));
29 for (i=1;i<=n;i++)
30 {
31 int j=1;
32 while (f[j]>a[i].w) j++;
33 f[j]=a[i].w;
34 }
35 i=1; while (f[i]) i++;
36 printf("%d\n",i-1);
37 }
38 }
代码)