Program 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
题意:给定一个M,和一些区间[Li,Ri],要选出几个区间能完全覆盖住[0,M]区间,要求数量最少,如果不能覆盖输出0.
思路:贪心的思想,把区间按Ri从大到小排序,然后遇到一个满足的[Li,Ri],就更新缩小区间,直到完全覆盖。
注意[Li,Ri]只有满足Li小于等于且Ri大于当前覆盖区间左端这个条件。才能选中。
代码如下:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
struct node{
int x,y;
}a[100001],b[100001];
int cmp(node a,node b)
{
return a.x<b.x;
}
int main()
{
int t;
int m,n,p,max,sum,i,f,pmax,begin,end;
scanf("%d",&t);
while(t--)
{
scanf("%d",&m);
n=0;
while(scanf("%d%d",&begin,&end))
{
if(begin==0&&end==0)
break;
if(end>=0)
{
a[n].x=begin;
a[n].y=end;
++n;
} }
sort(a,a+n,cmp);
if(a[0].x>0)
printf("0\n");
else
{
p=0;
max=0;
sum=1;
while(p+1<n&&a[p+1].x<=0)
{
++p;
if(a[p].y>a[max].y)
max=p;
}
p=max;
b[1].x=a[p].x;
b[1].y=a[p].y;
while(p<n&&b[sum].y<m)
{
f=1;
pmax=max;
while(p+1<n&&a[p+1].x<=a[max].y)
{
f=0;
++p;
if(a[p].y>a[pmax].y)
pmax=p;
}
if(f)
break;
max=pmax;
++sum;
b[sum].x=a[max].x;
b[sum].y=a[max].y; }
if(b[sum].y>=m)
{
printf("%d\n",sum);
for(i=1;i<=sum;i++)
printf("%d %d\n",b[i].x,b[i].y);
}
else
printf("0\n");
}
if(t)
printf("\n");
}
return 0;
}
Program 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的最小使用线段的数量. 题解 考虑一个这样的贪心: 先按照左端 ...
- 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)个点状的 ...
- 51nod 1091 线段的重叠【贪心/区间覆盖类】
1091 线段的重叠 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 收藏 关注 X轴上有N条线段,每条线段包括1个起点和终点.线段的重叠是这样来算的,[10 2 ...
- 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, ...
随机推荐
- Hadoop 基本操作
1.关闭安全模式 hadoop dfsadmin -safemode leave
- Hbase之必要时取出请求的行(列族所有数据)
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.CellScanner; import org. ...
- 推荐两篇Unity与Android交互的文章
http://www.xuanyusong.com/archives/676 里面18,19介绍
- rand()和srand()GetTickCount函数用法
标准库<cstdlib>(被包含于<iostream>中)提供两个帮助生成伪随机数的函数: 函数一:int rand(void):从srand (seed)中指定的seed开始 ...
- ARPG游戏技能系统设计
ARPG游戏的技能的前端表现, 主要指的是人物动作和特效表现, 从普遍意义上讲,大致可以分为三个阶段:起手.飞行.碰撞(爆炸). [特效可见性判断] 1.[attacker & victim] ...
- 【bzoj1029】道路抢修
[bzoj1029]建筑抢修 传送门 http://www.lydsy.com/JudgeOnline/problem.php?id=1029 分析 http://blog.csdn.net/popo ...
- Qt之可重入与线程安全
简述 本篇文章中,术语"可重入性"和"线程安全"被用来标记类与函数,以表明它们如何被应用在多线程应用程序中. 一个线程安全的函数可以同时被多个线程调用,甚至调用 ...
- java之多线程(Thread)
package DEMO; //主线程 public class Example12_2 { public static void main(String [] args ) { Thread myd ...
- OC 实例变量(instance var)与属性(@property)的关系 isa指针
实例变量(instance var)与属性(@property)的关系 Objective-C 2.0之后,声明一个@property name自动产生一个实例变量,名为_name,因此省去实例变量和 ...
- java.lang.ClassNotFoundException: org.apache.http.util.Args
java.lang.ClassNotFoundException: org.apache.http.util.Args at org.apache.catalina.loader.WebappClas ...