高效算法——D 贪心,区间覆盖问题
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
解题思路:
1、把各区间按照x从小到大排序,如果区间1的起点大于0,则无解(因为其他区间的左起点更大);否则选择起点在s的最长区间;
2、选择区间[li, ri]后,新的起点应更新为ri,并且忽略所有区间在ri之前的部分;
程序代码:
#include <cstdio>
#include <algorithm>
using namespace std;
struct node
{
int x,y;
};
node a[];
int b[];
int cmp( node a, node b)
{
return a.x<b.x;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int m,i=;
scanf("%d",&m);
while(scanf("%d%d",&a[i].x,&a[i].y)==&&(a[i].x||a[i].y))
{
i++;
}
sort(a,a+i,cmp);
int k=,x=,y=;
int j;
while(x<m)
{
y=x;
for( j=;j<i;j++)
if(a[j].x<=x&&a[j].y>=y)
{y=a[j].y;b[k]=j;}
if(x==y)
{
k=;break;
}
x=y;
k++;
}
printf("%d\n",k);
for( j=;j<k;j++)
printf("%d %d\n",a[b[j]].x,a[b[j]].y);
}
return ;
}
高效算法——D 贪心,区间覆盖问题的更多相关文章
- 高效算法——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的最小使用线段的数量. 题解 考虑一个这样的贪心: 先按照左端 ...
- 51nod 1091 线段的重叠【贪心/区间覆盖类】
1091 线段的重叠 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 收藏 关注 X轴上有N条线段,每条线段包括1个起点和终点.线段的重叠是这样来算的,[10 2 ...
- UVA 10382 - Watering Grass【贪心+区间覆盖问题+高精度】
UVa 10382 - Watering Grass n sprinklers are installed in a horizontal strip of grass l meters long a ...
- 南阳OJ-12-喷水装置(二)贪心+区间覆盖
题目链接: http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=12 题目大意: 有一块草坪,横向长w,纵向长为h,在它的橫向中心线上不同位置处装有 ...
- nyoj 12——喷水装置二——————【贪心-区间覆盖】
喷水装置(二) 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 有一块草坪,横向长w,纵向长为h,在它的橫向中心线上不同位置处装有n(n<=10000)个点状的 ...
- UVA 10382 Watering Grass 贪心+区间覆盖问题
n sprinklers are installed in a horizontal strip of grass l meters long and w meters wide. Each spri ...
- B. Heaters 思维题 贪心 区间覆盖
B. Heaters 这个题目虽然只有1500的分数,但是我还是感觉挺思维的,我今天没有写出来,然后看了一下题解 很少做这种区间覆盖的题目,也不是很擅长,接下来讲讲我看完题解后的思路. 题目大意是:给 ...
- UVA 10020 Minimal coverage(贪心 + 区间覆盖问题)
Minimal coverage The Problem Given several segments of line (int the X axis) with coordinates [Li, ...
随机推荐
- EntityFramework 中生成的类加注释
EF5在生成实体类时获取不到数据库中表的说明字段,需要使用单独的t4模板来获取 下载文件 将文件与edmx 放同一文件夹 1.在生成类的t4模板中加入 <#@ include file=&quo ...
- jquery实现很简单的DIV拖动
今天用jquery实现了一个很简单的拖动...实现思路很简单 如下: 在thickbox 弹出层内实现拖拽DIV,那么得进行一下相对宽高的运算:必须加上相对于可见窗口宽高和弹出层宽高之间的差: ...
- asp.net模态窗口返回值
个人感觉模态窗口在做网站的时候,使用到的比较少,前段时间在做项目时要实现以模态窗口传值和接收返回值, 模态窗口传值实现比较简单,但是做好后发现在Chrome浏览器中接收不到返回值,修改好Chrome浏 ...
- ashx页面 “检测到有潜在危险的 Request.Form 值”的解决方法(控制单个处理程序不检测html标签)
如题: 使用web.config的configuration/location节点. 在configuration节点内新建一个location节点,注意这个节点和system.webserver那些 ...
- 解决UITabeleViewCell的分割线不能铺满问题
-(void)viewDidLayoutSubviews { if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)] ...
- corejava_chap02
//单行注释 --不能用在一行代码的中间/**/多行注释 --任何地方/** */文档注释 文档注释用在:package.class.member variables.member method. ...
- VS 编程常见错误及解决方法
1. VS2013 无法打开包括文件:“cv.h"等一些头文件 解决方法: cv.h是build\include文件夹下的头文件,所在文件夹位置是D:\Program Files (x86) ...
- Linux内存点滴:用户进程内存空间
原文出处:PerfGeeks 经常使用top命令了解进程信息,其中包括内存方面的信息.命令top帮助文档是这么解释各个字段的.VIRT , Virtual Image (kb)RES, Residen ...
- Apache配置完虚拟主机后,使用Chrome访问localhost还是默认目录htdocs
Chrome 解析DNS出错,这个错误比较罕见,甚至说有点奇特.今天在使用Apache配置虚拟主机时,出现了一个非常奇怪的现象.我按照配置的步骤配置虚拟主机,如下 配置虚拟主机的步骤如下: 1. 启用 ...
- phpexcel 一些基本的设置 (表格的基本属性)
网址是:http://www.thinkphp.cn/code/1893.html