可以说是区间覆盖问题的例题...

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的更多相关文章

  1. UVA 10020 Minimal coverage(贪心 + 区间覆盖问题)

     Minimal coverage  The Problem Given several segments of line (int the X axis) with coordinates [Li, ...

  2. uva 10020 Minimal coverage 【贪心】+【区间全然覆盖】

    Minimal coverage The Problem Given several segments of line (int the X axis) with coordinates [Li,Ri ...

  3. uva.10020 Minimal coverage(贪心)

    10020 Given several segments of line (int the X axis) with coordinates [Li, Ri]. You are to choose t ...

  4. UVa 10020 - Minimal coverage(区间覆盖并贪心)

    Given several segments of line (int the X axis) with coordinates [Li, Ri]. You are to choose the min ...

  5. uva 10020 Minimal coverage

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  6. UVa 10020 (最小区间覆盖) Minimal coverage

    题意: 数轴上有n个闭区间[ai, bi],选择尽量少的区间覆盖一条指定线段[0, m] 算法: [start, end]为已经覆盖到的区间 这是一道贪心 把各个区间先按照左端点从小到大排序,更新st ...

  7. UVA10020:Minimal coverage(最小区间覆盖)

    题目: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=68990#problem/M 题目需求:数轴上有n个闭区间[ai,bi],选择尽量 ...

  8. Uva 10382 (区间覆盖) Watering Grass

    和 Uva 10020几乎是一样的,不过这里要把圆形区域转化为能够覆盖的长条形区域(一个小小的勾股定理) 学习一下别人的代码,练习使用STL的vector容器 这里有个小技巧,用一个微小量EPS来弥补 ...

  9. UVA 10382 - Watering Grass【贪心+区间覆盖问题+高精度】

    UVa 10382 - Watering Grass n sprinklers are installed in a horizontal strip of grass l meters long a ...

随机推荐

  1. java 实现死锁

    package 线程安全的讨论; class DThread implements Runnable { private Object o1=null; private Object o2=null; ...

  2. 五指cms筛选功能的实现:

    筛选功能的实现: $_POST['page_urlrule'] = 'tuan-{$pinpai}-{$renqun}-{$type}-{$price}-{$area}-{$tese}-{$st}-{ ...

  3. Solution for latex error:”Unknown graphics extension: .eps“ or "Can not find XXX"

    Sample code: \begin{figure*} \centering % Requires \usepackage{graphicx} \includegraphics[width=7in] ...

  4. HW6.19

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  5. StackOverflow:7个你从未见过的Java问题最佳答案

    本文由码农网 – 李俊英原创翻译,转载请看清文末的转载要求,欢迎参与我们的付费投稿计划! 对开发人员来说, StackOverflow就像一个金矿.对具体的问题,它能帮我们找到最有用的答案,并且我们也 ...

  6. [Objective-c 基础 - 2.5] NSString

    1.NSString基本使用 使用%@占位符输出对象 ; ; NSString *str2 = [NSString stringWithFormat:@"My age is %d and n ...

  7. Linux--用SecureCRT来上传和下载文件

    SecureCRT下的文件传输协议有以下几种:ASCII.Xmodem.Ymodem.Zmodem ASCII:这是最快的传输协议,但只能传送文本文件. Xmodem:这种古老的传输协议速度较慢,但由 ...

  8. Css基础-介绍及语法

    css 文件后缀.css 基础语法: selector { property:value } 例如: h1 {color:red;font-size:14px;} color:字体颜色  font-s ...

  9. 教你50招提升ASP.NET性能(二):移除不用的视图引擎

    (2)Remove unused View Engines 招数2: 移除不用的视图引擎 If you're an ASP.NET MVC developer, you might not know ...

  10. Nape的回调系统 nape.callbacks

    在Nape中增加一个回调大致分为三步 1:定义一些标签,并根据需求为不同的Interactor打上不同的标签 2:定义一个监听器,这个监听器定义了哪些标签触发了哪种行为之后做何种回调 3:为Space ...