当前位置:   article > 正文

Codeforces-845C:Two TVs(思维)_two tvs codeforces - 845c

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

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. struct lenka
  4. {
  5. int t,in;
  6. }a[1000000];
  7. int cmp(const lenka& x,const lenka& y)
  8. {
  9. if(x.t==y.t)return x.in>y.in;
  10. return x.t<y.t;
  11. }
  12. int main()
  13. {
  14. int n;
  15. scanf("%d",&n);
  16. for(int i=0;i<n;i++)
  17. {
  18. scanf("%d%d",&a[2*i].t,&a[2*i+1].t);
  19. a[2*i].in=1;
  20. a[2*i+1].in=-1;
  21. }
  22. sort(a,a+2*n,cmp);
  23. int ans=0,in=0;
  24. for(int i=0;i<2*n;i++)
  25. {
  26. in+=a[i].in;
  27. ans=max(ans,in);
  28. }
  29. puts(ans>2?"NO":"YES");
  30. return 0;
  31. }





声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/71585
推荐阅读
相关标签
  

闽ICP备14008679号