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 ...
随机推荐
- 【深度学习】CNN 中 1x1 卷积核的作用
[深度学习]CNN 中 1x1 卷积核的作用 最近研究 GoogLeNet 和 VGG 神经网络结构的时候,都看见了它们在某些层有采取 1x1 作为卷积核,起初的时候,对这个做法很是迷惑,这是因为之前 ...
- _itoa _itow _itot atoi atof atol
函数原型: char *_itoa( int value, char *string, int radix ); //ANSI wchar_t * _itow( int value, wchar_t ...
- 解决git push至远程仓库失败的问题
产生问题的原因: 远程仓库存在本地不存在的文件, 一个常见的例子是创建repository时勾选了README.md, 但此时本地还没有这个文件, 就会导致本地文件无法同步到远程仓库的问题. 解决方法 ...
- 【NOIP2016提高A组集训第14场11.12】随机游走——期望+树形DP
好久没有写过题解了--现在感觉以前的题解弱爆了,还有这么多访问量-- 没有考虑别人的感受,没有放描述.代码,题解也写得歪歪扭扭. 并且我要强烈谴责某些写题解的代码不打注释的人,像天书那样,不是写给普通 ...
- bzoj1003物流运输 最短路+DP
bzoj1003物流运输 题目描述 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的运输路线,以便对整个运输 ...
- day65作业
有 红.黄.蓝 三个按钮,以及一个200x200矩形框box,点击不同的按钮,box的颜色会被切换为指定的颜色 <body> <div id="app"> ...
- 在Spring框架中获取连接池的四种方式
1:DBCP数据源 DBCP类包位于 <SPRING_HOME>/lib/jakarta-commons/commons-dbcp.jar,DBCP是一个依赖Jakarta commons ...
- (转载)关于My97 datepicker与Angular ng-model绑定问题解决。
转载自 http://zerosoft.blog.51cto.com/679447/1611403 <input type="text" ng-model="d&q ...
- Linux 命令之head, tail, tr, sort, uniq, grep
head [filename] head -n 11 [filename] -> First 11 lines head -c 20 [filename] -> First 20 char ...
- PHP+Ajax点击加载更多内容 -这个效果好,速度快,只能点击更多加载,不能滚动自动加载
这个效果好,速度快,只能点击更多加载,不能滚动自动加载 一.HTML部分 <div id="more"> <div class="single_item ...