CodeForces 845C Two TVs (模拟)
2017-09-06 18:17
666 查看
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
output
input
output
一开始想错了,没有注意到这是两台电脑而只是看到不能在同一台电脑上看到两个节目的这个条件忽略了这是两台电脑。
思路:记录两台电视的使用情况以及当前播放节目的结束时间,然后模拟一下就可以了。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct node
{
int l,r;
}q[300000];
bool cmp(node a,node b)
{
if(a.l==b.l)
return a.r<b.r;
return a.l<b.l;
}
struct mach
{
int flag,r;
}tv[2];
int main()
{
int n;
while(~scanf("%d",&n))
{
tv[0].flag=0,tv[1].flag=0;
int f=1;
for(int i=0;i<n;i++)
scanf("%d%d",&q[i].l,&q[i].r);
sort(q,q+n,cmp);
for(int i=0;i<n;i++)
{
int time=q[i].l;
int flag=0;
if(!tv[0].flag)
{
tv[0].flag=1;
tv[0].r=q[i].r;
flag=1;
}
else if(tv[0].flag)
{
if(time>tv[0].r)
{
tv[0].r=q[i].r;
flag=1;
}
}
if(!tv[1].flag&&!flag)
{
tv[1].flag=1;
tv[1].r=q[i].r;
flag=1;
}
else if(tv[1].flag&&!flag)
{
if(time>tv[1].r)
{
tv[1].r=q[i].r;
flag=1;
}
else
{
f=0;
break;
}
}
if(!flag)
{
f=0;
break;
}
}
if(f)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
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
一开始想错了,没有注意到这是两台电脑而只是看到不能在同一台电脑上看到两个节目的这个条件忽略了这是两台电脑。
思路:记录两台电视的使用情况以及当前播放节目的结束时间,然后模拟一下就可以了。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct node
{
int l,r;
}q[300000];
bool cmp(node a,node b)
{
if(a.l==b.l)
return a.r<b.r;
return a.l<b.l;
}
struct mach
{
int flag,r;
}tv[2];
int main()
{
int n;
while(~scanf("%d",&n))
{
tv[0].flag=0,tv[1].flag=0;
int f=1;
for(int i=0;i<n;i++)
scanf("%d%d",&q[i].l,&q[i].r);
sort(q,q+n,cmp);
for(int i=0;i<n;i++)
{
int time=q[i].l;
int flag=0;
if(!tv[0].flag)
{
tv[0].flag=1;
tv[0].r=q[i].r;
flag=1;
}
else if(tv[0].flag)
{
if(time>tv[0].r)
{
tv[0].r=q[i].r;
flag=1;
}
}
if(!tv[1].flag&&!flag)
{
tv[1].flag=1;
tv[1].r=q[i].r;
flag=1;
}
else if(tv[1].flag&&!flag)
{
if(time>tv[1].r)
{
tv[1].r=q[i].r;
flag=1;
}
else
{
f=0;
break;
}
}
if(!flag)
{
f=0;
break;
}
}
if(f)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
相关文章推荐
- Codeforces-845C:Two TVs(思维)
- Codeforces 272D Dima and Two Sequences【思维+模拟】
- Codeforces 845 C Two TVs(贪心之优先队列维护)
- 【Codeforces】-632B-Alice, Bob, Two Teams(模拟,思维)
- Codeforces--616A--Comparing Two Long Integers(模拟水题)
- Codeforces 845 C Two TVs
- 【CodeForces】632B - Alice, Bob, Two Teams(模拟)
- codeforces 767 b The Queue(模拟)
- CodeForces 811B——Vladik and Complicated Book ——思维,模拟
- 【CodeForces 803B】Distances to Zero(模拟)
- Codeforces 426 B Sereja and Mirroring【暴力枚举+模拟】水题
- 【CodeForces 797B】Odd sum(模拟)
- Codeforces Good Bye 2016 C. New Year and Rating(模拟)
- codeforces 251 div2 C. Devu and Partitioning of the Array 模拟
- CodeForces - 668B Little Artem and Dance(模拟,思路)
- 【CodeForces 777A】Shell Game(模拟)
- codeforces 732D 模拟暴力
- Codeforces 676D Theseus and labyrinth 模拟+bfs
- CodeForces 471B MUH and Important Things 模拟 水
- 【模拟】Codeforces 706A Beru-taxi