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. 【第四篇章-android平台MediaCodec】推断是否支持硬件解码码

    public boolean isSupportMediaCodecHardDecoder(){ boolean isHardcode = false; //读取系统配置文件/system/etc/m ...

  2. M3U8格式解说及实际应用分析

    M3U8有啥优点 ? 网上搜索了一下,大家众说纷纭,个人理解主要是能够做多码率的适配,依据网络带宽,client会选择一个适合自己码率的文件进行播放,保证视频流的流畅. 在IOS device和mac ...

  3. C语言 - printf的占位符(%) 异常

    printf的占位符(%) 异常 本文地址: http://blog.csdn.net/caroline_wendy/article/details/26719135 C语言中, 使用%代表占位符的意 ...

  4. swift学习笔记(六)析关闭过程和使用分配给属性的默认值

    一.通过关闭和功能的默认实现财产值 当存储属性默认值需要定制,能为客户提供通过关闭或全局函数的自定义默认值. 注意:全局函数的结构,和枚举使用keywordstatic大喊    用classkeyw ...

  5. tarjan算法(割点/割边/点连通分量/边连通分量/强连通分量)

    tarjan算法是在dfs生成一颗dfs树的时候按照访问顺序的先后,为每个结点分配一个时间戳,然后再用low[u]表示结点能访问到的最小时间戳 以上的各种应用都是在此拓展而来的. 割点:如果一个图去掉 ...

  6. 经常使用的js小方法

    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <% Strin ...

  7. 基于KMP与Levenshtein模糊匹配算法的银行联行号查询(转)

    在人民银行那里,每个银行的每一个营业网点都有自己唯一的银行联行号,根据这个号码能快速定位一间银行具体的分支行,就像根据一个身份证号码能快速确定一个人一样.例如汇款时,汇款单上要求填写收款人开户行,然后 ...

  8. .Net下一个Winform方案可以让MessageBox.Show它显示在父窗口的中间

    下面的文字,缺省值是在屏幕中间显示. DialogResult dr = MessageBox.Show("是否要删除此数据?", "删除确认", Messag ...

  9. ORA-12012: error on auto execute of job &quot;ORACLE_OCM

    ALERT日志中报错例如以下: Sun Mar 30 06:05:40 2014 Errors in file /oracle/app/oracle/diag/rdbms/zscims/zscims1 ...

  10. Linux常见命令整理(一)

    整理一下,以备后用 cd /home 进入/home文件夹 cd .. 返回上一级文件夹  cd ../.. 返回上两级文件夹  cd 进入个人的主文件夹  cd - 返回上次所在的文件夹 pwd 显 ...