UVA 10020 Minimal coverage(贪心 + 区间覆盖问题)
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
题意:给定一个M,和一些区间[Li,Ri]。。要选出几个区间能完全覆盖住[0,M]区间。要求数量最少。。如果不能覆盖输出0.
思路:贪心的思想。。把区间按Ri从大到小排序。 然后遇到一个满足的[Li,Ri],就更新缩小区间。。直到完全覆盖。
注意[Li,Ri]只有满足Li小于等于且Ri大于当前覆盖区间左端这个条件。才能选中。
代码:
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; int t;
int start, end, qn, outn;
struct M {
int start;
int end;
} q[100005], out[100005]; int cmp (M a, M b) {//按最大能覆盖到排序
return a.end > b.end;
}
int main() {
scanf("%d", &t);
while (t --) {
qn = 0; outn = 0; start = 0;
scanf("%d", &end);
while (~scanf("%d%d", &q[qn].start, &q[qn].end) && q[qn].start + q[qn].end) {
qn ++;
}
sort(q, q + qn, cmp);
while (start < end) {
int i;
for (i = 0; i < qn; i ++) {
if (q[i].start <= start && q[i].end > start) {
start = q[i].end;//更新区间
out[outn ++] = q[i];
break;
}
}
if (i == qn) break;//如果没有一个满足条件的区间,直接结束。
}
if (start < end) printf("0\n");
else {
printf("%d\n", outn);
for (int i = 0; i < outn; i ++)
printf("%d %d\n", out[i].start, out[i].end);
}
if (t) printf("\n");
}
return 0;
}
UVA 10020 Minimal coverage(贪心 + 区间覆盖问题)的更多相关文章
- 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(贪心)
10020 Given several segments of line (int the X axis) with coordinates [Li, Ri]. You are to choose t ...
- 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
可以说是区间覆盖问题的例题... Note: 区间包含+排序扫描: 要求覆盖区间[s, t]; 1.把各区间按照Left从小到大排序,如果区间1的起点大于s,则无解(因为其他区间的左起点更大):否则选 ...
- UVA 10382 Watering Grass 贪心+区间覆盖问题
n sprinklers are installed in a horizontal strip of grass l meters long and w meters wide. Each spri ...
- uva 10020 Minimal coverage
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- 高效算法——E - 贪心-- 区间覆盖
E - 贪心-- 区间覆盖 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=85904#problem/E 解题思路: 贪心思想, ...
- 【题解】Cut the Sequence(贪心区间覆盖)
[题解]Cut the Sequence(贪心区间覆盖) POJ - 3017 题意: 给定一大堆线段,问用这些线段覆盖一个连续区间1-x的最小使用线段的数量. 题解 考虑一个这样的贪心: 先按照左端 ...
- UVA 10382 - Watering Grass【贪心+区间覆盖问题+高精度】
UVa 10382 - Watering Grass n sprinklers are installed in a horizontal strip of grass l meters long a ...
随机推荐
- JavaEE Tutorials (10) - Java持久化查询语言
10.1查询语言术语14010.2使用Java持久化查询语言创建查询141 10.2.1查询中的命名参数142 10.2.2查询中的位置参数14210.3简化的查询语言语法142 10.3.1选择语句 ...
- android基础篇------------java基础(12)(多线程操作)
<一>基本概念理解 1.什么是进程? 进程就是在某种程度上相互隔离,独立运行的程序.一般来说,系统都是支持多进程操作的,这所谓的多进程就是让系统好像同时运行多个程序. 2.什么是线程呢? ...
- c#关于EXCEL导出数据库的做法
using System;using System.Diagnostics;using System.Collections;using System.Data;using System.Web;us ...
- 【ActiveMQ】设置自动重连
<property name="brokerURL" value="tcp://localhost:61616"/> <property na ...
- What day is that day?(快速幂,打表找周期,或者求通项公式)
有些题怎么都解不出来,这时候可以打表,找规律,求通项公式等,这些方法让人拍手叫绝,真不错…… Description It's Saturday today, what day is it after ...
- linux分区工具fdisk的使用
fdisk是linux下的一块分区工具,使用简单方便,由于是对系统进行修改,需要root权限. 常用参数如下: fdisk -l : 列出所有的硬盘信息 直接传入设备名称可进入对该硬盘分区.例如,f ...
- 在Visual Studio 2010中安装iGraph
本安装步骤参考了:http://www.16kan.com/question/detail/182225.html Igraph actually does work with Visual C++ ...
- 工程脚本插件方案 - c集成Python基础篇(VC++嵌入Python)
序: 为什么要集成脚本,怎么在工程中集成Python脚本. 在做比较大型的工程时,一般都会分核心层和业务层.核心层要求实现高效和稳定的基础功能,并提供调用接口供业务层调用的一种标准的框架划分.在实际中 ...
- 新发现的mbstowcs, mbstowcs_s函数,转换多字节到宽字符
http://en.cppreference.com/w/c/string/multibyte/mbstowcs https://msdn.microsoft.com/fr-fr/library/ey ...
- Android EditText 无法换行
问题 关于控制是否换行的属性android:singleLine 当值为true的时候,只能一行,不换行 当值为false的时候,可以换行 但是现在遇到一个问题: <EditText andro ...