题目: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=68990#problem/M

题目需求:数轴上有n个闭区间[ai,bi],选择尽量少的区间覆盖一条指定的线段[0,m]。

题目解析:没什么好说的,就是贪心,具体看代码。

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <queue>
#define inf 0x3f3f3f3f
#define eps 1e-9
typedef long long ll;
using namespace std;
struct node
{
int l,r;
}q[];
int n,tt,s,e,key;
int p[][];
int cmp(const void *a,const void *b)
{
struct node *aa=(struct node *)a;
struct node *bb=(struct node *)b;
if(aa->l!=bb->l)
return aa->l-bb->l;
else return aa->r-bb->r;
}
bool ff;
int main()
{
int T,sum,xx,yy;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
tt=;
s=,e=n;
sum=;
while(scanf("%d%d",&xx,&yy)!=EOF)
{
if(xx==&&yy==) break;
if(xx<=&&yy<=) continue;
if(xx<) xx=;
if(yy>n) yy=n;
q[tt].l=xx;
q[tt++].r=yy;
}
qsort(q,tt,sizeof(q[]),cmp);
int maxx=-inf;
while(s<e)
{
ff=false;
maxx=-inf;
for(int i=;i<tt;i++)
{
if(q[i].l<=s&&maxx<q[i].r)
{
key=i;
maxx=q[i].r;
ff=true;
}
else if(q[i].l>s)
{
break;
}
}
if(!ff) break;
s=q[key].r;
p[sum][]=q[key].l;
p[sum++][]=q[key].r;
}
if(!ff) printf("0\n");
else
{
printf("%d\n",sum);
for(int i=;i<sum;i++)
printf("%d %d\n",p[i][],p[i][]);
}
if(T!=) cout<<endl;
}
return ;
}

UVA10020:Minimal coverage(最小区间覆盖)的更多相关文章

  1. UVA 10020 Minimal coverage(贪心 + 区间覆盖问题)

     Minimal coverage  The Problem Given several segments of line (int the X axis) with coordinates [Li, ...

  2. UVa 10020 - Minimal coverage(区间覆盖并贪心)

    Given several segments of line (int the X axis) with coordinates [Li, Ri]. You are to choose the min ...

  3. UVA-10020 Minimal coverage(贪心)

    题目大意:在x轴上,给一些区间,求出能把[0,m]完全覆盖的最少区间个数及该情形下的各个区间. 题目分析:简单的区间覆盖问题.可以按这样一种策略进行下去:在所有区间起点.长度有序的前提下,对于当前起点 ...

  4. UVa 10020 (最小区间覆盖) Minimal coverage

    题意: 数轴上有n个闭区间[ai, bi],选择尽量少的区间覆盖一条指定线段[0, m] 算法: [start, end]为已经覆盖到的区间 这是一道贪心 把各个区间先按照左端点从小到大排序,更新st ...

  5. UVA10020(最小区间覆盖)

    题意:       给你一个区间[0,m]和一些小的区间[l,r]让你选择最少的小区间个数去把整个区间覆盖起来. 思路:       算是比较经典的贪心题目吧(经典于难度没什么对应关系),大体思路可以 ...

  6. POJ - 2376 Cleaning Shifts 贪心(最小区间覆盖)

    Cleaning Shifts Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some clea ...

  7. poj 2376 Cleaning Shifts 最小区间覆盖

    Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 40751   Accepted: 9871 ...

  8. E. Third-Party Software - 2 贪心----最小区间覆盖

    E. Third-Party Software - 2 time limit per test 2.0 s memory limit per test 256 MB input standard in ...

  9. 【区间覆盖问题】uva 10020 - Minimal coverage

    可以说是区间覆盖问题的例题... Note: 区间包含+排序扫描: 要求覆盖区间[s, t]; 1.把各区间按照Left从小到大排序,如果区间1的起点大于s,则无解(因为其他区间的左起点更大):否则选 ...

随机推荐

  1. zend Studio10.6.2破解注册码

    下载破解包:http://download.csdn.net/detail/gsls200808/7982115 安装方法:破解的jar文件com.zend.verifier_10.6.2.v2014 ...

  2. Spring的一种拦截器SimpleUrlHandlerMapping

    spring的一种拦截器,用于在XML文件中配置以拦截url,它是以map映射的方式进行拦截.映射是从前台urls到具体后台的beans.同时支持到bean实例和bean名称的映射,后者要求非单实例控 ...

  3. 第二百八十一节,MySQL数据库-SQL注入和pymysql模块防止SQL注入

    MySQL数据库-SQL注入和pymysql模块防止SQL注入 SQL注入就是通过SQL语句绕开程序判断,获取到数据库的内容 下面以一个简单的程序登录SQL注入举例: 正常登录 1.数据库有一张会员表 ...

  4. 【BZOJ】1088: [SCOI2005]扫雷Mine(递推)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1088 脑残去想递推去了... 对于每一个第二列的格子,考虑多种情况,然后转移.....QAQ 空间可 ...

  5. 让rm命令提示确认后再删除

    首先在~/.bashrc文件中添加一行: # User specific aliases and functionsalias rm='rm -i' 注意,此处 rm 和 = 之间不能有空格,否则会有 ...

  6. ios开发之 -- x-code删除描述文件

    描述文件所在的目录是:~/Library/MobileDevice/Provisioning\ Profiles/ 进入这个目录,删除所有描述文件.

  7. 《C++ Primer Plus》学习笔记 2.1.1 main()函数

    main()函数的基本结构如下: int main() { statements ; } 这几行代码构成了函数定义(function definition),该定义由两部分组成: 第一行int mai ...

  8. 用MCI处置WAV视频时,怎样才能让视频在当前窗口播放

    用MCI处理WAV视频时,怎样才能让视频在当前窗口播放MCI播放视频默认是新开一个窗口播放,播放完毕返回原来的窗口,想着原来窗口播放如何做? mciSendCommand或mciSendString怎 ...

  9. 【BZOJ3232】圈地游戏 分数规划+最小割

    [BZOJ3232]圈地游戏 Description DZY家的后院有一块地,由N行M列的方格组成,格子内种的菜有一定的价值,并且每一条单位长度的格线有一定的费用. DZY喜欢在地里散步.他总是从任意 ...

  10. SaltStack远程执行

    上一篇:SaltStack概述及安装 master也需要安装一个minion 启动salt-master systemctl start salt-master 配置文件在目录/etc/salt下 p ...