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].
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.
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,和一些区间[L,R]。。要选出几个区间能完全覆盖住[0,M]区间。要求数量最少。。如果不能覆盖输出0.

思路:贪心的思想。。把区间按R从大到小排序。 然后遇到一个满足的[Li,Ri],就更新缩小区间。。直到完全覆盖。

注意[L,R]只有满足L小于等于且R大于当前覆盖区间左端这个条件。才能选中。

贪心,把各区间按照R从大到小排序。如果区间1的起点不是s,无解,否则选择起点在s的最长区间。选择此区间[Li,Ri]后,新的起点设置为Ri,然后经过依次扫描之后就可以得出最小的线段数。并用另一个结构体储存路径。

#include <stdio.h>
#include <algorithm>
using namespace std;
int t;
int start,end,n,f;
struct qujian
{
int start;
int end;
};
qujian q[],p[];
int cmp (qujian a,qujian b)
{
return a.end > b.end;
}
int main()
{
scanf("%d", &t);
while (t --)
{
n = ;
f = ;
start = ;
scanf("%d", &end);
while (scanf("%d%d", &q[n].start, &q[n].end) && (q[n].start||q[n].end))
{
n++;
}
sort(q,q+n,cmp);
while(start<end)
{
int i;
for (i=;i<n;i++)
{
if (q[i].start <= start && q[i].end > start)
{
start = q[i].end; //更新区间
p[f] = q[i];
f++;
break;
}
}
if (i==n) break; //如果没有一个满足条件的区间,直接结束。
}
if(start<end) printf("0\n");
else
{
printf("%d\n",f);
for (int i=;i<f;i++)
printf("%d %d\n", p[i].start,p[i].end);
}
if (t) printf("\n");
}
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

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

  4. uva.10020 Minimal coverage(贪心)

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

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

    题意:有一块草坪,这块草坪长l 米,宽 w 米,草坪有一些喷头,每个喷头在横坐标为 p 处,每个喷头的纵坐标都是(w/2) ,并且喷头的洒水范围是一个以喷头为圆心,半径为 r 米的圆.每次最少需要打开 ...

  7. UVA 10382 Watering Grass (区间覆盖,贪心)

    问题可以转化为草坪的边界被完全覆盖.这样一个圆形就换成一条线段. 贪心,从中选尽量少的线段把区间覆盖,按照把线段按左端点排序,记录一个当前已经覆盖区间的位置cur, 从左端点小于等于cur选一个右端点 ...

  8. 外星人的供给站 (区间覆盖 t贪心)

    /** 区间覆盖问题 分析: 每个点可以确定两个圆心 圆心的范围形成 一个区间 在这个区间上以任意一点画圆便可将此点 包含在内 如果有两个点所确定的区间相交了 说明这两个点可以用一个圆包含在内 即用一 ...

  9. nyoj--12--喷水装置(二)(区间覆盖问题+贪心)

    喷水装置(二) 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描述 有一块草坪,横向长w,纵向长为h,在它的橫向中心线上不同位置处装有n(n<=10000)个点状的喷水 ...

随机推荐

  1. hdu4666 最远曼哈顿距离

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4666 #include <cstdio> #include <cstring> ...

  2. JavaScript 操作 DOM 常用 API 总结

    文本整理了javascript操作DOM的一些常用的api,根据其作用整理成为创建,修改,查询等多种类型的api,主要用于复习基础知识,加深对原生js的认识. 基本概念 在讲解操作DOM的api之前, ...

  3. 在javaweb中通过servlet类和普通类读取资源文件

    javaweb有两种方式读取资源文件 在Servlet中读取,可以使用servletContext,servletContext可以拿到web所有的资源文件,然后随便读,但是这种方法不常用,尽量少在S ...

  4. python list排序的两种方法及实例讲解

    对List进行排序,Python提供了两个方法方法1 用List的内建函数list sort进行排序list sort(func=None, key=None, reverse=False)Pytho ...

  5. AS3 读写 C++ 64位数字

    为框架添加了一套新的与C++通讯的数据协议,其中和C++的大爷们对于他们的64位数字(unsigned long long)读写的问题纠结了很久.真心觉得“学好C++走遍天下都不怕啊” AS里Numb ...

  6. CF 295E Yaroslav and Points(Splay)

    题目大意: 两个操作 1 id op  把id的位置+op 2 id op  查询在[id.op]之间的全部的数的差 思路: 关键是pushup函数. 自己退一下会发现.跟区间的总和,区间的节点个数有 ...

  7. Java基础知识强化之集合框架笔记09:Collection集合迭代器使用的问题探讨

    1.Collection集合迭代器使用的问题探讨: (1)问题1:能用while循环写这个程序,我能不能用for循环呢?                  可以使用for循环替代. (2)问题2:不要 ...

  8. Python开发【第九篇】:HTML (二)

    python[第十四篇]HTML基础 时间:2016-08-08 20:57:27      阅读:49      评论:0      收藏:0      [点我收藏+] 标签: 什么是HTML? H ...

  9. VI文件编辑操作说明

    vi Hello.c (回车后就进入,按i或a键开始编辑.要退出按ESC,进入中间模式,按冒号 :后面跟命令):wq (保存并退出):q!(退出不保存)

  10. 【原】AVAudio录制,播放 (解决真机播放音量太小)

    原文链接:http://www.cnblogs.com/A--G/p/4624526.html 最近学习AVFoundation里的audio操作,最基本的录制和播放,参考了一个Code4pp的 一个 ...