【最长下降子序列】【动态规划】【二分】XMU 1041 Sequence

题目链接:

  http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1041

题目大意

  一个二维平面,上面n(n<=1 000 000)个点。问至少选多少个点才能完全包含所有的点。

  包含是指xy坐标均不大于。

题目思路:

  【最长下降子序列】【动态规划】【二分】

  这题n有107,所以用二分做最长下降子序列。

  首先将所有点按x坐标或者y坐标排序,保证一维的单调性。

  之后在剩余一维的数中求最长严格下降子序列即可。

  (如果下一个点是上升的那么可以放弃当前的点转而取下一个点,可以使结果更优,所以最终取点是下降的)

  用一个数组存下达到当前长度的子序列最后一个数字。查找的时候用二分。

 1 //
 2 //by coolxxx
 3 //
 4 #include<iostream>
 5 #include<algorithm>
 6 #include<string>
 7 #include<iomanip>
 8 #include<memory.h>
 9 #include<time.h>
10 #include<stdio.h>
11 #include<stdlib.h>
12 #include<string.h>
13 //#include<stdbool.h>
14 #include<math.h>
15 #define min(a,b) ((a)<(b)?(a):(b))
16 #define max(a,b) ((a)>(b)?(a):(b))
17 #define abs(a) ((a)>0?(a):(-(a)))
18 #define lowbit(a) (a&(-a))
19 #define sqr(a) ((a)*(a))
20 #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
21 #define eps (1e-8)
22 #define J 10000000
23 #define MAX 0x7f7f7f7f
24 #define PI 3.1415926535897
25 #define N 1000004
26 using namespace std;
27 typedef long long LL;
28 int cas,cass;
29 int n,m,lll,ans;
30 struct xxx
31 {
32     int x,y;
33 }p[N];
34 int a[N];
35 bool cmp(xxx aa,xxx bb)
36 {
37     if(aa.x!=bb.x)return aa.x<bb.x;
38     return aa.y<bb.y;
39 }
40 int main()
41 {
42     #ifndef ONLINE_JUDGE
43 //    freopen("1.txt","r",stdin);
44 //    freopen("2.txt","w",stdout);
45     #endif
46     int i,j,l,r,mid,x;
47 //    for(scanf("%d",&cas);cas;cas--)
48 //    for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
49 //    while(~scanf("%s",s))
50     while(~scanf("%d",&n))
51     {
52         ans=0;
53         for(i=1;i<=n;i++)
54             scanf("%d%d",&p[i].x,&p[i].y);
55         sort(p+1,p+1+n,cmp);
56         for(i=1;i<=n;i++)
57         {
58             l=0,r=ans;x=p[i].y;
59             while(l<r)
60             {
61                 mid=(l+r+1)>>1;
62                 if(x<a[mid])l=mid;
63                 else r=mid-1;
64             }
65             a[l+1]=x;
66             ans=max(l+1,ans);
67         }
68         printf("%d
",ans);
69     }
70     return 0;
71 }
72 /*
73 //
74 
75 //
76 */
千万不要点
原文地址:https://www.cnblogs.com/Coolxxx/p/5692708.html