题意

POJ2376 Cleaning Shifts 0x50「动态规划」例题

http://bailian.openjudge.cn/practice/2376

总时间限制:
1000ms
内存限制:
65536kB
描述
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.
			<dt>输入</dt>
<dd>* Line 1: Two space-separated integers: N and T<br><br>* 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.</dd>
<dt>输出</dt>
<dd>* 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.</dd>
<dt>样例输入</dt>
<dd><pre>3 10

1 7

3 6

6 10

样例输出
2
提示
This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed.

INPUT DETAILS:

There are 3 cows and 10 shifts. Cow #1 can work shifts 1..7, cow #2 can work shifts 3..6, and cow #3 can work shifts 6..10.

OUTPUT DETAILS:

By selecting cows #1 and #3, all shifts are covered. There is no way to cover all the shifts using fewer than 2 cows.
来源
USACO 2004 December Silver

分析

针对这题的贪心做法:

由于代价都是1,贪心策略是从左往右,尽量选择长度最大的区间。

首先对所有奶牛排序,按照开始时间排序。

然后更新起点=终点+1,搜索剩下的奶牛中能够覆盖这个起点同时终点最远的那一头,更新终点。

也可以考虑dp,设f[x]表示覆盖[1,x]的最小代价,那么把区间按照r排序,每次用线段树维护更新即可。

时间复杂度\(O(n \log n)\),这种做法可以轻松的处理 POJ3171 Cleaning Shifts 这道同名题。

代码

#include<iostream>
#include<cstring>
#include<algorithm>
#define rg register
#define il inline
#define co const
template<class T>il T read(){
rg T data=0,w=1;rg char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') w=-1;ch=getchar();}
while(isdigit(ch)) data=data*10+ch-'0',ch=getchar();
return data*w;
}
template<class T>il T read(rg T&x) {return x=read<T>();}
typedef long long ll;
using namespace std; co int N=1e5+1,INF=0x3f3f3f3f;
int n,m,f[N],b[N],tot;
struct T{
int l,r,x;
bool operator<(co T&w)co {return r<w.r;}
}a[N],t[N*4];
#define lc (p<<1)
#define rc (p<<1|1)
void build(int p,int l,int r){
t[p].l=l,t[p].r=r,t[p].x=l?INF:0;
if(l==r) return;
int mid=l+r>>1;
build(lc,l,mid),build(rc,mid+1,r);
}
void change(int p,int x,int y){
if(t[p].l==t[p].r) return t[p].x=y,void();
int mid=t[p].l+t[p].r>>1;
if(x<=mid) change(lc,x,y);
else change(rc,x,y);
t[p].x=min(t[lc].x,t[rc].x);
}
int ask(int p,int l,int r){
if(l<=t[p].l&&t[p].r<=r) return t[p].x;
int mid=t[p].l+t[p].r>>1;
if(r<=mid) return ask(lc,l,r);
if(l>mid) return ask(rc,l,r);
return min(ask(lc,l,r),ask(rc,l,r));
}
int main(){
read(n),read(m);
b[++tot]=1;
for(int i=1;i<=n;++i){
read(a[i].l),read(a[i].r);
b[++tot]=a[i].l,b[++tot]=a[i].l+1;
b[++tot]=a[i].r,b[++tot]=a[i].r+1;
}
b[++tot]=m;
sort(b+1,b+tot+1),tot=unique(b+1,b+tot+1)-b-1;
while(b[tot]>m) --tot;
sort(a+1,a+n+1);
build(1,0,tot);
memset(f,0x3f,sizeof f);
f[0]=0;
for(int i=1;i<=n;++i){
a[i].l=lower_bound(b+1,b+tot+1,a[i].l)-b;
a[i].r=lower_bound(b+1,b+tot+1,a[i].r)-b;
int num=ask(1,a[i].l-1,a[i].r-1)+1;
if(f[a[i].r]>num)
f[a[i].r]=num,change(1,a[i].r,f[a[i].r]);
}
if(f[tot]==INF) puts("-1");
else printf("%d\n",f[tot]);
return 0;
}

