【区间覆盖问题】uva 10020 - Minimal coverage
可以说是区间覆盖问题的例题...
Note:
区间包含+排序扫描;
要求覆盖区间[s, t];
1、把各区间按照Left从小到大排序,如果区间1的起点大于s,则无解(因为其他区间的左起点更大);否则选择起点在s的最长区间;
2、选择区间[li, ri]后,新的起点应更新为ri,并且忽略所有区间在ri之前的部分;
| 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 代码如下
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = ;
struct Seg
{
int L, R;
bool operator < (const Seg &a) const
{
if(L != a.L) return L < a.L;
else return R > a.R;
}
} seg[maxn], ans[maxn];
void solve(int n, int m)
{
int cnt = , l = , r = ;//起点l,起点l最长区间的右端点r
if(seg[].L > l)
{
printf("0\n");
return ;
}//无解
bool ok = false;
while()
{
if(l >= m) break;
int i, pos = ;
ok = false;//判断变量ok,重要!
for(i = ; i < n; i++)
{
if(seg[i].L <= l)
{
if(seg[i].R > r)
{
pos = i;
r = seg[pos].R;
ok = true;
}//寻找起点在l的最长区间
}
}
if(ok)
{
l = r;
ans[cnt].L = seg[pos].L;
ans[cnt++].R = seg[pos].R;
}//更新起点l
else break;
}
if(ok)
{
printf("%d\n", cnt);
for(int i = ; i < cnt; i++)
printf("%d %d\n", ans[i].L, ans[i].R);
}
else printf("0\n");
}
int main()
{
int T;
scanf("%d", &T);
for(int kase = ; kase < T; kase++)
{
if(kase) printf("\n");
memset(seg, , sizeof(seg));
memset(ans, , sizeof(ans));
int m; scanf("%d", &m);
int i = , L, R;
while(scanf("%d%d", &L, &R))
{
if(!L && !R) break;
seg[i].L = L; seg[i].R = R;
i++;
}
sort(seg, seg+i);//排序
solve(i, m);
}
return ;
}
【区间覆盖问题】uva 10020 - Minimal coverage的更多相关文章
- UVA 10020 Minimal coverage(贪心 + 区间覆盖问题)
Minimal coverage The Problem Given several segments of line (int the X axis) with coordinates [Li, ...
- uva 10020 Minimal coverage 【贪心】+【区间全然覆盖】
Minimal coverage The Problem Given several segments of line (int the X axis) with coordinates [Li,Ri ...
- uva.10020 Minimal coverage(贪心)
10020 Given several segments of line (int the X axis) with coordinates [Li, Ri]. You are to choose t ...
- UVa 10020 - Minimal coverage(区间覆盖并贪心)
Given several segments of line (int the X axis) with coordinates [Li, Ri]. You are to choose the min ...
- uva 10020 Minimal coverage
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- UVa 10020 (最小区间覆盖) Minimal coverage
题意: 数轴上有n个闭区间[ai, bi],选择尽量少的区间覆盖一条指定线段[0, m] 算法: [start, end]为已经覆盖到的区间 这是一道贪心 把各个区间先按照左端点从小到大排序,更新st ...
- UVA10020:Minimal coverage(最小区间覆盖)
题目: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=68990#problem/M 题目需求:数轴上有n个闭区间[ai,bi],选择尽量 ...
- Uva 10382 (区间覆盖) Watering Grass
和 Uva 10020几乎是一样的,不过这里要把圆形区域转化为能够覆盖的长条形区域(一个小小的勾股定理) 学习一下别人的代码,练习使用STL的vector容器 这里有个小技巧,用一个微小量EPS来弥补 ...
- UVA 10382 - Watering Grass【贪心+区间覆盖问题+高精度】
UVa 10382 - Watering Grass n sprinklers are installed in a horizontal strip of grass l meters long a ...
随机推荐
- 输入一个整数n,求从1到n这n个整数的十进制表示中1出现的次数
题目:输入一个整数n,求从1到n这n个整数的十进制表示中1出现的次数.例如输入12,从1到12这些整数中包含1 的数字有1,10,11和12,1一共出现了5次. 分析:首先最先想到的是遍历从1到n的每 ...
- 字符串旋转(str.find()---KMP)
此题旋转带有技巧性,问题转化为常见的问题,熟练STL可以直接用str.find()函数,其是主要想用KMP算法实现字符串的查找算法... //如果对于一个字符串A,将A的前面任意一部分挪到后边去形成的 ...
- ubuntu下PHP支持cURL
公司项目需要,注册需要验证手机号码,其中需要LAMP支持cURL.由于事先安装平台的时候,并没有注意到这一点,所以编译PHP5的时候,并没有使用参数--with-curl.后来需要的时候,查一些参考方 ...
- 细谈Linux和windows差异之图形化用户接口、命令行接口
相信来看本博文的朋友,肯定是已经玩过linux好段时间了,才能深刻理解我此番话语. 这是在Windows下的命令行接口 这是windows下的用户接口 就是它,explorer.ext,可以去尝试.把 ...
- Java自定义缓冲区MyBufferedReader
package IODemo; import java.io.FileReader; import java.io.IOException; import java.io.Reader; /** * ...
- 【Stage3D学习笔记续】真正的3D世界(一):透视矩阵
如果各位看官跟着我的学习笔记一路看过来的话,一定会吐槽我的,这都是什么3D啊?从头到尾整个都是在使用GPU绘制一堆2D图像而已,的确,之前我们一直使用正交矩阵利用GPU加速来实现2D世界的展示,算不上 ...
- SQL Server重建索引计划
每周日2点进行”一致性检查“ 每周六1点进行”重建索引“,重建索引会自动完成更新统计信息操作
- C#-datagridview右键选中行
在datagridview中有时需要在右键点击某行的时候就选中它,那么我们只需要在datagridview的CellMonseDown事件中添加如下代码就行: && e.ColumnI ...
- ZOJ 3822 Domination 期望dp
Domination Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/showProblem ...
- SqlServer按照指定顺序对字段进行排序
最近的一个项目,使用存储过程对报表进行分析,其中有一些名称需要根据指定顺序显示,而其名称对应的编号并不是按照要求的顺序排列的.通过上网查找资料,发现sql 中的charindex函数可以帮助解决这个问 ...