Educational Codeforces Round 27 C. Two TVs(模拟)

水深无声 2022-06-10 14:39 304阅读 0赞

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 l**i and ends at moment r**i.

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 l**i and r**i (0 ≤ l**i < r**i ≤ 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

  1. 3
  2. 1 2
  3. 2 3
  4. 4 5

output

  1. YES

input

  1. 4
  2. 1 2
  3. 2 3
  4. 2 3
  5. 1 2

output

  1. NO

题解:

题意:

有两台电视,有节目的开始时间和结束时间,这题的题意还蛮有意思,你可以同时看两个节目,但是你在一个节目结束的时刻不能用这一台开下一个这个时刻开始的节目(但是可以换一台看),问你是否可以看完所有节目,直接模拟两个电视的关闭情况就行了

代码:

  1. #include<algorithm>
  2. #include<iostream>
  3. #include<cstring>
  4. #include<stdio.h>
  5. #include<math.h>
  6. #include<string>
  7. #include<stdio.h>
  8. #include<queue>
  9. #include<stack>
  10. #include<map>
  11. #include<vector>
  12. #include<deque>
  13. using namespace std;
  14. #define lson k*2
  15. #define rson k*2+1
  16. #define M (t[k].l+t[k].r)/2
  17. #define INF 1008611111
  18. #define ll long long
  19. #define eps 1e-15
  20. struct node
  21. {
  22. int s,t;
  23. int d;
  24. }a[200005];
  25. int cmp(node x,node y)//节目开始时间从小到大排,相同结束时间早的在前
  26. {
  27. if(x.s!=y.s)
  28. return x.s<y.s;
  29. return x.t<y.t;
  30. }
  31. struct tv
  32. {
  33. int e;//记录电视节目的结束时间
  34. int tag;//记录电视的开闭
  35. }t[5];
  36. int main()
  37. {
  38. int i,j,n,flag=1;
  39. scanf("%d",&n);
  40. for(i=0;i<n;i++)
  41. {
  42. scanf("%d%d",&a[i].s,&a[i].t);
  43. }
  44. sort(a,a+n,cmp);
  45. t[0].e=-1;
  46. t[1].e=-1;
  47. t[0].tag=0;
  48. t[1].tag=0;
  49. for(i=0;i<n;i++)//模拟电视的开闭
  50. {
  51. if(a[i].s>t[0].e)//根据节目的结束时间清除电视的状态
  52. {
  53. t[0].tag=0;
  54. }
  55. if(a[i].s>t[1].e)
  56. {
  57. t[1].tag=0;
  58. }
  59. if(!t[0].tag)//如果第一台可以看
  60. {
  61. t[0].tag=1;
  62. t[0].e=a[i].t;
  63. }
  64. else if(!t[1].tag)
  65. {
  66. t[1].tag=1;
  67. t[1].e=a[i].t;
  68. }
  69. else
  70. {
  71. flag=0;
  72. break;
  73. }
  74. }
  75. if(flag)
  76. printf("YES\n");
  77. else
  78. printf("NO\n");
  79. return 0;
  80. }

发表评论

表情:
评论列表 (有 0 条评论,304人围观)

还没有评论,来说两句吧...

相关阅读