BZOJ1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚
1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚
Time Limit: 5 Sec Memory Limit: 64 MB
Submit: 414 Solved: 191
[Submit][Status]
Description
Farmer
John's cows, pampered since birth, have reached new heights of
fastidiousness. They now require their barn to be immaculate. Farmer
John, the most obliging of farmers, has no choice but hire some of the
cows to clean the barn. Farmer John has N (1 <= N <= 10,000) cows
who are willing to do some cleaning. Because dust falls continuously,
the cows require that the farm be continuously cleaned during the
workday, which runs from second number M to second number E during the
day (0 <= M <= E <= 86,399). Note that the total number of
seconds during which cleaning is to take place is E-M+1. During any
given second M..E, at least one cow must be cleaning. Each cow has
submitted a job application indicating her willingness to work during a
certain interval T1..T2 (where M <= T1 <= T2 <= E) for a
certain salary of S (where 0 <= S <= 500,000). Note that a cow who
indicated the interval 10..20 would work for 11 seconds, not 10. Farmer
John must either accept or reject each individual application; he may
NOT ask a cow to work only a fraction of the time it indicated and
receive a corresponding fraction of the salary. Find a schedule in which
every second of the workday is covered by at least one cow and which
minimizes the total salary that goes to the cows.
翰发现,如果要使这群有洁癖的奶牛满意,他不得不雇佣她们中的一些来清扫牛棚, 约翰的奶牛中有N(1≤N≤10000)头愿意通过清扫牛棚来挣一些零花
钱.由于在某个时段中奶牛们会在牛棚里随时随地地乱扔垃圾,自然地,她们要求在这段时间里,无论什么时候至少要有一头奶牛正在打扫.需要打扫的时段从某一
天的第M秒开始,到第E秒结束f0≤M≤E≤86399).注意这里的秒是指时间段而不是时间点,也就是说,每天需要打扫的总时间是E-M+I秒. 约翰已经从每头牛那里得到了她们愿意接受的工作计划:对于某一头牛,她每天都愿意在笫
Ti,.T2秒的时间段内工作(M≤Ti≤马≤E),所要求的报酬是S美元(0≤S≤500000).与需打扫时段的描述一样,如果一头奶牛愿意工作的时
段是每天的第10_20秒,那她总共工作的时间是11秒,而不是10秒.约翰一旦决定雇佣某一头奶牛,就必须付给她全额的工资,而不能只让她工作一段时
间,然后再按这段时间在她愿意工作的总时间中所占的百分比来决定她的工资.现在请你帮约翰决定该雇佣哪些奶牛以保持牛棚的清洁,当然,在能让奶牛们满意的
前提下,约翰希望使总花费尽量小.
Input
*
Line 1: Three space-separated integers: N, M, and E. * Lines 2..N+1:
Line i+1 describes cow i's schedule with three space-separated integers:
T1, T2, and S.
Output
*
Line 1: a single integer that is either the minimum total salary to get
the barn cleaned or else -1 if it is impossible to clean the barn.
Sample Input
0 2 3 //一号牛,从0号stall打扫到2号,工资为3
3 4 2
0 0 1
INPUT DETAILS:
FJ has three cows, and the barn needs to be cleaned from second 0 to second
4. The first cow is willing to work during seconds 0, 1, and 2 for a total
salary of 3, etc.
Sample Output
HINT
约翰有3头牛,牛棚在第0秒到第4秒之间需要打扫.第1头牛想要在第0,1,2秒内工作,为此她要求的报酬是3美元.其余的依此类推. 约翰雇佣前两头牛清扫牛棚,可以只花5美元就完成一整天的清扫.
Source
题解:
这道题不错,线段树优化DP。
按照l端点排序牛
f[i]表示到i时刻的最小代价,那么对于一只l-r费用c的牛
可以用f[i-1]+c更新l-r
用线段树维护,单点查询,区间更新
代码:
(不知道哪里写残了T_T)
#include<cstdio> #include<cstdlib> #include<cmath> #include<cstring> #include<algorithm> #include<iostream> #include<vector> #include<map> #include<set> #include<queue> #include<string> #define inf 100000000000 #define maxn 80000+2000 #define maxm 500+100 #define eps 1e-10 #define ll long long #define pa pair<int,int> #define for0(i,n) for(int i=0;i<=(n);i++) #define for1(i,n) for(int i=1;i<=(n);i++) #define for2(i,x,y) for(int i=(x);i<=(y);i++) #define for3(i,x,y) for(int i=(x);i>=(y);i--) #define mod 1000000007 using namespace std; inline int read() { int x=,f=;char ch=getchar(); while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();} while(ch>=''&&ch<=''){x=*x+ch-'';ch=getchar();} return x*f; } struct rec{int l,r;ll w;}a[maxn]; struct seg{int l,r;ll mi,tag;}t[*maxn]; int n,x,y; inline bool cmp(rec a,rec b) { return a.l<b.l||(a.l==b.l&&a.r<b.r); } inline void pushup(int k) { t[k].mi=min(t[k<<].mi,t[k<<|].mi); } inline void update(int k,ll z) { t[k].mi=min(t[k].mi,z); t[k].tag=min(t[k].tag,z); } inline void pushdown(int k) { if(t[k].tag==-)return ; update(k<<,t[k].tag); update(k<<|,t[k].tag); t[k].tag=-; } inline void build(int k,int x,int y) { int l=t[k].l=x,r=t[k].r=y,mid=(l+r)>>;t[k].tag=-; if(l==r){t[k].mi=l==?:inf;return ;} build(k<<,l,mid);build(k<<|,mid+,r); pushup(k); } inline ll query(int k,int x) { int l=t[k].l,r=t[k].r,mid=(l+r)>>; if(l==r)return t[k].mi; pushdown(k); if(x<=mid)return query(k<<,x);else return query(k<<|,x); } inline void change(int k,int x,int y,ll z) { int l=t[k].l,r=t[k].r,mid=(l+r)>>; if(l==x&&r==y){update(k,z);return;} pushdown(k); if(y<=mid)change(k<<,x,y,z); else if(x>mid)change(k<<|,x,y,z); else change(k<<,x,mid,z),change(k<<|,mid+,y,z); pushup(k); } int main() { freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); n=read();x=read();y=read(); for1(i,n) { a[i].l=read()-x+;a[i].r=read()-x+;a[i].w=read(); if(a[i].l<=)a[i].l=; } sort(a+,a+n+,cmp); build(,,y-x+); for1(i,n) { if(a[i].r<a[i].l)continue; if(a[i].r<=)continue; if(a[i].l>y-x+)continue; ll t=query(,a[i].l-); if(t!=inf)change(,a[i].l,min(a[i].r,y-x+),t+a[i].w);else break; } //for1(i,n)cout<<a[i].l<<' '<<a[i].r<<' '<<a[i].w<<endl; //for1(k,8*n)cout<<t[k].l<<' '<<t[k].r<<' '<<t[k].mi<<' '<<t[k].tag<<endl; ll ans=query(,y-x+); printf("%lld\n",ans==inf?-:ans); return ; }
hzwer的
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define ll long long
#define inf 10000000000
using namespace std;
inline ll read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,bg,ed;
struct seg{int l,r;ll tag,mn;}t[];
struct data{int t1,t2,c;}a[];
inline bool operator<(data a,data b)
{
return a.t1<b.t1;
}
void pushdown(int k)
{
if(t[k].l==t[k].r)return;
ll tag=t[k].tag;t[k].tag=inf;
if(tag!=inf)
{
t[k<<].tag=min(t[k<<].tag,tag);
t[k<<|].tag=min(t[k<<|].tag,tag);
t[k<<].mn=min(t[k<<].mn,tag);
t[k<<|].mn=min(t[k<<|].mn,tag);
}
}
void build(int k,int l,int r)
{
t[k].l=l;t[k].r=r;t[k].mn=inf;t[k].tag=inf;
if(l==r)return;
int mid=(l+r)>>;
build(k<<,l,mid);build(k<<|,mid+,r);
}
ll query(int k,int x)
{
pushdown(k);
if(x<bg)return ;
int l=t[k].l,r=t[k].r;
if(l==r)return t[k].mn;
int mid=(l+r)>>;
if(x<=mid)return query(k<<,x);
else return query(k<<|,x);
}
void update(int k,int x,int y,ll val)
{
pushdown(k);
int l=t[k].l,r=t[k].r;
if(x==l&&y==r)
{
t[k].tag=val;
t[k].mn=min(t[k].mn,val);
return;
}
int mid=(l+r)>>;
if(y<=mid)update(k<<,x,y,val);
else if(x>mid)update(k<<|,x,y,val);
else
{
update(k<<,x,mid,val);
update(k<<|,mid+,y,val);
}
}
int main()
{
n=read();bg=read();ed=read();
build(,bg,ed);
for(int i=;i<=n;i++)
a[i].t1=read(),a[i].t2=read(),a[i].c=read();
sort(a+,a+n+);
for(int i=;i<=n;i++)
{
int t1=a[i].t1,t2=a[i].t2,c=a[i].c;
ll tmp=query(,t1-);
if(tmp==inf){puts("-1");return ;}
update(,t1,t2,tmp+c);
}
ll ans=query(,ed);
if(ans<inf)printf("%lld",ans);
else puts("-1");
return ;
}
我是sb,刚开始线段树的懒惰标记设为-1,简直没救。。。
代码:
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#define inf 100000000000
#define maxn 80000+2000
#define maxm 500+100
#define eps 1e-10
#define ll long long
#define pa pair<int,int>
#define for0(i,n) for(int i=0;i<=(n);i++)
#define for1(i,n) for(int i=1;i<=(n);i++)
#define for2(i,x,y) for(int i=(x);i<=(y);i++)
#define for3(i,x,y) for(int i=(x);i>=(y);i--)
#define mod 1000000007
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=*x+ch-'';ch=getchar();}
return x*f;
}
struct rec{int l,r;ll w;}a[maxn];
struct seg{int l,r;ll mi,tag;}t[*maxn];
int n,x,y;
inline bool cmp(rec a,rec b)
{
return a.l<b.l||(a.l==b.l&&a.r<b.r);
}
inline void pushup(int k)
{
t[k].mi=min(t[k<<].mi,t[k<<|].mi);
}
inline void update(int k,ll z)
{
t[k].mi=min(t[k].mi,z);
t[k].tag=min(t[k].tag,z);
}
inline void pushdown(int k)
{
if(t[k].tag==inf)return ;
update(k<<,t[k].tag);
update(k<<|,t[k].tag);
t[k].tag=inf;
}
inline void build(int k,int x,int y)
{
int l=t[k].l=x,r=t[k].r=y,mid=(l+r)>>;t[k].tag=inf;
if(l==r){t[k].mi=l==?:inf;return ;}
build(k<<,l,mid);build(k<<|,mid+,r);
pushup(k);
}
inline ll query(int k,int x)
{
int l=t[k].l,r=t[k].r,mid=(l+r)>>;
if(l==r)return t[k].mi;
pushdown(k);
if(x<=mid)return query(k<<,x);else return query(k<<|,x);
}
inline void change(int k,int x,int y,ll z)
{
int l=t[k].l,r=t[k].r,mid=(l+r)>>;
if(l==x&&r==y){update(k,z);return;}
pushdown(k);
if(y<=mid)change(k<<,x,y,z);
else if(x>mid)change(k<<|,x,y,z);
else change(k<<,x,mid,z),change(k<<|,mid+,y,z);
pushup(k);
}
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
n=read();x=read();y=read();
for1(i,n)
{
a[i].l=read()-x+;a[i].r=read()-x+;a[i].w=read();
if(a[i].l<=)a[i].l=;
}
sort(a+,a+n+,cmp);
build(,,y-x+);
for1(i,n)
{
if(a[i].r<a[i].l)continue;
if(a[i].r<=)continue;
if(a[i].l>y-x+)continue;
ll t=query(,a[i].l-);
if(t!=inf)change(,a[i].l,min(a[i].r,y-x+),t+a[i].w);
}
//for1(i,n)cout<<a[i].l<<' '<<a[i].r<<' '<<a[i].w<<endl;
//for1(k,8*n)cout<<t[k].l<<' '<<t[k].r<<' '<<t[k].mi<<' '<<t[k].tag<<endl;
ll ans=query(,y-x+);
printf("%lld\n",ans==inf?-:ans);
return ;
}
BZOJ1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚的更多相关文章
- [BZOJ1672][Usaco2005 Dec]Cleaning Shifts 清理牛棚 线段树优化DP
链接 题意:给你一些区间,每个区间都有一个花费,求覆盖区间 \([S,T]\) 的最小花费 题解 先将区间排序 设 \(f[i]\) 表示决策到第 \(i\) 个区间,覆盖满 \(S\dots R[i ...
- BZOJ 1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚
题目 1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚 Time Limit: 5 Sec Memory Limit: 64 MB Description Farm ...
- BZOJ_1672_[Usaco2005 Dec]Cleaning Shifts 清理牛棚_动态规划+线段树
BZOJ_1672_[Usaco2005 Dec]Cleaning Shifts 清理牛棚_动态规划+线段树 题意: 约翰的奶牛们从小娇生惯养,她们无法容忍牛棚里的任何脏东西.约翰发现,如果要使这群 ...
- P4644 [Usaco2005 Dec]Cleaning Shifts 清理牛棚
P4644 [Usaco2005 Dec]Cleaning Shifts 清理牛棚 你有一段区间需要被覆盖(长度 <= 86,399) 现有 \(n \leq 10000\) 段小线段, 每段可 ...
- [Usaco2005 Dec]Cleaning Shifts 清理牛棚 (DP优化/线段树)
[Usaco2005 Dec] Cleaning Shifts 清理牛棚 题目描述 Farmer John's cows, pampered since birth, have reached new ...
- 【BZOJ1672】[Usaco2005 Dec]Cleaning Shifts 清理牛棚 动态规划
[BZOJ1672][Usaco2005 Dec]Cleaning Shifts Description Farmer John's cows, pampered since birth, have ...
- 洛谷P4644 [USACO2005 Dec]Cleaning Shifts 清理牛棚 [DP,数据结构优化]
题目传送门 清理牛棚 题目描述 Farmer John's cows, pampered since birth, have reached new heights of fastidiousness ...
- 【bzoj1672】[USACO2005 Dec]Cleaning Shifts 清理牛棚
题目描述 Farmer John's cows, pampered since birth, have reached new heights of fastidiousness. They now ...
- 【bzoj1672】[USACO2005 Dec]Cleaning Shifts 清理牛棚 dp/线段树
题目描述 Farmer John's cows, pampered since birth, have reached new heights of fastidiousness. They now ...
随机推荐
- HDU4605---Magic Ball Game(主席树 好题)
题意:一颗二叉树,任意节点要么有两个孩子要么没孩子. 然后有一个球,从结点1开始往子孙结点走. 每碰到一个结点,有三种情况 如果此球重量等于该结点重量,球就停下了 如果此球重量小于该结点重量,则分别往 ...
- 【剑指offer】面试题24:二叉搜索树的后序遍历序列
题目: 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. 思路: 递归 注意,主要就是假定数组为空时结果为fa ...
- CCF 送货 + 欧拉路模板
#include <bits/stdc++.h> using namespace std; stack<int> st; vector<]; ][]; ],cp[]; i ...
- (转)awk命令
转自:http://man.lupaworld.com/content/manage/ringkee/awk.htm#id2874788 整理:Jims of 肥肥世家 <jims.yang@g ...
- 探究css !important的应用之道
定义及语法: !important是CSS1就定义的语法,作用是提高指定样式规则的应用优先权. 语法格式:{ cssRule !important },即将!important写在定义的最后面, 例如 ...
- POJ 1637 混合图求欧拉回路 最大流实现
前面讲过了无向图,有向图求欧拉回路,欧拉通路的做法.可以直接根据度数来判断,当然前提是这是一个连通图. 这道题既有无向边,又有有向边,然后求欧拉回路. 采用的方法是最大流. 具体处理方法. 首先,我们 ...
- 小学生之KTV播放原理
第一步: 创建一个Song类 //歌曲名称 public string SongName { get; set; } //歌曲路劲 public string SongPath { get; set ...
- Head First HTML与CSS — 布局与定位
1.流(flow)是浏览器在页面上摆放HTML元素所用的方法. 对于块元素,浏览器从上到下沿着元素流逐个显示所遇到的各个元素,会在每个块元素之间加一个换行: 对于内联元素,在水平方向会相互挨着,总体上 ...
- XAML 命名空间和命名空间映射
本主题将介绍大部分 XAML 文件的根元素中存在的 XML/XAML 命名空间 (xmlns) 映射.它还将介绍如何为自定义类型和程序集生成类似的映射. XAML 命名空间如何与代码定义和类型库相关 ...
- SQL Server中的临时表和表变量 Declare @Tablename Table
在SQL Server的性能调优中,有一个不可比面的问题:那就是如何在一段需要长时间的代码或被频繁调用的代码中处理临时数据集?表变量和临时表是两种选择.记得在给一家国内首屈一指的海运公司作SQL Se ...