Ural 1303 Minimal Coverage(贪心)
题目地址:Ural 1303
先按每一个线段的左端点排序,然后设置一个起点s。每次都从起点小于等于s的线段中找到一个右端点最大的。
并将该右端点作为新的起点s,然后继续找。
从左到右扫描一遍就可以。
代码例如以下:
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include <set>
#include <algorithm> using namespace std;
#define LL __int64
const int INF=0x3f3f3f3f;
int a[100000];
struct node
{
int l, r, ll, rr;
} fei[100000];
int cmp(node x, node y)
{
return x.l<y.l;
}
int main()
{
int m, i, j, n, l, r, s, max1, cnt=0, tot, pos, flag, flag1, ll, rr;
scanf("%d",&m);
while(scanf("%d%d",&ll, &rr)!=EOF&&(ll||rr))
{
if(rr<=0||ll>=m) continue ;
fei[cnt].ll=ll;
fei[cnt].rr=rr;
l=ll;r=rr;
if(l<0)
l=0;
if(r>m)
r=m;
fei[cnt].l=l;
fei[cnt++].r=r;
}
sort(fei,fei+cnt,cmp);
s=0;
tot=0;
if(cnt==0)
{
printf("No solution\n");
}
else
{
for(i=0; i<cnt;)
{
max1=-1;
flag=0;
while(fei[i].l<=s&&i<cnt)
{
flag=1;
if(max1<fei[i].r)
{
max1=fei[i].r;
pos=i;
}
i++;
}
if(!flag)
break;
if(s<max1)
{
a[tot++]=pos;
s=max1;
}
}
if(i<cnt||s<m)
puts("No solution");
else
{
printf("%d\n",tot);
for(i=0; i<tot; i++)
{
printf("%d %d\n",fei[a[i]].ll,fei[a[i]].rr);
}
}
}
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【贪心】
链接: http://acm.timus.ru/problem.aspx?space=1&num=1303 http://acm.hust.edu.cn/vjudge/contest/view ...
- 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 ...
随机推荐
- 前端换行显示,后端返回<br>
- URL正则
现有需求 表单填写域名只能填写 baseURL 或者 baseURL+端口 不带协议 否则为不合法 String url1 = ".com:90"; String url2 = & ...
- 线段树合并&&启发式合并笔记
这俩东西听起来很高端,实际上很好写,应用也很多~ 线段树合并 线段树合并,顾名思义,就是建立一棵新的线段树保存原有的两颗线段树的信息. 考虑如何合并,对于一个结点,如果两颗线段树都有此位置的结点,则直 ...
- vue之父子组件间通信实例讲解(props、$ref、$emit)
组件间如何通信,也就成为了vue中重点知识了.这篇文章将会通过props.$ref和 $emit 这几个知识点,来讲解如何实现父子组件间通信. 组件是 vue.js 最强大的功能之一,而组件实例 ...
- Django学习之配置篇
MTV Model Template View 数据库 模版文件 业务处理 了解Django框架,功能齐全 一.安装Django&Django基本配置 安装Django pip3 django ...
- 【codeforces 589G】Hiring
[题目链接]:http://codeforces.com/problemset/problem/589/G [题意] 有n个人; 每个人每天在开始工作之前,都需要di单位的准备时间,然后才能开始工作; ...
- Testing for SSL renegotiation
https://blog.ivanristic.com/2009/12/testing-for-ssl-renegotiation.html
- hadoop-15-Ambari进行HDP、zookeeper安装
hadoop-15-Ambari进行HDP.zookeeper安装 1,登陆server_1:8080 admin/admin 2,命名:Hdp_cluster 3,输入HDP.HDP_Utils 地 ...
- 8.6 First_value和Last_value
8.6 First_value和Last_value正在更新内容.请稍后
- poj_3667线段树区间合并
对照着notonlysuccess大牛的代码写的 #include<iostream> #include<cstdio> #include<cstring> #in ...