天天看點

cfC. Two TVs

C. Two TVs

time limit per test2 seconds

memory limit per test256 megabytes

inputstandard input

outputstandard 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).

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include<cstdio>

#include<cstdlib>
#include<cmath>
#include <vector>
using namespace std;
vector<pair<int,int> >e;
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        int l,r;
        scanf("%d%d",&l,&r);
        e.push_back(make_pair(l,1));
        e.push_back(make_pair(r+1,-1));
    }
    int cnt=0;
    sort(e.begin(),e.end());
    for(int i=0;i<e.size();i++){
        cnt+=e[i].second;
        if(cnt>2) return 0*printf("NO");
    }
    return 0*printf("YES");
}