845C - Two TVs(扫描线)
845C - Two TVs
题解是用扫描线
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn=2e5+10; 4 5 struct Node{ 6 int x,v; 7 bool operator<(const Node& a)const{ 8 if(x==a.x) return v>a.v; // 先处理结束的点 9 return x<a.x; 10 } 11 }p[maxn<<1]; 12 13 int main(){ 14 int n; 15 scanf("%d",&n); 16 int x,y; 17 int cnt=0; 18 for(int i=0;i<n;i++){ 19 scanf("%d%d",&x,&y); 20 p[cnt++]=Node{x,1}; 21 p[cnt++]=Node{y,-1}; 22 } 23 sort(p,p+cnt); 24 int ans=0; 25 for(int i=0;i<cnt;i++){ 26 if(p[i].v==1) ans++; 27 else ans--; 28 if(ans==3) break; 29 } 30 if(ans==3) puts("NO"); 31 else puts("YES"); 32 }
我觉得我写的也差不多就是扫描线~
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn=2e5+10; 4 struct node{ 5 int s,e; 6 bool operator<(const node& a)const{ 7 return s<a.s||s==a.s&&e<a.e; 8 } 9 }p[maxn]; 10 int n; 11 12 int main(){ 13 scanf("%d",&n); 14 for(int i=0;i<n;i++)scanf("%d%d",&p[i].s,&p[i].e); 15 sort(p,p+n); 16 int last2=p[0].e,last1=p[1].e; 17 18 for(int i=2;i<n;i++){ 19 if(p[i].s<=last1&&p[i].s<=last2) { 20 puts("NO"); 21 return 0; 22 }else if(p[i].s>last2){ 23 last2=p[i].e; 24 continue; 25 }else if(p[i].s>last1){ 26 last1=p[i].e; 27 continue; 28 } 29 } 30 puts("YES"); 31 return 0; 32 }