ural 1303 Minimal Coverage【贪心】
链接:
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d
& %I64u
Description
Input
line. Numbers in the pair are separated with space(s). List of pairs is ended with pair of zeroes. The list contains no more than 100000 pairs, including the pair of zeroes.
Output
in input with exception that ending pair of zeroes should not be printed. Segments should be printed in increasing order of their left end point coordinate.
Sample Input
input | output |
---|---|
1 |
No solution |
1 |
1 |
题意:
读入一个整数m(1<=m<=5000)。
接下来若干行,表示若干条线段。
从l[i]到r[i](最多100000条线段)。
到一行0 0为止。
求最少要用多少条线段可以覆盖区间[0,m]。
输出第一行一个数字,表示最少线段数。
接下来若干行表示选择的线段。按照顺序!!!注意:这里的按照顺序不是指线段输入的顺序而是每条线段的左右点的顺序
若无解则输出'No solution'。
算法: 贪心
思路:
code:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; const int maxn = 100000+10; struct Line{
int left,right;
}line[maxn], ans[maxn]; bool cmp(Line L1, Line L2) //从左到右排序,覆盖掉被其他线包含的线
{
if(L1.left != L2.left) return L1.left < L2.left;
else return L1.right >= L2.right;
} int main()
{
int m;
while(scanf("%d", &m) != EOF)
{
int left,right;
int n = 0;
for(;;)
{
scanf("%d%d", &left,&right);
if(left == 0 && right == 0) break;
line[++n].left = left;
line[n].right = right;
}
sort(line+1, line+n+1, cmp); int num = 1;
for(int i = 2; i <= n; i++) //覆盖过程
{
if(line[i].left > line[num].left && line[i].right > line[num].right)
line[++num] = line[i];
} n = num;
line[n+1].left = line[n+1].right = m+1; //边界处理
int cover = 0; //当前要求覆盖的点
int total = 0; //当前选中了的线
for(int i = 1; i <= n; i++)
{
if(line[i+1].left > cover && line[i].left <= cover)
{
ans[++total] = line[i];
cover = line[i].right;
if(cover >= m)
{
printf("%d\n", total);
for(int j = 1; j <= total; j++)
{
printf("%d %d\n", ans[j].left, ans[j].right);
}
return 0; //退出程序
}
}
}
printf("No solution\n");
}
return 0;
}
ural 1303 Minimal Coverage【贪心】的更多相关文章
- 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(贪心)
题目地址:Ural 1303 先按每一个线段的左端点排序,然后设置一个起点s.每次都从起点小于等于s的线段中找到一个右端点最大的. 并将该右端点作为新的起点s,然后继续找. 从左到右扫描一遍就可以. ...
- URAL 1303 Minimal Coverage
URAL 1303 思路: dp+贪心,然后记录路径 mx[i]表示从i开始最大可以到的位置 sufmx[i]表从1-i的某个位置开始最大可以到达的位置 比普通的贪心效率要高很多 代码: #inclu ...
- URAL 1303. Minimal Coverage(DP)
题目链接 又是输出路径...这题完全受上题影响,感觉两个题差不多..用了基本上一样的算法写了,这题比较纠结,就是卡内存啊...5000*5000的数组开不了..然后没办法,水了好几次MLE,看了一下虎 ...
- 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 ...
- Minimal coverage (贪心,最小覆盖)
题目大意:先确定一个M, 然后输入多组线段的左端和右端的端点坐标,然后让你求出来在所给的线段中能够 把[0, M] 区域完全覆盖完的最少需要的线段数,并输出这些线段的左右端点坐标. 思路分析: 线段区 ...
- uva 10020 Minimal coverage 【贪心】+【区间全然覆盖】
Minimal coverage The Problem Given several segments of line (int the X axis) with coordinates [Li,Ri ...
随机推荐
- 智能选择器和语义化的CSS
本文由白牙根据Heydon Pickering的<Semantic CSS With Intelligent Selectors>所译,整个译文带有我自己的理解与思想,如果译得不好或不对之 ...
- JS方面重点摘要(一)
1.获取样式(1)style只能获取到行间样式的属性(2)火狐getComputedStyle(obj,null)[attr],IE:obj.currentStyle[attr] 2.ready.on ...
- PHP防止sql注入-JS注入
一:为了网站数据安全,所有和数据库操作的相关参数必须做相关过滤,防止注入引起的网站中毒和数据泄漏 1.PHP自带效验函数 mysql_real_escape_string() 函数转义 SQL 语句中 ...
- 要做国外的app,使用到的分享和统计SDK推荐
国内的就不说了,多如牛毛,常用的是友盟,极光,shareSDK等等. 国外的统计有: Flurry(https://developer.yahoo.com) google analytics(http ...
- springboot 有用网址收集
http://www.ityouknow.com/spring-boot.html springboot多数据源配置: https://blog.csdn.net/neosmith/article/d ...
- UITextView 设置边框
UITextView * txtView = [[UITextView alloc] initWithFrame:CGRectMake(10, 50, 200, 50)]; txtVi ...
- js正则表达式的分组提取
- ASP.NET MVC深入浅出(被替换) 第一节: 结合EF的本地缓存属性来介绍【EF增删改操作】的几种形式 第三节: EF调用普通SQL语句的两类封装(ExecuteSqlCommand和SqlQuery ) 第四节: EF调用存储过程的通用写法和DBFirst模式子类调用的特有写法 第六节: EF高级属性(二) 之延迟加载、立即加载、显示加载(含导航属性) 第十节: EF的三种追踪
ASP.NET MVC深入浅出(被替换) 一. 谈情怀-ASP.NET体系 从事.Net开发以来,最先接触的Web开发框架是Asp.Net WebForm,该框架高度封装,为了隐藏Http的无状态 ...
- ASP.NET CORE RAZOR :向 Razor 页面应用添加模型
本文来自:https://docs.microsoft.com/zh-cn/aspnet/core/tutorials/razor-pages/model 在本部分中将添加用于管理数据库中的电影的类. ...
- 利用nginx搭建tomcat集群
1.tomcat集群 利用nginx对请求进行分流,将请求平均的分给不同的tomcat去处理,减少单个tomcat的负载量,提高tomcat的响应速度. 2.创建多个tomcat服务器(同一个服务器上 ...