区间覆盖

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

 

思路:

    把所有区间从小到大排序,每次进行比较,找区间右边尽量大的,直到覆盖整个区间

 

源代码:

    

 1 #include<iostream>
 2 #include<cstring>
 3 #include<string>
 4 #include<algorithm>
 5 using namespace std;
 6 #define maxn 100000+5
 7 int M;
 8 struct Seg{
 9     int left;
10     int right;
11     friend bool operator<(const Seg&a, const Seg&b)
12     {
13         if (a.left != b.left)
14             return a.left<b.left;
15         return a.right> b.left;          //现在还不懂为什么要这样/(ㄒoㄒ)/~~
16     
17     }
18 }q[maxn];
19 int ai[maxn];
20 int main()
21 {
22     int T;
23     cin >> T;
24     while (T--)
25     {
26         int flag = 0;
27         cin >> M;
28         int Index = 0;
29             while (cin >> q[Index].left >> q[Index].right)
30             {
31                 
32                 if (q[Index].left==0&&q[Index].right==0)
33                             break;    
34                 if (q[Index].right > 0) ++Index;   //把区间右边大于0的才存进去
35             }
36         
37         sort(q, q + Index);
38         if (q[0].left > 0)                  //第一个的左边都大于0,表示没有区间可以覆盖目标区间
39         {
40             cout << "0\n";
41             if (T)                        //不是最后一组数据要求输出空行
42                 cout << endl;
43                 continue;
44             
45         }
46         int cur = 0;
47         int j = -1;
48         for (int i = 0; i < Index; i++)
49         {
50             if (q[i].left <= cur)     //区间左边小于0的满足初步条件
51             {
52                 if (j == -1)
53                     ai[++j] = i;         
54                 else if (q[i].right > q[ai[j]].right)      //找区间右边大的
55                     ai[j] = i;
56             }
57                 else
58                 {
59                     cur = q[ai[j]].right;      //找区间右边尽量大的
60                     ai[++j] = i;
61                 }
62 
63                 if (q[ai[j]].right >= M)     //覆盖了整个区间
64                 {
65                     flag = 1;
66                     break;
67                 }
68             
69         }
70         if (flag)
71         {
72             cout << j + 1 << endl;
73             for (int i = 0; i <=j; i++)
74                 
75                 cout << q[ai[i]].left << " " << q[ai[i]].right << endl;
76             
77         }
78             else
79             
80                 cout << "0\n";
81                 if (T)
82                     cout << endl;
83             
84     }
85 
86     return 0;
87 }

 

转载于:https://www.cnblogs.com/Lynn0814/p/4716081.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值