POJ 2376 Cleaning Shifts 贪心

时间:2022-02-25 02:14:23

Cleaning Shifts

题目连接:

http://poj.org/problem?id=2376

Description

Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cleaning chores around the barn. He always wants to have one cow working on cleaning things up and has divided the day into T shifts (1 <= T <= 1,000,000), the first being shift 1 and the last being shift T.

Each cow is only available at some interval of times during the day for work on cleaning. Any cow that is selected for cleaning duty will work for the entirety of her interval.

Your job is to help Farmer John assign some cows to shifts so that (i) every shift has at least one cow assigned to it, and (ii) as few cows as possible are involved in cleaning. If it is not possible to assign a cow to each shift, print -1.

Input

  • Line 1: Two space-separated integers: N and T

  • Lines 2..N+1: Each line contains the start and end times of the interval during which a cow can work. A cow starts work at the start time and finishes after the end time.

Output

  • Line 1: The minimum number of cows Farmer John needs to hire or -1 if it is not possible to assign a cow to each shift.

Sample Input

3 10

1 7

3 6

6 10

Sample Output

2

Hint

题意

给你n个区间,要求你选出最少的区间,使得能够覆盖[1,t]

题解:

dp很简单,dp[i] = min(dp[j])+1,其中需满足a[j].r+1>=a[i].l

贪心也很简单,每次我们扫的时候,直接扫到能够转移到的最右边就好了(其实也是DP思想..

期末考试前写道水题攒攒RP T T

代码

#include<algorithm>
#include<iostream>
#include<stdio.h>
using namespace std; struct node
{
int x,y;
};
bool cmp(node a,node b)
{
if(a.x==b.x)return a.y>b.y;
return a.x<b.x;
}
node p[25005]; int main()
{
int n,t;
scanf("%d%d",&n,&t);
for(int i=1;i<=n;i++)
scanf("%d%d",&p[i].x,&p[i].y);
sort(p+1,p+1+n,cmp);
int l = 0;
int ans = 0;
int i = 1;
while(i<=n)
{
if(p[i].x>l+1)return puts("-1");
int tmp = l;
while(i<=n&&p[i].x<=l+1)
{
tmp = max(tmp,p[i].y);
i++;
}
l = tmp;ans++;
if(l>=t)return printf("%d\n",ans);
}
return puts("-1");
}

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

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

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

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

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

  3. POJ 2376 Cleaning Shifts &lpar;贪心,区间覆盖&rpar;

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

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

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

  5. poj 2376 Cleaning Shifts

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

  6. POJ 2376 Cleaning Shifts【贪心】

    POJ 2376 题意: 给出一给大区间和n各小区间,问最少可以用多少小区间覆盖整个大区间. 分析: 贪心法.设t为当前所有已确定区间的最右端,那我们可以每次都取所有可选的小区间(左端点<=t+ ...

  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. Search Insert Position &lbrack;LeetCode&rsqb;

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  2. IO流--流转换

    import java.io.*; //键盘读入 字节流转换成字符流操作 提高效率 public class io { public static void main(String[] args) t ...

  3. Memcache的部署和使用

    一.memcache简介 Memcache是danga.com的一个项目,最早是为 LiveJournal 服务的,目前全世界不少人使用这个缓存项目来构建自己大负载的网站,来分担数据库的压力. Mem ...

  4. win7下简单FTP服务器搭建

    本文介绍通过win7自带的IIS来搭建一个只能实现基本功能的FTP服务器,第一次装好WIN7后我愣是没整出来,后来查了一下网上资料经过试验后搭建成功,其实原理和步骤与windows前期的版本差不多,主 ...

  5. mycat实例(3)

    配置MyCat 4. 配置schema.xml schema.xml里面管理着MyCat的逻辑库.表,每张表使用的分片规则.分布在哪个DataNode以及DataSource上. 之前的例子: &lt ...

  6. Mysql服务启动问题

    mysql ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) u ...

  7. Chapter 14&lowbar;2 全局变量声明

    Lua中的全局变量不需要声明就可以使用.对于小程序十分方便,但是大型程序中 一处简单的笔误就可能造成难以发现的bug. 不过,这种性能可以改变.由于Lua将全局变量放在一个普通的table中,可以通过 ...

  8. 列表&lpar;list&rpar; ----python

    Python 列表(List) 序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推. Python有6个序列的内置类型 ...

  9. 前端入门12-JavaScript语法之函数

    声明 本系列文章内容全部梳理自以下几个来源: <JavaScript权威指南> MDN web docs Github:smyhvae/web Github:goddyZhao/Trans ...

  10. bootstrap顶部导航遮挡下面内容的解决办法

    使用bootstrap设置顶部导航,并将导航栏固定,代码如下: <nav class="navbar navbar-expand-lg navbar-light bg-light fi ...