Original question:
Given several segments of line (int the X axis) with coordinates [Li, Ri]. You is to choose the minimal amount of them, such they would completely cover the segment [0, M].
Input
The first line was the number of test cases, followed by a blank line.
Each test case in the input should contains a integer M (1≤m≤5000), followed by pairs "Li Ri"
(| li|, | ri| ≤50000, i≤100000), each on a separate line. Each test case of input was terminated by pair
' 0 0 '.
Each test case is separated by a.
Output
For each test case, at the first line of output your programm should print the minimal number of line
Segments which can cover segment [0, M]. In the following lines, the coordinates of segments, sorted
By their left end (Li), should is printed in the same format as in the input. Pair ' 0 0 ' should not being
Printed. If [0, M] can is covered by given line segments, your programm should print ' 0 ' (without
Quotes).
Print a blank line between the outputs for the consecutive test cases.
Sample Input
2
1
-1 0
-5-3
2 5
0 0
1
-1 0
0 1
0 0
Sample Output
0
1
0 1
Effect:
First give you a number T, tell you how many test samples you have, and then give you a number m to denote the interval [0,m]. Then give you a few pairs of interval [Li,ri], when the interval is 0, 0 o'clock input end. Now ask you to cover [0,m] with the fewest intervals.
#include <iostream> #include <algorithm> #include <map> #include <string> #include <cstring > #include <sstream> #include <cstdio> #include <vector> #include <cmath> #include <stack > #include <queue> #include <iomanip> #include <set> #include <fstream> #include <climits
> Using namespace std;
FStream input,output;
struct SEG {int s,e;};
SEG s[100001];
int m;
Vector<int> ans;
int cmp (const SEG &a,const seg &b) {return a.s<b.s;} int main () {Ios::sync_with_stdio (false);
int t,a,b,index,last,w;
cin>>t;
while (t--) {last=index=0;
cin>>m;
Ans.clear ();
while (cin>>a>>b) {if (a==0&&b==0) break;
if (b>0&&a<m) {s[index].s=a;
S[index].e=b;
index++; }} sort (s,s+iNDEX,CMP);
int tmp=-1;
int i=0;
while (true) {w=m;
if (i>=index) break;
while (I<index&&s[i].s<=last) {if (m-s[i].e<w) {
Tmp=i;
W=M-S[I].E;
} i++;
} if (tmp==-1) {i++;
Continue
} ans.push_back (TMP);
LAST=S[TMP].E; if (s[tmp].e>=m| |
I>=index) break;
} if (Ans.size () ==0) cout<<0<<endl;
else {cout<<ans.size () <<endl; for (int i=0;i<ans.size (); i++) {cout<<s[ans[i]].s<< "" <<s[ans[i]].e<&l
T;endl;
}} if (t>0) cout<<endl;
}//Input.close (); Output.cloSE ();
return 0;
}
Answer:
The obvious greedy problem is that the average interval greedy problem is to sort the interval by one of the short or the length, and then each time find the option closest to the target. This problem is also the case, you can start from small to large to sort, and then each time to select the end of the cell between the distance m point nearest the interval, at the same time to ensure that the current starting point and the last selection of the interval at the end of the connection can be.