【区间覆盖问题】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 ...
随机推荐
- 验证码生成-->漂亮啊
验证码不用输出太多的HTML代码,直接创建一个一般处理程序,直接上代码 public class VCode : IHttpHandler { HttpContext context = null; ...
- 自动脚本工具新版 v2.0
自动脚本工具 下载 下载工具后,解压,直接双击 "execute.bat" 文件后(前提已配置好 jdk 1.7 的环境),会生成文件夹 "output",该文 ...
- ASP.NET使用Jquery-Ajax向ashx传递参数中文出现乱码
今天遇到个问题,IE11下Jquery-Ajax向ashx传递参数中文出现乱码,但在谷歌.火狐.360等浏览器中没有乱码的问题,百度了好久最后发现使用escape()对参数值进行处理就可以了: 参考代 ...
- cygwin远程操作linux
远程登录 1.ssh <username>@<IP> eg:ssh root@10.20.30.255 2.输入密码就OK 远程拷贝 1.scp -r <username ...
- HTML5学习之FileReader接口
http://blog.csdn.net/zk437092645/article/details/8745647 用来把文件读入内存,并且读取文件中的数据.FileReader接口提供了一个异步API ...
- 《Genesis-3D开源游戏引擎完整实例教程-跑酷游戏篇01:道路的自动生成》
1.道路的自动生成 道路自动生成概述: 3D跑酷游戏的核心就是跑,在跑这一过程中增加趣味性使得游戏具有更多的可玩性.道路的自动生成和自由拼接,为游戏增设了更多的不可预见性.这种不可预见性使得玩家在游戏 ...
- Esper系列(十四)Contained-Event Selection
功能:该语法是针对所查询事件中的属性又是另一种属性的查询结果控制. 格式: 1 "+j); 19 bean.setBean(item); 20 list.add(bea ...
- Java中的IP对象以及本地域名解析
本地域名解析操作步骤: 1.打开C:\WINDOWS\system32\drivers\etc目录 2.找到host文件,用记事本打开 3.添加“空间IP 域名” package WebProgra ...
- 获取本机IP地址和MAC地址
unit NetFunc; interface uses SysUtils, Windows, dialogs, winsock, Classes, ComObj, WinInet, Variants ...
- 74HC595 for STM32 源代码【worldsing笔记】
74HC595是硅结构的CMOS器件, 兼容低电压TTL电路,遵守JEDEC标准. 74HC595是具有8位移位寄存器和一个存储器,三态输出功能. 移位寄存器和存储器是分别的时钟. 数据在SHcp(移 ...