Minimal coverage (贪心,最小覆盖)
题目大意:先确定一个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 (贪心,最小覆盖)的更多相关文章
- UVA 10020 Minimal coverage(贪心 + 区间覆盖问题)
Minimal coverage The Problem Given several segments of line (int the X axis) with coordinates [Li, ...
- uva.10020 Minimal coverage(贪心)
10020 Given several segments of line (int the X axis) with coordinates [Li, Ri]. You are to choose t ...
- ural 1303 Minimal Coverage(贪心)
链接: http://acm.timus.ru/problem.aspx?space=1&num=1303 按照贪心的思想,每次找到覆盖要求区间左端点时,右端点最大的线段,然后把要求覆盖的区间 ...
- 贪心 URAL 1303 Minimal Coverage
题目传送门 /* 题意:最少需要多少条线段能覆盖[0, m]的长度 贪心:首先忽略被其他线段完全覆盖的线段,因为选取更长的更优 接着就是从p=0开始,以p点为标志,选取 (node[i].l < ...
- ural 1303 Minimal Coverage【贪心】
链接: http://acm.timus.ru/problem.aspx?space=1&num=1303 http://acm.hust.edu.cn/vjudge/contest/view ...
- 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(贪心)
题目大意:在x轴上,给一些区间,求出能把[0,m]完全覆盖的最少区间个数及该情形下的各个区间. 题目分析:简单的区间覆盖问题.可以按这样一种策略进行下去:在所有区间起点.长度有序的前提下,对于当前起点 ...
- 【区间覆盖问题】uva 10020 - Minimal coverage
可以说是区间覆盖问题的例题... Note: 区间包含+排序扫描: 要求覆盖区间[s, t]; 1.把各区间按照Left从小到大排序,如果区间1的起点大于s,则无解(因为其他区间的左起点更大):否则选 ...
- UVa 10020 - Minimal coverage(区间覆盖并贪心)
Given several segments of line (int the X axis) with coordinates [Li, Ri]. You are to choose the min ...
随机推荐
- <每日一题>题目27:插入排序(假)
''' 插入排序:假设元素左侧全部有序,找到自己的位置插入 ''' import random import cProfile def insert_sort(nums): for i in rang ...
- 04-2-object类型
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- CodeForces - 752B
CodeForces - 752Bhttps://vjudge.net/problem/597648/origin简单模拟,主要是细节特殊情况多考虑一下,看代码就行 #include<iostr ...
- 分布式事务中间件 Fescar—RM 模块源码解读
前言 在SOA.微服务架构流行的年代,许多复杂业务上需要支持多资源占用场景,而在分布式系统中因为某个资源不足而导致其它资源占用回滚的系统设计一直是个难点.我所在的团队也遇到了这个问题,为解决这个问题上 ...
- 此处有加速 apt-get github docker pull
ubuntu get-apt 加速 创建 aptupdate.sh 脚本,内容为: #!/bin/bash mv /etc/apt/sources.list /etc/apt/sources.list ...
- 《数据结构与算法分析——C语言描述》ADT实现(NO.00) : 链表(Linked-List)
开始学习数据结构,使用的教材是机械工业出版社的<数据结构与算法分析——C语言描述>,计划将书中的ADT用C语言实现一遍,记录于此.下面是第一个最简单的结构——链表. 链表(Linked-L ...
- JasperReport查看和打印报告7
报表填充过程JasperPrint对象的输出可以使用内置的浏览器组件来查看,打印或导出到更多的流行的文件格式,如PDF,HTML,RTF,XLS,ODT,CSV或XML.Jasper文件查看和打印将包 ...
- 警告: [SetPropertiesRule]{Context/Loader} Setting property 'useSystemClassLoaderAsParent' to 'false' did not find a matching property.
警告: [SetPropertiesRule]{Context/Loader} Setting property 'useSystemClassLoaderAsParent' to 'false' d ...
- Hadoop 伪分布式安装配置
- ionic4环境搭建
1.下载安装node 下载地址:https://nodejs.org/en/ 安装后,win+R输入cmd,回车运行cmd命令窗口,输入下面的命令验证node是否安装成功: 如果出现上图所示,则证明n ...