D - Two TVs CodeForces - 845C

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

题意:给出N个节目它们开始和结束的时间。问能否用两台电视机看完所有的节目。Poly可以同时看两台电视机
知识点:模拟和排序
思路:用结构体数组存那些节目。排序从小到大。开两个数组模拟两台电视机,只要符合条件就可以加入,如果不符合直接break失败。

#include<iostream>
#include<algorithm>
using namespace std;
struct Tv{
    long long int l,r;
}tv[200005],exam1[200005],exam2[200005];
int N;
bool cmd(Tv a,Tv b){
    if(a.l==b.l)
        return a.r<b.r;
    else
        return a.l<b.l;
}
int main(){
    cin>>N;
    int tl,tr;
    for(int i=0;i<N;i++){
        cin>>tl>>tr;
        tv[i].l=tl;
        tv[i].r=tr;
    }
    if(N==1||N==2){
        cout<<"YES"<<endl;
    }else{
    sort(tv,tv+N,cmd);
    exam1[0]=tv[0];
    exam2[0]=tv[1];
    int t1=0,t2=0;
    bool flag=true;
    for(int i=2;i<N;i++){
        if(tv[i].l>exam1[t1].r){
            exam1[++t1]=tv[i];
        }else if(tv[i].l>exam2[t2].r){
            exam2[++t2]=tv[i];
        }else {
            flag=false;
            break;
        }
    }
    if(flag==true){
        cout<<"YES"<<endl;
    }else{
    cout<<"NO"<<endl;
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值