博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
区间覆盖
阅读量:4975 次
发布时间:2019-06-12

本文共 3489 字,大约阅读时间需要 11 分钟。

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
2 #include
3 #include
4 #include
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; //现在还不懂为什么要这样/(ㄒ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 else58 {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 else79 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

你可能感兴趣的文章
CSS实现单行、多行文本溢出显示省略号
查看>>
vs里面的移位运算问题
查看>>
mysql如何利用Navicat 导出和导入数据库
查看>>
Swift 元组 Tuple
查看>>
MyISAM 和 InnoDB 讲解
查看>>
乐观锁与悲观锁
查看>>
快速排序 冒泡排序
查看>>
帝国cms安装在二级目录 构建中英文网站
查看>>
zoj分类
查看>>
awk系列:在awk中如何使用流程控制语句
查看>>
Windows10的周年更新中无法关闭Cortana?这里有方法
查看>>
用到的一些方法
查看>>
JS 生成二维码和加上logo图片
查看>>
C++笔记-类层次结构
查看>>
Net::OpenSSH 使用例子
查看>>
mysql READ-COMMITTED 模式下 行锁不会升级到表级锁
查看>>
Burst Windows 2003 VPS安装中文语言包图文教程
查看>>
公式图片生成代码
查看>>
世博会
查看>>
最近要看的东西
查看>>