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.

    第1行:3个正整数N,M,E,用空格隔开.
    第2到N+1行:第i+l行给出了编号为i的奶牛的工作计划,即3个用空格隔开的正整数Ti,T2,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.

    输出一个整数,表示约翰需要为牛棚清理工作支付的最少费用.如果清理工作不可能完成,
那么输出-1.

Sample Input

3 0 4 //三头牛,要打扫从0到4号stall
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

5

HINT

约翰有3头牛,牛棚在第0秒到第4秒之间需要打扫.第1头牛想要在第0,1,2秒内工作,为此她要求的报酬是3美元.其余的依此类推.    约翰雇佣前两头牛清扫牛棚,可以只花5美元就完成一整天的清扫.

Source

Silver

题解:

这道题不错,线段树优化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 清理牛棚的更多相关文章

  1. [BZOJ1672][Usaco2005 Dec]Cleaning Shifts 清理牛棚 线段树优化DP

    链接 题意:给你一些区间,每个区间都有一个花费,求覆盖区间 \([S,T]\) 的最小花费 题解 先将区间排序 设 \(f[i]\) 表示决策到第 \(i\) 个区间,覆盖满 \(S\dots R[i ...

  2. BZOJ 1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚

    题目 1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚 Time Limit: 5 Sec  Memory Limit: 64 MB Description Farm ...

  3. BZOJ_1672_[Usaco2005 Dec]Cleaning Shifts 清理牛棚_动态规划+线段树

    BZOJ_1672_[Usaco2005 Dec]Cleaning Shifts 清理牛棚_动态规划+线段树 题意:  约翰的奶牛们从小娇生惯养,她们无法容忍牛棚里的任何脏东西.约翰发现,如果要使这群 ...

  4. P4644 [Usaco2005 Dec]Cleaning Shifts 清理牛棚

    P4644 [Usaco2005 Dec]Cleaning Shifts 清理牛棚 你有一段区间需要被覆盖(长度 <= 86,399) 现有 \(n \leq 10000\) 段小线段, 每段可 ...

  5. [Usaco2005 Dec]Cleaning Shifts 清理牛棚 (DP优化/线段树)

    [Usaco2005 Dec] Cleaning Shifts 清理牛棚 题目描述 Farmer John's cows, pampered since birth, have reached new ...

  6. 【BZOJ1672】[Usaco2005 Dec]Cleaning Shifts 清理牛棚 动态规划

    [BZOJ1672][Usaco2005 Dec]Cleaning Shifts Description Farmer John's cows, pampered since birth, have ...

  7. 洛谷P4644 [USACO2005 Dec]Cleaning Shifts 清理牛棚 [DP,数据结构优化]

    题目传送门 清理牛棚 题目描述 Farmer John's cows, pampered since birth, have reached new heights of fastidiousness ...

  8. 【bzoj1672】[USACO2005 Dec]Cleaning Shifts 清理牛棚

    题目描述 Farmer John's cows, pampered since birth, have reached new heights of fastidiousness. They now ...

  9. 【bzoj1672】[USACO2005 Dec]Cleaning Shifts 清理牛棚 dp/线段树

    题目描述 Farmer John's cows, pampered since birth, have reached new heights of fastidiousness. They now ...

随机推荐

  1. Codeforces Round #300 F - A Heap of Heaps (树状数组 OR 差分)

    F. A Heap of Heaps time limit per test 3 seconds memory limit per test 512 megabytes input standard ...

  2. redis 网络流程图 <一>

    本来一直想好好读下redis源码.可是每次读了一点就不读了.  主要是没坚持每天都读. 隔几天看.就忘记前面的流程.就越来越不想看了.  很是蛋疼.这个还是要坚持读完的.打算这段时间都源码的时候.都大 ...

  3. CSS(三)

    CSS又上完了,真是快!!! 预习了JS的一部分,写了几条简单的JS代码: 1.成绩判定: <!DOCTYPE html> <html lang="en"> ...

  4. PHP中字符串类型与数值类型混合计算

    字符串转数值的规则 当一个字符串被当作一个数值来取值,其结果和类型如下: 如果该字符串没有包含 '.','e' 或 'E' 并且其数字值在整型的范围之内(由 PHP_INT_MAX 所定义),该字符串 ...

  5. Java学习之List接口

    List接口 List接口的定义如下: public interface List<E>extends Collection<E> 可以发现List接口时Collection接 ...

  6. Java和C#中String直接赋值与使用new创建(==与equals进行比较)的区别

    在Java中,字符串可以直接赋值或者使用new来新建,直接赋值的话是编译阶段(.class文件)中就将该字符串值放到常量池中,以后如果有其他变量直接赋予同样的值的话就不再分配内存空间,而是直接给它个引 ...

  7. gcc -L -l的使用

    -l参数就是用来指定程序要链接的库,-l参数紧接着就是库名,那么库名跟真正的库文件名有什么关系呢?就拿数学库来说,他的库名是m,他的库文件名是libm.so,很容易看出,把库文件名的头lib和尾.so ...

  8. css优先级计算

    主要的css选择器有id,class,tag,[],:,::等,而通常需要对其优先级进行判断的有id,class,tag,另外内联样式和!important也和css的优先级有关系. 如果将这五种不同 ...

  9. 3.1,pandas【基本功能】

    一:改变索引 reindex方法对于Series直接索引,对于DataFrame既可以改变行索引,也可以改变列索引,还可以两个一起改变. 1)对于Series In [2]: seri = pd.Se ...

  10. Function Currying in javascript 的一些注释

    理解函数柯里化(Function Currying ),最关键的是理解下面这个函数: function curry(fn){ var args = Array.prototype.slice.call ...