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 ...
随机推荐
- HDU5125--magic balls(LIS)
题意:求a数组的LIS,但是加了一个条件,为了LIS最大 b[i] a[i]可以交换.最多交换mci: 赤果果的dp啊,可是这个题用线段树的话却会TLE,,由于查询的只是1-x的最大值 因此我们可以用 ...
- 使用python进行接口测试(二)
之前使用过urllib和urllib2做接口测试,在做的途中,感觉使用urllib2直接进行的get,post 请求并没有那么好用.作为测试人员,所需要的测试工具应当以方便为第一要务,测试的耗时只要是 ...
- hadoop-2.6.0为分布式安装
hadoop-2.6.0为分布式安装 伪分布模式集群规划(单节点)------------------------------------------------------------------- ...
- javaweb笔记2之HTTP协议
1 什么是http协议 是浏览器客户端 和 服务器端 数据传输的 格式规范: 2 查看http协议 (1)用火狐的firebug插件查看 (2)使用谷歌的Ghome查看(审查元素-> ...
- ASIHTTPRequest使用指南---<<翻译稿>>
ASIHTTPRequest使用指南---<<翻译稿>> 当第一次使用ASIHTTPRequest进行http请求时,会出现非常多的bug提示.查了一些资料,发现在少倒入了几个 ...
- c++大作业--学籍管理系统--
1.题目描写叙述 学籍管理系统: 依据信息管理系统的业务流程.要求以及所要实现的目标,完毕下面功能: (1)建立学生档案的管理和维护.实现计算机自己主动化管理体制. (2)建立学生成绩管理机制,在计算 ...
- android 判断是否有sim卡及运营商
判断是否有sim卡的方法: int absent = TelephonyManager.SIM_STATE_ABSENT; if (1 == absent) { Log.d(TAG,"请 ...
- NumberSpinner( 数字微调) 组件
本节课重点了解 EasyUI 中 Spinner(微调)组件的使用方法,这个组件依赖于Numberbox(数值输入框)和 Spinner(微调)组件. 一. 加载方式//class 加载方式<i ...
- javascript获取标签样式(获取背景为例)
function getStyle(el){ if(window.getComputedStyle){ return window.getComputedStyle(el,null); } retur ...
- Lesson 1: What is design? Why is it important?
Week 2: What is design? Why is it important? Article 1: Startups, this is how design works. It's a s ...