1303. Minimal Coverage

Given set of line segments [L i, R i] with integer coordinates of their end points. Your task is to find the minimal subset of the given set which covers segment [0, M] completely (M is a positive integer).

Input

First line of the input contains an integer M (1 ≤ M ≤ 5000). Subsequent lines of input contain pairs of integers L i  and R i  (−50000 ≤ L i  < R i  ≤ 50000). Each pair of coordinates is placed on separate line. Numbers in the pair are separated with space. Last line of input data contains a pair of zeroes. The set contains at least one and at most 99999 segments.

Output

Your program should print in the first line of output the power of minimal subset of segments which covers segment [0, M]. The list of segments of covering subset must follow. Format of the list must be the same as described in input with exception that ending pair of zeroes should not be printed. Segments should be printed in increasing order of their left end point coordinate.
If there is no covering subset then print “No solution” to output.

Samples

input output
1
-1 0
-5 -3
2 5
0 0
No solution
1
-1 0
0 1
0 0
1
0 1
**************************************************************************************
Greedy (reference to other people's code)
**************************************************************************************
 1 #include<iostream>
 2 #include<string>
 3 #include<cstring>
 4 #include<cstdio>
 5 #include<cmath>
 6 #include<algorithm>
 7 using namespace std;
 8 struct seg
 9 {
10     int s;
11     int e;
12   seg &operator =(seg &b)
13     {
14        s=b.s;
15        e=b.e;
16        return *this;
17     }
18 
19 }p[100010],ans[100010];
20 int M,i,j,k,t;
21 int m,n;
22 int min;
23 int gs;
24 void  init()
25 {
26     cin>>M;
27     i=0;
28     while(cin>>m>>n)
29      {
30          if(m==0&&n==0)
31           break;
32          p[i].s=m;
33          p[i++].e=n;
34      }
35      k=i;
36 }
37 bool cmp(seg a,seg b)
38  {
39      if(a.s<b.s)return true;
40      if(a.s>b.s)return false;
41      if(a.s==b.s)
42       {
43           if(a.e>b.e)return true;
44            else  return false;
45       }
 46 is  }
 47  int   main ()
 48  {
 49      the init ();
 50      Sort (P, P + K, CMP); // first sort, followed by the end 
51 is      T = 0 ;
 52 is      for (I = . 1 ; I <K ; I ++ )
 53 is       IF (P [I] .s> P [T] .s && P [I] .E> P [T] .E) P [T ++] = P [I]; // delete covered line 
54 is      K = T;
 55      P [K + . 1 ] .s = M + . 1 ; // for comparison is not out of range 
56 is      P [K + . 1 ] = M + .E . 1 ;
 57 is      J =0 ;
 58      int H = 0 ;
 59      for (I = 0 ; I <= K; I ++ )
 60       IF (j <P [I + . 1 ] .s && P [I] .s <= j) // satisfies the condition that is guaranteed j This is not the next one line to avoid repetition 
61 is         {
 62 is             J = P [I] .E;
 63 is             ANS [++ H] = P [I]; // record; 
64          IF (P [I] .E> = M) // satisfy the condition output 
65              {
 66                  COUT H << << endl;
 67                  for ( int G = . 1 ; G <= H; G ++)
68                  cout<<ans[g].s<<' '<<ans[g].e<<endl;
69                 return 0;
70             }
71        }
72     cout<<"No solution"<<endl;
73     return 0;
74 
75 
76 
77 }
View Code

 

 

Reproduced in: https: //www.cnblogs.com/sdau--codeants/p/3234803.html

Guess you like

Origin blog.csdn.net/weixin_34250434/article/details/93432981