参考:https://blog.csdn.net/xjh_shin/article/details/77491693
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstring> 5 using namespace std; 6 int n; 7 const int N=2*1e5+5; 8 struct node 9 { 10 int s,e;//s为开始时间,e为结束时间 11 }no[N]; 12 int cmp(struct node x,struct node y)//应该先按开始时间排序,再按结束时间排序! 13 { 14 if (x.s==y.s) 15 { 16 return x.e<y.e; 17 } 18 return x.s<y.s; 19 } 20 void test() 21 { 22 for (int i=0;i<n;i++) 23 { 24 printf("%d %d ",no[i].s,no[i].e); 25 } 26 getchar(); 27 } 28 int main() 29 { 30 // freopen("btext.txt","r",stdin); 31 while (cin>>n) 32 { 33 for (int i=0;i<n;i++) 34 { 35 cin>>no[i].s>>no[i].e; 36 } 37 if (n<3) 38 { 39 cout<<"YES"<<endl; 40 continue; 41 } 42 sort(no,no+n,cmp); 43 // test();//提交前要记得注释测试语句! 44 int flag,ta,tb; 45 ta=no[0].e; 46 tb=no[1].e; 47 flag=2;//已看的节目数量 48 for (int i=2;i<n;i++) 49 { 50 if (ta<no[i].s) 51 { 52 flag++; 53 if (flag==n) 54 { 55 cout<<"YES"<<endl; 56 break; 57 } 58 ta=no[i].e; 59 continue; 60 } 61 if (tb<no[i].s) 62 { 63 flag++; 64 if (flag==n) 65 { 66 cout<<"YES"<<endl; 67 break; 68 } 69 tb=no[i].e; 70 continue; 71 } 72 cout<<"NO"<<endl; 73 break; 74 } 75 } 76 77 return 0; 78 }