UVa 10020-minimal Coverage (interval coverage and greed)

Source: Internet
Author: User

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

Test instructions: Given a m, and some intervals [l,r]. To select several intervals to cover the [0,m] range completely. Minimum required quantity. If the output 0 cannot be overwritten.

Thought: The greedy thought. Sort the intervals by r from large to small. Then meet a satisfied [Li,ri] and update the narrowing interval. Until fully covered.

Note [L,r] only satisfies the condition that L is less than or equal and R is greater than the left end of the current coverage interval. To be selected.

#include <stdio.h>#include<algorithm>using namespacestd;intT;intstart,end,n,f;structqujian{intstart; intend;}; Qujian q[100005],p[100005];intCMP (Qujian A,qujian b) {returnA.end >b.end;}intMain () {scanf ("%d", &t);  while(T--) {n=0; F=0; Start=0; scanf ("%d", &end);  while(SCANF ("%d%d", &q[n].start, &q[n].end) && Q[n].start +q[n].end) {N++; } sort (Q,q+n,cmp);  while(start<end) {            inti;  for(i=0; i<n;i++)            {                if(Q[i].start <= start && q[i].end >start) {Start= Q[i].end;//Update IntervalP[F] =Q[i]; F++;  Break; }            }            if(i==n) Break;//If there is not an interval that satisfies the condition, it ends directly.         }        if(start<end) printf ("0\n"); Else{printf ("%d\n", F);  for(intI=0; i<f;i++) printf ("%d%d\n", P[i].start,p[i].end); }        if(t) printf ("\ n"); }    return 0;}

UVa 10020-minimal Coverage (interval coverage and greed)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.