POJ 2376

题意:

给出一给大区间和n各小区间,问最少可以用多少小区间覆盖整个大区间。

分析:

贪心法。设t为当前所有已确定区间的最右端,那我们可以每次都取所有可选的小区间(左端点<=t+1)中右端点最大的值,然后更新最右端点ans++。初始时t=0

注:所谓衔接不是[0,1][1,2]这样首尾相接,而是[0,1][2,3]即可,故为 t+1

#include<iostream>
#include<algorithm>
#include<string.h>
#include<cstring>
#include<vector>
#include<set>
#include<stdio.h>
#include<stack>
#include<bitset>
using namespace std; int n,T;
struct P
{
int x,y;
}s[25002];
int cmp(P a,P b)
{
return a.x<b.x;
}
int main()
{
scanf("%d%d",&n,&T);
for(int i=0;i<n;i++)scanf("%d%d",&s[i].x,&s[i].y);
sort(s,s+n,cmp);
s[n].x=0x7fffffff; //边界处要考虑
int t=0,temp=0,ans=0;
bool f=0;
for(int i=0;i<n;i++)
{
if(s[i].x<=t+1)
{
if(s[i].y>temp)temp=s[i].y,f=1;
if(s[i+1].x>t+1&&f)
{
ans++;
t=temp;
f=0;
}
}
}
if (t<T) printf("-1\n"); else printf("%d\n",ans);
return 0;
}

POJ 2376 Cleaning Shifts【贪心】的更多相关文章

  1. POJ 2376 Cleaning Shifts 贪心

    Cleaning Shifts 题目连接: http://poj.org/problem?id=2376 Description Farmer John is assigning some of hi ...

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

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

  3. poj 2376 Cleaning Shifts 贪心 区间问题

    <pre name="code" class="html"> Cleaning Shifts Time Limit: 1000MS   Memory ...

  4. POJ 2376 Cleaning Shifts (贪心,区间覆盖)

    题意:给定1-m的区间,然后给定n个小区间,用最少的小区间去覆盖1-m的区间,覆盖不了,输出-1. 析:一看就知道是贪心算法的区间覆盖,主要贪心策略是把左端点排序,如果左端点大于1无解,然后, 忽略小 ...

  5. POJ 2376 Cleaning Shifts(轮班打扫)

    POJ 2376 Cleaning Shifts(轮班打扫) Time Limit: 1000MS   Memory Limit: 65536K [Description] [题目描述] Farmer ...

  6. poj 2376 Cleaning Shifts

    http://poj.org/problem?id=2376 Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  7. 【原创】poj ----- 2376 Cleaning Shifts 解题报告

    题目地址: http://poj.org/problem?id=2376 题目内容: Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K ...

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

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

  9. ACM学习历程——POJ 2376 Cleaning Shifts(贪心)

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

随机推荐

  1. IP地址分类以及子网划分

    五类IP地址段 根据上表的说明,我们可以知道: 你只要知道 IP 的第一个十进制数,就能够约略了解到该 IP 属于哪一个等级, 以及同网域 IP 数量有多少. 这也是为啥我们上头选了 192.168. ...

  2. Python基础(函数部分)-day04

    写在前面 上课第四天,打卡: 加勒比海盗今天上映:端午节公司发的粽子很有范! 一.函数的基本概念 - 函数是什么?  函数,就是一个'锤子',一个具有特定功能的'锤子',使用者可以在适当的时候使用这个 ...

  3. ELF格式探析之三:sections

    前文链接: ELF格式探析之一:Segment和Section ELF格式探析之二:文件头ELF Header详解 今天我们讲对目标文件(可重定位文件)和可执行文件都很重要的section. 我们在讲 ...

  4. u-boot移植(十三)---代码修改---裁剪及环境变量 二

    一.错误处理 上一节遇到一个错误: print一下: 发现我们在jz2440.h中静态写的网络参数都没有写进去. dm9000 address not set. dm9000的地址未设置. 这里对应两 ...

  5. hadoop HA 配置 + zookeeper 服务注册

    环境测试 6台机器 centos6.7 x64 master ( namenode/cluster ) 10.10.100.101 namenode1 10.10.100.105 namenode2 ...

  6. Wannafly挑战赛21 E 未来城市规划

    传送门 题目中给的信息很难直接维护,但是可以考虑一条边对答案的贡献 在以\(x\)为根的子树里,如果一条边\(i\)的权值为\(w_i\),这条边深度更深的端点为\(to_i\),那么这条边对这个子树 ...

  7. SSM框架报错分析(一)——There is no getter for property named 'XXX' in 'class java.lang.String'

    一.发现问题 <select id="queryStudentByNum" resultType="student" parameterType=&quo ...

  8. TypeError: 'range' object does not support item assignment

    TypeError: 'range' object does not support item assignment I was looking at some python 2.x code and ...

  9. Linux串口—struct termios结构体【转】

    转自:https://blog.csdn.net/yemingzhu163/article/details/5897156 一.数据成员 termios 函数族提供了一个常规的终端接口,用于控制非同步 ...

  10. Redis简介+常用命令

    Redis=REmote DIctionary Server Redis是一个使用C语言编写的开源数据库,是高性能的key-value数据库,是内存数据库,支持数据持久化. Redis常用数据类型: ...