最大递增子序列变形——二维 O(n*logn) TOJ4701

 1 #include <cstring>
 2 #include <iostream>
 3 #include <algorithm>
 4 
 5 using namespace std;
 6 
 7 class Node
 8 {
 9 public:
10     int x;
11     int y;
12     bool operator <(const Node&n) const
13     {
14         return x<=n.x&&y<=n.y;
15     }
16 };
17 
18 bool cmp(Node a,Node b)
19 {
20     if(a.x==b.x)
21         return a.y<b.y;
22     else
23         return a.x<b.x;
24 }
25 
26 Node arr[100010];
27 Node ans[100010];
28 
29 int main()
30 {
31     int T;
32     cin>>T;
33     while(T--)
34     {
35         int n;
36         cin>>n;
37         for(int i=0;i<n;i++)
38         {
39             cin>>arr[i].x>>arr[i].y;
40         }
41         sort(arr,arr+n,cmp);
42         int siz=1;
43         ans[0].x=arr[0].x;
44         ans[0].y=arr[0].y;
45         for(int i=1;i<n;i++)
46         {
47             int s=lower_bound(ans,ans+siz,arr[i])-ans;
48             if(s==siz)
49             {
50                 if(ans[s-1].x<=arr[i].x&&ans[s-1].y<=arr[i].y)
51                 {
52                     ans[s].x=arr[i].x;
53                     ans[s].y=arr[i].y;
54                     siz++;
55                 }
56             }
57             else
58             {
59                 ans[s].x=arr[i].x;
60                 ans[s].y=arr[i].y;
61             }
62 
63         }
64         cout<<siz<<endl;
65     }
66     return 0;
67 }

 

posted @ 2015-07-22 18:51  相儒以沫  阅读(215)  评论(0编辑  收藏  举报