JZ High School OJ 1382. Interval

Description

  Alice received some very special birthday gift: interval. Even if it is boring, Alice was able to come up with a lot of games on intervals, one of which is, Alice longest sequence selected from different sections, where each section must meet the gift, the other in the sequence must contain at each interval an interval.
  Program calculated maximum length sequence.
 

Input

  The first line of the input file contains an integer N (1 <= N <= 100000), indicates the number of sections.
  Next N lines of two integers A and B depict a section (1 <= A <= B <= 1000000).

Output

  The maximum length sequence output condition satisfied.
 

Sample Input

Input 1: 
3 
3. 4 
2. 5 
1. 6 

Input 2: 
. 5 
10 30 
20 is 40 
30 50 
10 60 
30 40 

Input 3: 
. 6 
1. 4 
1. 5 
1. 6 
1. 7 
2. 5 
3. 5

Sample Output

Output 1: 
3 

Output 2: 
3 

Output 3: 
5
 

Data Constraint

 
 

Hint

[Explain] Sample
  Example 3 can be found in sequence interval length is 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 }

Guess you like

Origin www.cnblogs.com/anbujingying/p/11308070.html
Recommended