题目大意:先确定一个M, 然后输入多组线段的左端和右端的端点坐标,然后让你求出来在所给的线段中能够

把[0, M] 区域完全覆盖完的最少需要的线段数,并输出这些线段的左右端点坐标。

思路分析:

线段区间的起点是0,那么找出所有区间起点小于0中的最合适的区间。

因为需要尽量少的区间,所以选择右端点更大的区间,它包含所选线段更大。

如果在所有区间中找到了解,且右端点小于M,则把找到的区间的右端点定为新的线段区间的起点。

 #include <iostream>
#include <stdio.h>
#include <cstring>
#include <cmath>
#include <algorithm> using namespace std; struct node
{
int L, R;
}a[], b[]; bool cmp(node a, node b)
{
return a.R > b.R;
} int main()
{
int M;
while(scanf("%d", &M) != EOF)
{
int Index = ;
while()
{
scanf("%d%d", &a[Index].L, &a[Index].R);
if(a[Index].L == && a[Index].R == )
break;
++Index;
} sort(a, a+Index, cmp); int border = ; // 起始边界点为0
int cnt = ;
while(border < M)
{
int i = ;
for(; i < Index; ++i)
{
// a[i].R >= border提交将会Runtime error
if(a[i].L <= border && a[i].R > border)
{
b[cnt] = a[i];
cnt++;
border = a[i].R; // 更新边界点
break;
}
}
if(i == Index)
break;
} if(border < M)
cout << "No solution" << endl;
else
{
cout << cnt << endl;
for(int i = ; i < cnt; ++i)
cout << b[i].L << " " << b[i].R << endl;
}
} return ;
}

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(贪心)

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

  3. ural 1303 Minimal Coverage(贪心)

    链接: http://acm.timus.ru/problem.aspx?space=1&num=1303 按照贪心的思想,每次找到覆盖要求区间左端点时,右端点最大的线段,然后把要求覆盖的区间 ...

  4. 贪心 URAL 1303 Minimal Coverage

    题目传送门 /* 题意:最少需要多少条线段能覆盖[0, m]的长度 贪心:首先忽略被其他线段完全覆盖的线段,因为选取更长的更优 接着就是从p=0开始,以p点为标志,选取 (node[i].l < ...

  5. ural 1303 Minimal Coverage【贪心】

    链接: http://acm.timus.ru/problem.aspx?space=1&num=1303 http://acm.hust.edu.cn/vjudge/contest/view ...

  6. uva 10020 Minimal coverage 【贪心】+【区间全然覆盖】

    Minimal coverage The Problem Given several segments of line (int the X axis) with coordinates [Li,Ri ...

  7. UVA-10020 Minimal coverage(贪心)

    题目大意:在x轴上,给一些区间,求出能把[0,m]完全覆盖的最少区间个数及该情形下的各个区间. 题目分析:简单的区间覆盖问题.可以按这样一种策略进行下去:在所有区间起点.长度有序的前提下,对于当前起点 ...

  8. 【区间覆盖问题】uva 10020 - Minimal coverage

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

  9. UVa 10020 - Minimal coverage(区间覆盖并贪心)

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

随机推荐

  1. Range的范围

    range(N,M)的范围应为: [N,M) 例如:

  2. 廖雪峰Java10加密与安全-2加密算法-2Base64编码

    1.Base64编码 Base64一种把二进制数据用文本表示的编码算法.例如 中有3个字节{\xe4, \xb8, \xad},一共是24位,每6位分组,变成4个字节{39, 0b, 22, 2d}, ...

  3. 巧用 position:absolute

    1.跟随性 下面这种方法更加简便以及更方便维护, 例如“西部世界”,由于不用将父元素设为position:relative,position:absolute的位置也就不用根据文字多少而重新进行top ...

  4. myeclipse 配置resin

    一.新建web project 二.配置本地resin 创建resin/conf/test.conf文件(可从resin.conf copy)中修改 <web-app id="/&qu ...

  5. PAT甲级——A1051 Pop Sequence

    Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., N and p ...

  6. BZOJ3339&&3585 Rmq Problem&&mex

    BZOJ3339&&3585:Rmq Problem&&mex Description 有一个长度为n的数组{a1,a2,...,an}.m次询问,每次询问一个区间内最 ...

  7. hdu6243

    hdu6243结论题,每个的概率是(n-1)/n,然后乘以总数n,结果就是 n-1 #include<iostream> #include<cstdio> #include&l ...

  8. Redis源码解析:11RDB持久化

    Redis的RDB持久化的相关功能主要是在src/rdb.c中实现的.RDB文件是具有一定编码格式的数据文件,因此src/rdb.c中大部分代码都是处理数据格式的问题. 一:RDB文件格式 上图就是一 ...

  9. stream的map用法

    List<String> list = new ArrayList<>();list.add("1");list.add("2");li ...

  10. LUOGU P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper

    题目描述 A little known fact about Bessie and friends is that they love stair climbing races. A better k ...