题意

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. lnmp或者lamp环境一键安装

    参考网址:https://lnmp.org/install.html 下载并安装LNMP一键安装包: 您可以选择使用下载版(推荐美国及海外VPS或空间较小用户使用)或者完整版(推荐国内VPS使用,国内 ...

  2. js同步、异步、回调的执行顺序以及闭包的理解

    首先,记住同步第一.异步第二.回调最末的口诀 公式表达:同步=>异步=>回调 看一道经典的面试题: for (var i = 0; i < 5; i++) { setTimeout( ...

  3. 解决github访问及上传慢的问题

    在本地host文件中添加映射 http://tool.chinaz.com/dns , 查询 github.global.ssl.fastly.net 和 assets-cdn.github.com ...

  4. VR外包—长年承接虚拟现实项目和AR外包游戏、软件(北京动点飞扬软件)

    VR外包AR外包公司(虚拟现实外包公司)承接虚拟现实项目开发(企业.教育.游戏) 可公对公签正规合同,开发票. 我们是北京的公司.专业团队,成员为专业 VR/AR 产品公司一线开发人员,有大型产品开发 ...

  5. Docker安装Tomcat镜像并部署web项目

    一.安装Tomcat 1.查找Docker Hub上的tomcat镜像 docker search tomcat 2.拉取官方的镜像 docker pull tomcat 等待下载完毕,需要一些时间. ...

  6. Linux下修改用户的UID、GID

    01.用户的UID和GID不能被占用 [root@26 ~]# id mvpuid=503(mvp) gid=503(mvp) groups=503(mvp) ###假定我需要设置mvp的uid/gi ...

  7. 如何设置在html中保留超链接格式但不实现跳转

    ---恢复内容开始--- 老师布置了一个任务,要求用户登录或者不登录都会有一个主页(home.jsp),如果登录的话就会跳转至登录界面(login.jsp),在登录界面中有个验证码,还要求有个和很多登 ...

  8. 【转】前端的BFC、IFC、GFC和FFC

    什么是BFC.IFC.GFC和FFC CSS2.1中只有BFC和IFC, CSS3中才有GFC和FFC. FC的全称是:Formatting Contexts,是W3C CSS2.1规范中的一个概念. ...

  9. YII框架实现 RBAC

    (1).在  common\config\main.php添加 'components' => [ ’authManager’ => [            ’class’ => ...

  10. js 获取getElementsTagName()方法返回值的内容

    <div id="news-top" class="section"> <h3>Some title</h3> <di ...