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. ZooKeeper集群安装

    ZooKeeper是Apache提供的.分布式服务协调系统,应用比較广泛. 由于项目中使用Kafka MQ,而Kafka全然使用ZooKeeper实现Kafka各组件的服务协调,包含Broker.Co ...

  2. Android (1) - Activity

    onCreate(Bundle status) --> setContentView(View view) --> findViewById(int id) Intent intentFo ...

  3. 软件測试系统文章(文件夹&amp;链接在此)

    前言 我会在此账号上写一系列关于软件測试的文章,故在此置顶软件測试系列文章的文件夹和链接,以方便大家阅读! 文件夹 软件測试系列之入门篇(一) 软件測试系列之了解篇(二) 软件測试系列之黑白盒(三) ...

  4. Oracle SQL Lesson (8) - 使用集合操作符(Union,Intersect,Minus)

    集合操作符UNION/UNION ALLINTERSECTMINUS Union All不排序,不去重,其余均升序且去重.create table e1 as select * from emp wh ...

  5. twisted是python实现的基于事件驱动的异步网络通信构架。

    网:https://twistedmatrix.com/trac/ http://www.cnblogs.com/wy-wangyan/p/5252271.html What is Twisted? ...

  6. 重新想象 Windows 8 Store Apps (14) - 控件 UI: RenderTransform, Projection, Clip, UseLayoutRounding

    原文:重新想象 Windows 8 Store Apps (14) - 控件 UI: RenderTransform, Projection, Clip, UseLayoutRounding [源码下 ...

  7. SQL Tuning Advisor一个错误ORA-00600: internal error code, arguments: [kesqsMakeBindValue:obj]

    跑SELECT dbms_sqltune.report_tuning_task(:tuning_task) FROM dual;  错误消息,如下面: ORA-00600: internal erro ...

  8. linux 下上传 datapoint数据到yeelink 修改版本

      /*client.c*/ #include <stdio.h> #include <stdlib.h> #include <string.h> #include ...

  9. 重新想象 Windows 8 Store Apps (26) - 选取器: 自定义文件选取窗口, 自定义文件保存窗口

    原文:重新想象 Windows 8 Store Apps (26) - 选取器: 自定义文件选取窗口, 自定义文件保存窗口 [源码下载] 重新想象 Windows 8 Store Apps (26) ...

  10. 设计模式——工厂模式(Factory)

    要想正确理解设计模式,首先必须明白它是为了解决什么问题而提出来的. 设计模式学习笔记 --Shulin 转载请注明出处:http://blog.csdn.net/zhshulin 1.概念 工厂模式定 ...