week3 job C

The meaning of problems:
there are n (1 <= n <= 25000) a closed interval [ai, bi] number line, selected to cover a range of as little as specified segment [1, t] (1 < = t <= 1,000,000).

Covering the entire point, i.e., (1,2) + (3,4) may cover (1,4).

The impossible Output -1
INPUT:
first line: N T and the
second row N + 1 to row: each line a closed interval.
output:
number of sections of the selected output of the impossible -1
Sample INPUT:
. 3 10
. 1. 7
. 3. 6
. 6 10
Sample output:
2
Note:
This question a lot of input data, use instead scanf cin
ideas:
define a structure body, including the left and right points of the point section. Enter the number of sections and the final requirements of the subject, and the respective end sections. Interval input of the sort.
In the cycle, as subject of the request interval finally formed [1, T], so that the interval starting point of the left must not be greater than 1. When it is determined, the start point of the left end section out of the loop is greater than 1, when the right end is determined to have a certain interval t may be larger than the end of this cycle.
After it is determined to continue, and the right end of this range is determined by size determination of the right end point of the interval if the left end point of a section smaller than the determination of the left point. Because we judge the process, before the right end point has been continuous, so we need to determine the right end point and constantly updated. Therefore, in the above case it is determined if the interval required for scalar included in the interval, still taking the original interval, otherwise, it is used to update a scalar quantity for an interval determination section. Interval number unchanged.
In another case, if the required interval is determined to be greater than the left end of the left scalar point interval, interval number increases, the left end point update interval scalar scalar plus 1. In this case the right end of the interval determination section determines the need At this time, the size of the left point and the left point scalar interval. If the required range is determined larger than the left end of the left end of the scalar range, it can not form a continuous final section, the end out of the loop. Conversely, the scalar update determination section right point.
Finally, the output requirements of the subject template according to the judgment result.

Code:

#include<iostream>
#include<algorithm>
#include<vector>
#include<cstdio>
using namespace std;
struct node{
 int left;
 int right;
};
bool cmp(node a,node b)
{
 if(a.left!=b.left)
    return a.left<b.left;
 return a.right>b.right;
}
int main()
{
 int n,t;
 scanf("%d%d",&n,&t);
 node temp[100000];
 for(int i=0;i<n;i++)
 {
  scanf("%d%d",&temp[i].left,&temp[i].right);
 }
 sort(temp,temp+n,cmp);
 int count=1;
 int left=1,right=0;
 for(int i=0;i<n;i++)
  {
  if((i==0&&temp[i].left>1)||right>=t)
     break;
  if(temp[i].left<=left)
  {
   if(temp[i].right>right)
      right=temp[i].right;
  }
   
  if(temp[i].left>left)
  {
     count++;
   left=right+1;
   if(temp[i].left<=left)
   {
    if(temp[i].right>right)
       right=temp[i].right;
   }
   else
      break;
  }
 }
  if(right<t)
   printf("-1\n");
 else
    printf("%d\n",count);
 return 0;
}
Published 19 original articles · won praise 0 · Views 216

Guess you like

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