【区间覆盖问题】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 ...
随机推荐
- 进入IT行业,你后悔过吗?
问:你曾后悔进入 IT 行业吗?为什么? 也许你后悔做了IT,但是很希望你能用自己混IT界的惨痛经历给题主这样的后来人提个醒. 也许你庆幸做了IT,同样很希望能够看到同行朋友们的真诚交流. miao ...
- mysql fetch 系列函数
浏览器输出内容同上. 当使用 MYSQL_BOTH 或省略该参数是,将同时具有 MYSQL_NUM 与 MYSQL_ ASSOC 的特性. MySQL mysql_fetch_array 函数取得查询 ...
- HW5.26
public class Solution { public static void main(String[] args) { int totalCount = 0; int lineCount = ...
- POJ3922 A simple stone game
网上有很多解题报告,我的理解就是可以用类似数学归纳的方法证明,就是取一个数,让对手进入必败态. 详细见论文.
- 一切不以用户为中心的O2O 都是耍流氓
今天去万达广场逛街,手机搜了下附近的Wifi,发现有万达的免费Wifi,想起前些日子网上说万达要做O2O的试运营,于是尝试连接了下,结果弹出页面,要输入手机号,然后发送验证码才能登陆,结果输入手机号, ...
- linux磁盘简单分区方式
1:分区 fdisk /dev/sdb 2:格式化 mkfs -t ext3 /dev/sdb1 或者 mke2fs -t ext4 /dev/sdb2 3:挂载 mount /dev/sdb1 ...
- xampp配置host和httpd可以随意访问任何本机的地址
1.修改host 不管你用的是什么系统,windows, mac,电脑上都会有一个 hosts 文件,修改这个文件,可以改变主机名所对应的 ip 地址.比如你安装了 Web 开发环境(MAMP 或 W ...
- Python中如何把一个UTC时间转换为本地时间
需求: 将20141126010101格式UTC时间转换为本地时间. 在网上搜了好长时间都没有找到完美的解决方案.有的引用了第三方库,这就需要在现网安装第三方的软件.这个是万万不可的.因为真实环境不一 ...
- 剑指OFFER之第一个只出现一次的字符(九度OJ1283)
题目描述: 在一个字符串(1<=字符串长度<=10000,全部由大写字母组成)中找到第一个只出现一次的字符. 输入: 输入有多组数据每一组输入一个字符串. 输出: 输出第一个只出现一次的字 ...
- 实现windows和linux互传文件
http://www.cnblogs.com/ylan2009/archive/2012/01/12/2321126.html 尝试从windows xp向ubuntu11.10传文件 ubuntu使 ...