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

题意:用最少的区间覆盖(0,m)这个区间。

分析:尽量选覆盖目的区间大的区间。我们能够依照每一个给的区间的最左端端点排序,从左端点小于st的区间中选取右端点最大的区间赋给en。这时候再比較st=en与m的大小,不满足k>=m,继续上面的循环,直到满足,或者找不到能够覆盖的区间。 详情看代码。

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#define M 100005
using namespace std;
struct node{
int st, en;
}s[M];
int ans[M]; int cmp(node a, node b){
if(a.st == b.st) return a.en>b.en;
return a.st < b.st;
}
int main(){
int t, m;
scanf("%d", &t);
while(t --){
scanf("%d", &m);
int tot = 0, a, b;
while(scanf("%d%d", &a, &b), a||b){
if(a>b) swap(a, b);
s[tot].st = a; s[tot].en = b;
++tot;
}
sort(s, s+tot, cmp);
int st, en, num;
st = en = num = 0;
while(st<m){
en = st;
for(int i = 0; i < tot; i ++){
if(s[i].st<=st&&s[i].en>en){ //从左端点小于st的区间中找出右端点最大的赋给en,而且用ans【num】储存右端点最大的区间的下标
en = s[i].en; ans[num] = i;
}
}
if(en == st){ //假设没有找到,能够继续覆盖的区间,直接跳出来。
num = 0; break;
}
st = en;
++num;
}
cout<<num<<endl;
for(int i = 0; i < num; i++)
cout<<s[ans[i]].st<<" "<<s[ans[i]].en<<endl;
}
exit(0);
}

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(区间覆盖并贪心)

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

  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

    可以说是区间覆盖问题的例题... Note: 区间包含+排序扫描: 要求覆盖区间[s, t]; 1.把各区间按照Left从小到大排序,如果区间1的起点大于s,则无解(因为其他区间的左起点更大):否则选 ...

  5. uva 10020 Minimal coverage

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

  6. UVA 10382 Watering Grass 贪心+区间覆盖问题

    n sprinklers are installed in a horizontal strip of grass l meters long and w meters wide. Each spri ...

  7. ural 1303 Minimal Coverage(贪心)

    链接: http://acm.timus.ru/problem.aspx?space=1&num=1303 按照贪心的思想,每次找到覆盖要求区间左端点时,右端点最大的线段,然后把要求覆盖的区间 ...

  8. UVA - 1615 Highway(贪心-区间选点问题)

    题目: 给定平面上n(n≤105)个点和一个值D,要求在x轴上选出尽量少的点,使得对于给定的每个点,都有一个选出的点离它的欧几里得距离不超过D. 思路: 先自己造区间,然后贪心选点就可以了.之前做过一 ...

  9. Minimal coverage (贪心,最小覆盖)

    题目大意:先确定一个M, 然后输入多组线段的左端和右端的端点坐标,然后让你求出来在所给的线段中能够 把[0, M] 区域完全覆盖完的最少需要的线段数,并输出这些线段的左右端点坐标. 思路分析: 线段区 ...

随机推荐

  1. 讨论asp.net通过机器cookie仿百度(google)实现搜索input搜索提示弹出框自己主动

    为实现自己主动弹出通过用户输入关键词相关的搜索结果,在这里,我举两个解决方案,对于两个不同的方案. 常用的方法是建立一个用户数据库中查找关系表.然后输入用户搜索框keyword异步调用数据表中的相关数 ...

  2. 《Javascript高级程序设计》读书笔记之bind函数详解

    为什么需要bind var name = "The Window"; var object = { name: "My Object", getNameFunc ...

  3. LAMBDA表达式常用 (全)

    这里主要是将数据库中的常用操作用LAMBDA表达式重新表示了下,用法不多,但相对较常用,等有时间了还会扩展,并将查询语句及LINQ到时也一并重新整理下: 1.select语句:books.Select ...

  4. Vs2012在Linux应用程序开发(3):加入新平台hi3516

    下面我们将VS2012添加一个新的平台支持,由于近来与哈斯hi3516.就选它吧! 1.1     复制平台文件 原来一直认为要让VS支持一个新的平台须要编写代码,某天在看MSBUILD文件夹的时候突 ...

  5. 行人检测(Pedestrian Detection)资源整合

    一.纸 评论文章分类: [1] D. Geronimo, and A. M.Lopez. Vision-based Pedestrian Protection Systems for Intellig ...

  6. 二十7天 春雨滋润着无形 —Spring依赖注入

    6月11日,明确."夏条绿已密,朱萼缀明鲜.炎炎日正午,灼灼火俱燃." IT人习惯把详细的事物加工成的形状一致的类.正是这种一致,加上合适的规范.才干彰显对象筋道的牙感和bean清 ...

  7. 【ThinkingInC++】61、非成员运算符

    非成员运算符 当操作者的左侧是不同的类时.运算符重载不可能是正确的类中. IostreamOperatorOverloading.cpp /** * 书本:[ThinkingInC++] * 功能:非 ...

  8. CentOS6.5解压缩文件.tar.gz .war .zip

    拉开拉链.tar.gz文件: tar -zxvf web.tar.gz tar将文件解压缩到一个指定的文件夹. 拉开拉链.war .zip文件到指定的文件夹: unzip web.war -d web ...

  9. 工作经常使用的SQL整理,实战篇(三)

    原文:工作经常使用的SQL整理,实战篇(三) 工作经常使用的SQL整理,实战篇,地址一览: 工作经常使用的SQL整理,实战篇(一) 工作经常使用的SQL整理,实战篇(二) 工作经常使用的SQL整理,实 ...

  10. Google的Guava它Collection升华

    至于Guava这是不是在这里说.一个已被提上一个非常特殊的! 这主要是为了分享Guava对于一些升华处理组.井,不多说了,直接在代码: package com.joyce.guava.bean; /* ...