Codeforces 845C Two TVs【思维】水题
来源:互联网 发布:亚投行 现状 知乎 编辑:程序博客网 时间:2024/05/21 10:27
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?
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.
If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes).
31 22 34 5
YES
41 22 32 31 2
NO
题目大意:
现在一个节目的开始和结束区间已经给出【L,R】,问我们两台电视机是否能够观看完这N个节目。
思路:
我们直接将每个点离散化一下,然后每到一个节目的L端点,我们就要sum++,表示我们需要一台机器了,如果到一个节目的R+1端点,也就是节目结束的同时,我们sum--,表示这台机器暂时可以关闭一下了。
过程维护一下sum,如果sum>2了,那么说明两台机器不够,那么对应结果就是NO,否则就是YES.
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;struct node{ int val,pos,op;}a[4500000];int sum[4500000];int cmp(node a,node b){ if(a.pos==b.pos)return a.op<b.op; return a.pos<b.pos;}int main(){ int n; while(~scanf("%d",&n)) { int cnt=0; memset(sum,0,sizeof(sum)); for(int i=1;i<=n;i++) { int l,r; scanf("%d%d",&l,&r); a[cnt].pos=l; a[cnt++].op=1; a[cnt].pos=r+1; a[cnt++].op=-1; } int tot=1; sort(a,a+cnt,cmp); for(int i=0;i<cnt;i++) { a[i].val=tot; if(i==cnt-1||a[i].pos!=a[i+1].pos) { tot++; } } int flag=0; int summ=0; for(int i=0;i<cnt;i++) { summ+=a[i].op; if(summ>2)flag=1; } if(flag==1)printf("NO\n"); else printf("YES\n"); }}
- Codeforces 845C Two TVs【思维】水题
- Codeforces-845C:Two TVs(思维)
- codeforces 845C Two TVs
- Codeforces 845 C Two TVs
- CodeForces 845C Two TVs (模拟)
- Codeforces 845 C. Two TVs 思路:简单贪心算法
- Educational Codeforces Round 27 C. Two TVs(模拟)
- Codeforces 602C The Two Routes【最短路+思维】
- cfC. Two TVs
- 【codeforces 723C】 + 思维
- codeforces C. Queue (思维)
- codeforces-750【C思维】
- Codeforces 842C(思维)
- Codeforces--333C--The Two Routes(最短路弗洛伊德)(思维)
- codeforces 250/C 逆向思维
- Codeforces 651 C Watchmen【思维】
- codeforces-257【C思维,数学】
- codeforces-337【C思维,贪心】
- VIP(虚拟IP)设置-Ubuntu
- HDU2141-Can you find it?
- 推荐算法
- Linux下配置交叉编译环境arm-none-eabi-gcc
- 线性方程组迭代法--一个综述
- Codeforces 845C Two TVs【思维】水题
- 【PAT】【Advanced Level】1086. Tree Traversals Again (25)
- [JAVA] Spring 简述
- 求在一个格子里出现n棋子的最少操作数
- 云服务器 ECS Linux 系统 CPU 占用率较高问题排查思路
- 公钥与私钥
- 二叉搜索树
- 为什么需要htons(), ntohl(), ntohs(),htons() 函数
- 1.2.3python3函数