UVA-10020 Minimal coverage(贪心)
题目大意:在x轴上,给一些区间,求出能把[0,m]完全覆盖的最少区间个数及该情形下的各个区间。
题目分析:简单的区间覆盖问题。可以按这样一种策略进行下去:在所有区间起点、长度有序的前提下,对于当前起点,找到可以覆盖下去的最长区间进行覆盖,并不断更新起点,直到覆盖完所有区间。
代码如下:
# include<iostream>
# include<cstdio>
# include<vector>
# include<cstring>
# include<algorithm>
using namespace std;
struct QuJian
{
int l,r;
QuJian(){}
QuJian(int _l,int _r):l(_l),r(_r){}
bool operator < (const QuJian &a) const {///先按起点位置,再按区间长短排序
if(l==a.l)
return r>a.r;
return l<a.l;
}
};
vector<QuJian>P,q;
void init()
{///扔掉无意义的区间(属于其他区间的区间)
sort(q.begin(),q.end());
vector<QuJian>::iterator it,it1;
for(it=q.begin();it!=q.end();++it){
it1=(++it),--it;
while(it1!=q.end()){
if(it1->l>=it->l&&it1->r<=it->r)
q.erase(it1);
else
break;
}
}
}
void solve(int &r,const int &m)
{
int len=q.size();
for(int i=0;i<len&&r<m;++i){///漏写“r<m”,导致WA了一上午。。。
if(q[i].l>r)///无法进行下去,覆盖失败
break;
if(i+1<len&&q[i+1].l<=r&&q[i+1].r>=q[i].r)///在区间可以覆盖的前提下,寻找长度最长的区间
continue;
r=q[i].r;
P.push_back(q[i]);
}
}
void print(int &r,const int &m)
{
if(r<m)
printf("0\n");
else{
printf("%d\n",P.size());
for(int i=0;i<P.size();++i)
printf("%d %d\n",P[i].l,P[i].r);
}
}
int main()
{
//freopen("UVA-10020 Minimal coverage.txt","r",stdin);
int T,m,a,b;
scanf("%d",&T);
while(T--)
{
q.clear();
P.clear();
scanf("%d",&m);
while(scanf("%d%d",&a,&b)&&(a+b))
{///选择有价值的区间
if(a>b)
swap(a,b);
if(a>m||b<0)
continue;
q.push_back(QuJian(a,b));
}
init();
int r=0;
solve(r,m);
print(r,m);
if(T)
printf("\n");
}
return 0;
}
UVA-10020 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 ...
- 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
可以说是区间覆盖问题的例题... 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 ...
- uva 10020 Minimal coverage
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- ural 1303 Minimal Coverage(贪心)
链接: http://acm.timus.ru/problem.aspx?space=1&num=1303 按照贪心的思想,每次找到覆盖要求区间左端点时,右端点最大的线段,然后把要求覆盖的区间 ...
- Minimal coverage (贪心,最小覆盖)
题目大意:先确定一个M, 然后输入多组线段的左端和右端的端点坐标,然后让你求出来在所给的线段中能够 把[0, M] 区域完全覆盖完的最少需要的线段数,并输出这些线段的左右端点坐标. 思路分析: 线段区 ...
- 贪心 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 ...
随机推荐
- 计算概论(A)/基础编程练习1(8题)/5:鸡兔同笼
#include<stdio.h> int main() { // 鸡兔同笼中脚的总数:a < 32768 int a; scanf("%d", &a); ...
- 20145322 Exp5 利用nmap扫描
20145322 Exp5 利用nmap扫描 实验过程 使用命令创建一个msf所需的数据库 service postgresql start msfdb start 使用命令msfconsole开启m ...
- 六角填数|2014年蓝桥杯B组题解析第七题-fishers
六角填数 如图所示六角形中,填入1~12的数字. 使得每条直线上的数字之和都相同. 图中,已经替你填好了3个数字,请你计算星号位置所代表的数字是多少? 请通过浏览器提交答案,不要填写多余的内容. 思路 ...
- 【lrzsz 】Linux安装上传下载功能
1.在线安装服务 # yum -y install lrzsz 2.上传 rz 3.下载 sz
- attr返回被选元素的属性值
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- oadrunner11录制手机app脚本
oadrunner11录制手机app视频:http://pan.baidu.com/s/1bnc4cHL 注意点: 1.手机和loadrunner安装的电脑必须在同一网段2.视频的www.baidu. ...
- codeforces 251 div2 C. Devu and Partitioning of the Array 模拟
C. Devu and Partitioning of the Array time limit per test 1 second memory limit per test 256 megabyt ...
- editplus5激活码
editplus5激活码 name: Vovan code: 3AG46-JJ48E-CEACC-8E6EW-ECUAW 转自:https://blog.csdn.net/webfront/artic ...
- 利用Chrome的Heap Snapshot功能分析一个时间段内的内存占用率
在下图测试代码第13行和第16行设断点. 以调试方式运行,首先断点在第13行处触发: 打开Chrome开发者工具,点击Profiles tab, 再点击按钮"Take Snapshot&qu ...
- Codeforces 893E - Counting Arrays
893E - Counting Arrays 思路:质因子分解. 对于每个质因子,假设它有k个,那么求把它分配到y个数上的方案数. 相当于把k个小球分配到y个盒子里的方案数. 这个问题可以用隔板法(插 ...