CF-845C

C. Two TVs
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Polycarp is a great fan of television.

He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment li and ends at moment ri.

Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV.

Polycarp wants to check out all n shows. Are two TVs enough to do so?

Input

The first line contains one integer n (1 ≤ n ≤ 2·105) — the number of shows.

Each of the next n lines contains two integers li and ri (0 ≤ li < ri ≤ 109) — starting and ending time of i-th show.

Output

If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes).

Examples
input
3
1 2
2 3
4 5
output
YES
input
4
1 2
2 3
2 3
1 2
output
NO

 

题意:

有两台电视,每台可以播放一个时间段的节目,当节目结束的下一个时刻才可以播放其他节目,问是否可以观看全部节目。

 

对电视设一个判断有无在播放的tag,模拟即可。

 

AC代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 struct node{
 5     int s,t;
 6 }a[200010];
 7 
 8 struct tv{
 9     int e;
10     int tag;
11 }t[5];
12 
13 int cmp(node x,node y){
14     if(x.s==y.s)
15     return x.t<y.t;
16     else
17     return x.s<y.s;
18 }
19 
20 int main(){
21     ios::sync_with_stdio(false);
22     int n;
23     cin>>n;
24     for(int i=0;i<n;i++){
25         cin>>a[i].s>>a[i].t;
26     }
27     sort(a,a+n,cmp);
28     t[0].e=-1;
29     t[1].e=-1;
30     t[0].tag=0;
31     t[1].tag=0;
32     int flag=0;
33     for(int i=0;i<n;i++){
34         if(a[i].s>t[0].e){
35             t[0].tag=0;
36         }
37         else if(a[i].s>t[1].e){
38             t[1].tag=0;
39         } 
40         if(!t[0].tag){
41             t[0].tag=1;
42             t[0].e=a[i].t;
43         }
44         else if(!t[1].tag){
45             t[1].tag=1;
46             t[1].e=a[i].t;
47         }
48         else{
49             flag=1;
50             break;
51         }
52     }
53     if(flag){
54         cout<<"NO"<<endl;
55     }
56     else{
57         cout<<"YES"<<endl;
58     }
59     return 0;
60 } 

 

posted @ 2017-08-24 11:00  Kiven#5197  阅读(220)  评论(0编辑  收藏  举报