POJ2376 Cleaning Shifts
题意
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. - 样例输出
-
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
<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
分析
针对这题的贪心做法:
由于代价都是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的更多相关文章
- poj2376 Cleaning Shifts【线段树】【DP】
Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 32561 Accepted: 7972 ...
- POJ2376 Cleaning Shifts 【贪心】
Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11542 Accepted: 3004 ...
- poj2376 Cleaning Shifts(区间贪心,理解题意)
https://vjudge.net/problem/POJ-2376 题意理解错了!!真是要仔细看题啊!! 看了poj的discuss才发现,如果前一头牛截止到3,那么下一头牛可以从4开始!!! # ...
- poj-2376 Cleaning Shifts (排序+贪心)
http://poj.org/problem?id=2376 john有n头牛做打扫工作,他想在t时间内每个时间都至少有一头牛在做打扫工作,第一头牛在1,最后一头牛在t时间,每一头牛工作都有一个开始时 ...
- poj2376 Cleaning Shifts 区间贪心
题目大意: (不说牛了) 给出n个区间,选出个数最少的区间来覆盖区间[1,t].n,t都是给出的. 题目中默认情况是[1,x],[x+1,t]也是可以的.也就是两个相邻的区间之间可以是小区间的右端与大 ...
- 【POJ - 2376】Cleaning Shifts(贪心)
Cleaning Shifts Descriptions: 原文是English,我这就直接上Chinese了,想看原文的点一下链接哦 大表哥分配 N (1 <= N <= 25,000) ...
- 【BZOJ1672】[Usaco2005 Dec]Cleaning Shifts 清理牛棚 动态规划
[BZOJ1672][Usaco2005 Dec]Cleaning Shifts Description Farmer John's cows, pampered since birth, have ...
- poj 2376 Cleaning Shifts
http://poj.org/problem?id=2376 Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- POJ 2376 Cleaning Shifts(轮班打扫)
POJ 2376 Cleaning Shifts(轮班打扫) Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] Farmer ...
随机推荐
- Qt基础学习---滑动条之QSlider
Qt滑动条基本用法: //mydialog.h #ifndef MYDIALOG_H #define MYDIALOG_H #include <QDialog> class QLineEd ...
- react-native android/ios 根据配置文件编译时自动修改版本号
开发react-native时大都有过这个操作,当版本迭代时候要修改app版本号时,一般都这样做 Android: 的要修改build.gradle文件的versionName ios: 打开xcod ...
- win8外包公司——技术分享:参数传递
页面之间传递参数 windows phone 的参数传递和web 差不多.用“?”号传递 多个参数的时候用 “&”做分隔. 我接着昨天的项目继续添加一个FourPage.xaml 在昨天的Th ...
- ES6标准之箭头函数
语法 具有一个参数的简单函数 var single = a => a single('hello, world') // 'hello, world' 没有参数的需要用在箭头前加上小括号 var ...
- Json string value cannot have line breaks(解决方法)
点击所在的项目->Proterties->MyEclipse->Validation,把JSON Validator中的Manual和Build的对号给去掉,然后apply,OK. ...
- EL函数库及用法
导入标签库: <%@ tablib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%&g ...
- MVC实战之排球计分(七)——软件的具体实现与测试
在前面的几篇博客中咱们已经写过了软件的大概实现,在这篇博客中将讲述此软件的具体实现与测试. 1,新建一个项目,命名为:Volleyball,选择基本模板.如图: 点击确定.创建项目. 2,右键单击mo ...
- UEBA 学术界研究现状——用户行为异常检测思路:序列挖掘prefixspan,HMM,LSTM/CNN,SVM异常检测,聚类CURE算法
论文 技术分析<关于网络分层信息泄漏点快速检测仿真> "1.基于动态阈值的泄露点快速检测方法,采样Mallat算法对网络分层信息的离散采样数据进行离散小波变换;利用滑动窗口对该尺 ...
- Vue开发中遇到的问题及解决方案
问题一:npm run dev的时候控制台报错Vue packages version mismatch,如下面 可是检查package.json文件里vue和vue-template-compile ...
- python--jianja2
一:渲染模版 要渲染一个模板,通过render_template方法即可. @app.route('/about/')def about():return render_template('about ...