码迷,mamicode.com
首页 > 其他好文 > 详细

uva.10020 Minimal coverage(贪心)

时间:2015-05-19 18:50:56      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:

10020

 


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].
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.
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

贪心是要讲规则的,这道题的规则就是:首先要覆盖x=0这个点,那么选谁呢?自然选L<=0 && R 尽可能大的区间[l0,r0];然后下一个要覆盖的是x=ro,采取同样的策略……直到某一轮的选出的区间覆盖了x=m。

技术分享
 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<algorithm>
 4 const int inf = 0x3f3f3f3f ;
 5 int T ;
 6 int m ;
 7 int path[100000 + 10] ;
 8 struct node
 9 {
10     int l , r ;
11 }e[100000 + 10];
12 
13 bool cmp (node a , node b)
14 {
15     return a.l < b.l ;
16 }
17 
18 int main ()
19 {
20     //freopen ("a.txt" , "r" , stdin ) ;
21     scanf ("%d" , &T) ;
22     int cas = 0 ;
23     while (T --) {
24         if (cas != 0 ) puts ("") ;
25         cas ++ ;
26         scanf ("%d" , &m ) ;
27         int n = 0 ;
28         while (1) {
29             scanf ("%d%d" , &e[n].l , &e[n].r ) ;
30             if (e[n].l == 0 && e[n].r == 0) break ;
31             n ++ ;
32         }
33        //printf ("n = %d\n" , n );
34         std::sort (e , e + n , cmp) ;
35         bool flag = 0 ;
36         int cur = 0 , id = -1 , maxn = -inf ;
37         int tot = 0 ;
38         for (int i = 0 ; i < n ; i ++) {
39             for (int j = 0 ; j < n ; j ++) {
40                // printf ("l = %d , r = %d , cur = %d\n" , e[j].l , e[j].r , cur ) ;
41                 if (e[j].r > cur && e[j].l <= cur ) {
42                     if (e[j].r > maxn) {
43                         id = j ;
44                         maxn = e[j].r ;
45                     }
46                 }
47             }
48             if (id == -1) {
49                     flag = 1 ;
50                     break ;
51             }
52             path[tot ++] = id ;
53             cur = e[id].r ;
54             id = -1 ; maxn = -inf ;
55             if (cur >= m) break ;
56         }
57         if (flag) puts ("0") ;
58         else {
59             printf ("%d\n" , tot ) ;
60             for (int i = 0 ; i < tot ; i ++) {
61                 printf ("%d %d\n" , e[path[i]].l , e[path[i]].r ) ;
62             }
63         }
64     }
65     return 0 ;
66 }
View Code

uva.10020 Minimal coverage(贪心)

标签:

原文地址:http://www.cnblogs.com/get-an-AC-everyday/p/4515133.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!