UVA10020 - Minimal coverage

 

 Minimal coverage 

 

The Problem

Given several segments of line (int the X axis) with coordinates [Li,Ri]. You are to choose the minimal amount of them, such they would completely cover the segment [0,M].

 

The Input

 

The first line is the number of test cases, followed by a blank line.

Each test case in the input should contains an 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 is terminated by pair "0 0".

Each test case will be separated by a single line.

 

The Output

For each test case, in 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 be printed in the same format as in the input. Pair "0 0" should not be printed. If [0,M] can not be covered by given line segments, your programm should print "0"(without quotes).

Print a blank line between the outputs for two 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
题目大意:给定N条线段,最少需要选择多少条线段可以覆盖区间[0,M]。
题解:贪心问题。对于输入的数据,我们可以过滤掉和区间[0,M]没有公共区间的线段,因为这些线段没有意义。之后按左端值对线段进行升序排序。假设s为需要覆盖区间的起点,如果区间1的左端点大于s,则说明问题无解,否则选择左端点小于等于s的右端点值最大的区间。选择区间[Li,Ri]之后,新的起点应该设置为Ri,并重复进行上述操作,直到遇到无法覆盖的情况或者已经覆盖完毕。我这题WA了三次,原因是每更新一次起点之后,忘记判断不能够再进行覆盖的情况,也就是说之后的所有点的左端点都在起点的右边,害得我一直循环,o(╯□╰)o。
View Code
 1 #include<stdio.h>
 2 #include<string.h>
 3 #define MAXSN 100005
 4 typedef struct
 5 {
 6     long x;
 7     long y;
 8 } NODE;
 9 NODE s[MAXSN];
10 NODE f[MAXSN];
11 void qsort(long l,long r)
12 {
13     long i,j,mid;
14     NODE temp;
15     i=l;
16     j=r;
17     mid=s[(l+r)>>1].x;
18     while(i<=j)
19     {
20         while(s[i].x<mid) i++;
21         while(s[j].x>mid) j--;
22         if(i<=j)
23         {
24             temp=s[i];
25             s[i]=s[j];
26             s[j]=temp;
27             i++;
28             j--;
29 
30         }
31     }
32     if(i<r) qsort(i,r);
33     if(j>l) qsort(l,j);
34 }
35 int main(void)
36 {
37     long i,j,k,m,n,ans,a,b,r,max,flag;
38     NODE tt;
39     scanf("%ld",&n);
40     while(n--)
41     {
42         scanf("%ld",&m);
43         ans=0;
44         r=0;
45         k=0;
46         while(scanf("%ld%ld",&a,&b)==2)
47         {
48             if(a==0&&b==0) break;
49             if((a<=0&&b<=0)||(a>=m&&b>=m)) continue;
50             s[ans].x=a;
51             s[ans].y=b;
52             ans++;
53         }
54         qsort(0,ans-1);
55         if(s[0].x>0)
56         {
57             printf("0\n");
58             if(n) printf("\n");
59             continue;
60         }
61         i=0;
62         max=-1;
63         j=0;
64         flag=0;
65         while(i<ans)
66         {
67             while(s[i].x<=r&&i<ans)
68             {
69                 if(s[i].y>max)
70                 {
71                     tt=s[i];
72                     max=s[i].y;
73 
74                 }
75                 i++;
76             }
77             f[j++]=tt;
78             r=max;
79             if(r>=m) break;
80             if(s[i].x>r) break;
81         }
82         if(r<m)
83             printf("0\n");
84         else
85         {
86             printf("%ld\n",j);
87             for(i=0; i<j; i++)
88                 printf("%ld %ld\n",f[i].x,f[i].y);
89         }
90         if(n) printf("\n");
91 
92     }
93     return 0;
94 }

 

 

 

posted on 2013-03-07 16:25  仗剑奔走天涯  阅读(307)  评论(0编辑  收藏  举报

导航