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 ...
随机推荐
- 二叉树后序遍历的非递归算法(C语言)
首先非常感谢‘hicjiajia’的博文:二叉树后序遍历(非递归) 这篇随笔开启我的博客进程,成为万千程序员中的一员,坚持走到更远! 折磨了我一下午的后序遍历中午得到解决,关键在于标记右子树是否被访问 ...
- CSU1659: Graph Center(最短路)
Description The center of a graph is the set of all vertices of minimum eccentricity, that is, the s ...
- 在Centos 5.6下安装 redis
先引用redis官方(http://redis.io/) 的介绍: Redis is an open source, advanced key-value store.<br>It is ...
- [AngularJS] Using AngularJS's ngClass
.blue{ color: blue } .bold{ font-weight: bold; } .large{ font-size: 40px; } ngClass can accept an ar ...
- Java基础知识强化56:经典排序之快速排序(QuickSort)
1. 快速排序的原理: 快速排序(Quicksort)是对冒泡排序的一种改进. 快速排序由C. A. R. Hoare在1962年提出.它的基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其 ...
- inode-软链接与硬链接
一.inode是什么?理解inode,要从文件储存说起.文件储存在硬盘上,硬盘的最小存储单位叫做"扇区"(Sector).每个扇区储存512字节(相当于0.5KB).操作系统读取硬 ...
- canvas-画蜗牛
<!doctype html><html lang="en"> <head> <meta charset="UTF-8" ...
- 内容观察者 ContentObserver 监听短信、通话记录数据库 挂断来电
Activity public class MainActivity extends ListActivity { private TextView tv_info; private ...
- Android常见开源解决方案
原文:http://m.pstatp.com/group/6348269082899497218/?iid=6036708044&app=news_article&tt_from=mo ...
- Linux 开机报 or type Control-D to continue
解决步骤: 1.输入root密码 2.看是哪个盘报的错,我这边是sda3(可能会是不同的盘),就是代码中标为FAIL 输入以下命令fsck -y /dev/sda3