题目: 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. Web app root system property already set to different value 错误原因及解决

    http://yzxqml.iteye.com/blog/1761540 ——————————————————————————————————————————————————————————————— ...

  2. HDFS Federation客户端(viewfs)配置攻略

    转自:http://dongxicheng.org/hadoop-hdfs/hdfs-federation-viewfs/ 1. HDFS Federation产生背景 在Hadoop 1.0中,HD ...

  3. MapReduce程序的工作过程

    转自:http://www.aboutyun.com/thread-15494-1-2.html 问题导读1.HDFS框架组成是什么?2.HDFS文件的读写过程是什么?3.MapReduce框架组成是 ...

  4. HashSet非常的消耗空间,TreeSet因为有排序功能,因此资源消耗非常的高,我们应该尽量少使用

    注:HashMap底层也是用数组,HashSet底层实际上也是HashMap,HashSet类中有HashMap属性(我们如何在API中查属性).HashSet实际上为(key.null)类型的Has ...

  5. 【NOIP模拟题】Permutation(dp+高精度)

    首先我们可以这样想: 设状态f[i, j]表示1-i序列有j个'<'的方案数 那么考虑转移 因为i比i-1大,所以可以考虑从i-1来转移.首先i是要插入1-i-1这个序列的,所以我们可以思考插入 ...

  6. strust2的Action中validateXxx方法的用法

    Struts2控制部分时常需要验证来自页面的信息是否合法,若在执行struts2中 public String Xxx()方法操作数据库之前需要验证,ActionSupport提供了一个很好的方法.X ...

  7. [转]Loadrunner经典面试题

    http://www.mianwww.com/html/category/it-interview/loadrunner/  史上最全 在LoadRunner中为什么要设置思考时间和pacing 答: ...

  8. oh my zsh 切换 bash

    zsh切换bash bash切换zsh 切换bash chsh -s /bin/bash 切换zsh chsh -s /bin/zsh

  9. 使用jq实现打印机的效果

    本例中使用的是jq和es6的语法,代码如下: html: <div id="box"> this is test <br/>  这是测试用的 </di ...

  10. 开源内容管理系统Joomla3.5发布 基于PHP 7

    导读 作为深受广大站长喜爱的Joomla开源内容管理系统(Content Management System, CMS)正式推出3.5版本,这也是首个完全支持PHP 7语言开发的Joomla版本,基于 ...