JZ高中OJ 1382. 区间

Description

  Alice收到一些很特别的生日礼物:区间。即使很无聊,Alice还是能想出关于区间的很多游戏,其中一个是,Alice从中选出最长的不同区间的序列,其中满足每个区间必须在礼物中,另序列中每个区间必须包含下一个区间。
  编程计算最长序列的长度。
 

Input

  输入文件第一行包含一个整数N(1<=N<=100000),表示区间的个数。
  接下来N行,每行两个整数A和B描述一个区间(1<=A<=B<=1000000)。

Output

  输出满足条件的序列的最大长度。
 

Sample Input

输入1:
3
3 4
2 5
1 6

输入2:
5
10 30
20 40
30 50
10 60
30 40

输入3:
6
1 4
1 5
1 6
1 7
2 5
3 5

Sample Output

输出1:
3

输出2:
3

输出3:
5
 

Data Constraint

 
 

Hint

【样例解释】
  例3中可以找到长度为5的区间序列是:[1,7]、[1,6]、[1,5]、[2,5]、[3,5]
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int MAXN=1e5+5;
 4 const int INF=2e9;
 5 struct node {
 6     int l,r;
 7 }a[MAXN];
 8 int n;
 9 int s[MAXN],top;
10 bool cmp (node a,node b){
11     if (a.l==b.l) return a.r<b.r;
12     return a.l>b.l;
13 }
14 int main(){
15     cin>>n;
16     for (int i=1;i<=n;i++){
17         scanf ("%d%d",&a[i].l,&a[i].r);
18     }
19     sort (a+1,a+n+1,cmp);
20     for (int i=1;i<=n;i++){
21         int t=a[i].r;
22         if (t>=s[top]) s[++top]=t;
23         else {
24             int l=1,r=top,mid;
25             while (l<=r){
26                 mid=(l+r)/2;
27                 if (s[mid]<=t) l=mid+1;
28                 else r=mid-1;
29             }
30             s[l]=t;
31         }
32     }
33     cout<<top<<endl;
34     return 0;
35 }

猜你喜欢

转载自www.cnblogs.com/anbujingying/p/11308070.html
今日推荐