pku1065 Wooden Sticks

http://poj.org/problem?id=1065

DP,最长“上升”子序列

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 struct Sticks
 5 {
 6     int x, y;
 7 }s[5432];
 8 
 9 int n, dp[5432];
10 
11 int cmp1(struct Sticks a, struct Sticks b)
12 {
13     return a.x < b.x || a.y < b.y;
14 }
15 
16 int LIS()
17 {
18     int i, j, max1, max2 = 1;
19     dp[0] = 1;
20     for(i=1; i<n; i++)
21     {
22         max1 = 0;
23         for(j=0; j<i; j++)
24         {
25             if(cmp1(s[i], s[j]) && dp[j]>max1)
26             {
27                 max1 = dp[j];
28             }
29         }
30         dp[i] = max1+1;
31         if(dp[i] > max2)
32         {
33             max2 = dp[i];
34         }
35     }
36     return max2;
37 }
38 
39 int cmp(const void *a, const void *b)
40 {
41     struct Sticks p, q;
42     p = *(struct Sticks *)a;
43     q = *(struct Sticks *)b;
44     return (q.x < p.x || (p.x==q.x && q.y<p.y))? 1: -1;
45 }
46 
47 int main()
48 {
49     int t, i;
50     scanf("%d", &t);
51     while(t-- && scanf("%d", &n))
52     {
53         for(i=0; i<n; i++)
54         {
55             scanf("%d%d", &s[i].x, &s[i].y);
56         }
57         qsort(s, n, sizeof(s[0]), cmp);
58         printf("%d\n", LIS());
59     }
60     return 0;
61 }

 

posted @ 2013-01-20 01:09  Yuan1991  阅读(136)  评论(0)    收藏  举报