题目: 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. http://jadethao.iteye.com/blog/1926525

    http://jadethao.iteye.com/blog/1926525 ————————————————————————————————————————————————————————————— ...

  2. treegrid-dnd.js

    (function($){ $.extend($.fn.treegrid.defaults, { onBeforeDrag: function(row){}, // return false to d ...

  3. Jquery仿IGoogle实现可拖动窗口

    google可谓是ajax的特效用的淋漓尽致,google suggest, google map,igoogle 可拖动窗口等等...今天要做一个网站的类似效果,与编程人生的站长沟通了一下,仿照iG ...

  4. 如何实现Asp与Asp.Net共享Session

    <iframe align="top" marginwidth="0" marginheight="0" src="http ...

  5. Android SDK代理server解决国内不能更新下载问题

    读者须知:本篇文章中最靠谱的是第三种方式,近期有读者反映第三种方式也不行了,以下提供一点其它途径的开源镜像网站: 国内高校的开源镜像站 中国科学技术大学(debian.ustc.edu.cn) 上海交 ...

  6. zabbix服务器搭建

    zabbix服务器源码安装 参看官方文档 这里不做过多的翻译,我的系统是centos6.5,安装的时候是base安装,所以要装一些其他依赖包,除此之外,还有一些php插件: extension=bcm ...

  7. jquery ajax生成Select

    function DropDownList(url, domId, defaultValue) {    /// <summary>    /// ajax生成select    /// ...

  8. 【C语言】求旋转数组的最小数字,输入一个递增排序的数组的一个旋转,输出其最小元素

    //求旋转数组的最小数字,输入一个递增排序的数组的一个旋转,输出其最小元素 #include <stdio.h> #include <string.h> int find_mi ...

  9. hdu 1425:sort(排序,经典题。快排模板)

    sort Time Limit : 6000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submissi ...

  10. Java千百问_05面向对象(011)_引用传递和值传递有什么差别

    点击进入_很多其它_Java千百问 1.什么是值传递 值传递,是将内存空间中某个存储单元中存放的值,传送给还有一个存储单元.(java中的存储单元并不是物理内存的地址,但具有相关性) 比如: //定义 ...