【区间覆盖问题】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 ...
随机推荐
- jquery 日期控件
控件官网: http://www.interidea.org/demo/icalendar.php#demohtml绑定控件 $("#startdate").icalendar({ ...
- Ubuntu firefox falsh
Ubuntu下为Firefox安装Adobe Flash Player 使用环境: OS:Ubuntu 12.04 LTS Browser: Firefox 12.0 Ad ...
- jqgrid使用sql row_number进行分页
背景 系统中使用了jqgrid的展示,现在要处理10w+的数据量 现状 使用了全查询的,查询到了10w+的数据放到了datatable中,每次页面刷新需要9秒多,并且传递给另一个dll来处理一些事情. ...
- acm-DP整理
一.背包 .各种01背包 void leastOne_Pack(int id, int num ) {//至少取一个: int i, j, c, v ; ; i <= num ; i ++ ) ...
- Java 8 中 CAS 的增强
几天前,我偶然地将之前写的用来测试AtomicInteger和synchronized的自增性能的代码跑了一下,意外地发现AtomicInteger的性能比synchronized更好了,经过一番原因 ...
- jquery foreach
<form id="input_iForm" action="${pageContext.request.contextPath}/transfer/input_s ...
- Intent简介
1 Intent概念 1.1 Intent的作用 指明Intent所要启动的对象 提供将要启动对象组件运行需要的数据 组件类型 启动方法 Activity startActivity(Intent i ...
- Runtime 10种用法
来源:haojingxue_iOS 链接:http://www.jianshu.com/p/3182646001d1 阅读了多篇运行时的文章,感觉都很不错,从几篇文章里面提取一些个人认为比较重要的,偏 ...
- centos6下安装部署hadoop2.2
环境准备1.操作系统:centos6.0 64位2.hadoop版本:hahadoop-2.2.0 安装和配置步骤具体如下:1.主机和ip分配如下 ip地址 ...
- systemd service
Man page systemd.unit SYSTEMD.UNIT(5) systemd.unit SYSTEMD.UNIT(5) NAME systemd.unit - Unit configur ...