Codeforces-845C:Two TVs(思维)

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
思路:把节目时间进行标记,节目开始时间标记为1,节目结束时间标记为-1,然后对其按从小到大排序,然后循环一遍,每遇到一个节目开始时间ans++,每遇到一个节目结束时间,ans--。如果ans始终满足ans<=2,就说明用2个TV可以看完节目。

#include<bits/stdc++.h>
using namespace std;
struct lenka
{
    int t,in;
}a[1000000];
int cmp(const lenka& x,const lenka& y)
{
    if(x.t==y.t)return x.in>y.in;
    return x.t<y.t;
}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d%d",&a[2*i].t,&a[2*i+1].t);
        a[2*i].in=1;
        a[2*i+1].in=-1;
    }
    sort(a,a+2*n,cmp);
    int ans=0,in=0;
    for(int i=0;i<2*n;i++)
    {
        in+=a[i].in;
        ans=max(ans,in);
    }
    puts(ans>2?"NO":"YES");
    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值