POJ2376 Cleaning Shifts的更多相关文章

  1. poj2376 Cleaning Shifts【线段树】【DP】

    Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 32561   Accepted: 7972 ...

  2. POJ2376 Cleaning Shifts 【贪心】

    Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11542   Accepted: 3004 ...

  3. poj2376 Cleaning Shifts(区间贪心,理解题意)

    https://vjudge.net/problem/POJ-2376 题意理解错了!!真是要仔细看题啊!! 看了poj的discuss才发现,如果前一头牛截止到3,那么下一头牛可以从4开始!!! # ...

  4. poj-2376 Cleaning Shifts (排序+贪心)

    http://poj.org/problem?id=2376 john有n头牛做打扫工作,他想在t时间内每个时间都至少有一头牛在做打扫工作,第一头牛在1,最后一头牛在t时间,每一头牛工作都有一个开始时 ...

  5. poj2376 Cleaning Shifts 区间贪心

    题目大意: (不说牛了) 给出n个区间,选出个数最少的区间来覆盖区间[1,t].n,t都是给出的. 题目中默认情况是[1,x],[x+1,t]也是可以的.也就是两个相邻的区间之间可以是小区间的右端与大 ...

  6. 【POJ - 2376】Cleaning Shifts(贪心)

    Cleaning Shifts Descriptions: 原文是English,我这就直接上Chinese了,想看原文的点一下链接哦 大表哥分配 N (1 <= N <= 25,000) ...

  7. 【BZOJ1672】[Usaco2005 Dec]Cleaning Shifts 清理牛棚 动态规划

    [BZOJ1672][Usaco2005 Dec]Cleaning Shifts Description Farmer John's cows, pampered since birth, have ...

  8. poj 2376 Cleaning Shifts

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

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

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

随机推荐

  1. msvc命令行cl编译c程序问题及解决

    1.cmd命令行cl提示没有这玩意儿 装上Visual Studio之类 2.cl main.c提示缺dll everything搜dll所在路径,在环境配置PATH增加对应bin.IDE 3.cl ...

  2. ssh登录后很慢 卡住 树莓派

    ssh登录后很慢,ls命令都响应很慢.sftp也连接不上.结果发现是路由器的问题,重启一下路由器就好了

  3. 安卓外派(Android外派)提供安卓程序员外派业务(北京动点,可签合同)

    北京动点飞扬长年提供安卓工程师外派业务. 平均技术情况如下: 1.2~3年以上Android平台开发经验 2.熟练掌握java技术,熟悉面向对象编程设计 3.熟悉Android应用开发框架及Activ ...

  4. JS模态框 简单案例

    演示: <!doctype html> <html lang="en"> <head> <meta charset="UTF-8 ...

  5. servelet基础

    1.1           servlet简介 Java Servlet 是运行在 Web 服务器或应用服务器上的程序.她是一个浏览器和服务器之间的中间层.程序员开发程序,实现servlet的接口.S ...

  6. Django 编写模板并渲染的示例

    >>> from django.template import Template, Context >>> raw_template = ""& ...

  7. JVM垃圾回收(一)- 什么是垃圾回收

    什么是垃圾回收? 垃圾回收是追踪所有正在被使用的对象,并标注剩余的为garbage.这里我们先从JVM的GC是如何实现的说起. 手动内存管理 在开始介绍垃圾回收之前,我们先复习一下手动内存管理.它是指 ...

  8. pandas的数据结构之series

    Pandas的数据结构 1.Series Series是一种类似于一维数组的对象,由下面两个部分组成: index:相关的数据索引标签 values:一组数据(ndarray类型) series的创建 ...

  9. Java多线程——Condition条件

    简介 Condition中的await()方法相当于Object的wait()方法,Condition中的signal()方法相当于Object的notify()方法,Condition中的signa ...

  10. centos7安装doxygen

    编译 编译过程参考官网:https://www.stack.nl/~dimitri/doxygen/download.html 编译过程: git clone https://github.com/d ...