week3 job B

The meaning of problems:
there are n closed interval [a_i, b_i] number line. Take as few points, such that each section has at least one point (the interval containing different points may be the same).
input:
The first line of an integer N (N <= 100)

Of 2 N + 1 ~ line, each row two integers A, B (A, B <= 100)
Output:
an integer representing the number of points selected
examples:
the Input
2
. 1. 5
. 4. 6
the Output
. 1
the Input
. 3
. 1. 3
2. 5
. 6. 4
the Output
2
ideas: the
definition of a structure, which includes left and right points points. Enter the number of intervals and each interval, the interval of the sort. When the left end of a range of points in the first section on the right of the right end, the number of points increases. After the right end point of the back section after the section to the left point and compared. If the left end section traversed point is less than the left end of the interval for comparison, it indicates that they have the same point, without increasing the number of points.

Code:

#include<iostream>
#include<algorithm>
using namespace std;
struct node{
 int left;
 int right;
 operator<(const node &te) const
 {
  if(right!=te.right)
     return right<te.right;
  return left>te.left;
 }
};
int main()
{
 int n;
 cin>>n;
 //int left[n]={0};
 //int right[n]={0};
 //int count=1;
 node temp[n]={0};
 //int count=0;
 int count=1;
 //while(cin>>n)
 //for(int i=0;i<n;i++)
 //{
  for(int i=0;i<n;i++)
  {
   //cin>>left[i]>>right[i];
   cin>>temp[i].left>>temp[i].right;
  }
  sort(temp,temp+n);
  int m=temp[0].right;
  //count++;
  for(int j=1;j<n;j++)
  {
   //if(right[j]<left[j+1])
   //if(temp[j].right<temp[j+1].left)
   if(m<temp[j].left)
   {
    count++;
    //right[j]=right[j+1];
    //temp[j].right=temp[j+1].right;
    m=temp[j].right;
   }
   }
   cout<<count<<endl;
   return 0;
   }
Published 19 original articles · won praise 0 · Views 217

Guess you like

Origin blog.csdn.net/weixin_45117273/article/details/104770031