Luogu: P1803 messy yyy / line segment coverage (popularity -, the simplest interval problem)

topic:

Insert picture description here

Code:

#include<bits/stdc++.h>
using namespace std;
struct node{
    
    
 int x1,x2;
}nn[1000005];
bool cmp(node n1,node n2)
{
    
    
 return n1.x2<n2.x2;
}
int main()
{
    
    
 int m;
 cin>>m;
 for(int i=0;i<m;i++) cin>>nn[i].x1>>nn[i].x2;
 sort(nn,nn+m,cmp);
 int t=0;
 int ans=0;
 for(int i=0;i<m;i++)
 {
    
    
  if(nn[i].x1>=t)
  {
    
    
   ans++;
   t=nn[i].x2;
  }
 }
 cout<<ans;
}

Guess you like

Origin blog.csdn.net/weixin_42721412/article/details/108504733
Recommended