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 ...
随机推荐
- HDU_2011——求多项式的前n项和
Problem Description 多项式的描述如下:1 - 1/2 + 1/3 - 1/4 + 1/5 - 1/6 + ...现在请你求出该多项式的前n项的和. Input 输入数据由2行组 ...
- HDU3726---Graph and Queries 离线处理+Treap
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3726 题意:n个点m条边的一张无向图,每个点有一个权值, 有3中操作. D X 删除第X条边 Q X ...
- MD中bitmap源代码分析--设置流程
1. 同步/异步刷磁盘 Bitmap文件写磁盘分同步和异步两种: 1) 同步置位:当盘阵有写请求时,对应的bitmap文件相应bit被置位,bitmap内存页被设置了DIRTY标志.而在下发写请求给磁 ...
- Wix - 教程
1. 不错的教程 http://www.merlinia.com/mdt/WiXTutorial4.msl 2.
- Android应用程序的安装位置
Android应用程序的默认安装位置以及是否可移动取决于开发者在其AndroidManifest.xml中的设置: <manifestxmlns:android="http://s ...
- boost::pool与内存池技术
建议看这个链接的内容:http://cpp.winxgui.com/cn:mempool-example-boost-pool Pool分配是一种分配内存方法,用于快速分配同样大小的内存块, ...
- c++11 : Local and Unnamed Types as Template Arguments
In N2402, Anthony Williams proposes that local types, and unnamed types be usable as template argume ...
- Nginx 主配置文件参数详解
Nginx 主配置文件参数详解 Nginx 安装完毕后,会有响应的安装目录,安装目录里 nginx.conf 为 nginx 的主配置文件, ginx 主配置文件分为 4 部分,main(全局配置). ...
- 验证视图状态 MAC 失败。如果此应用程序由网络场或群集承载,请确保 <machineKey>
转自:http://hi.baidu.com/taotaowyx/blog/item/074bb8d83907bb3233fa1ce6.html 验证视图状态 MAC 失败.如果此应用程序由网络场或群 ...
- 华为 oj 表示数字(代码有参考)理解算法设计
虽然是初级题目,但是 也不是太容易就做出来的 还是用c++ 好些 因为c++ string 是可以存储到缓冲区的, 字符串长度可以变化 参考了某神的代码 和我的思路一样 ,就拿来主义了,挺经典的一道面 ...