题目描述

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秒.约翰一旦决定雇佣某一头奶牛,就必须付给她全额的工资,而不能只让她工作一段时间,然后再按这段时间在她愿意工作的总时间中所占的百分比来决定她的工资.现在请你帮约翰决定该雇佣哪些奶牛以保持牛棚的清洁,当然,在能让奶牛们满意的前提下,约翰希望使总花费尽量小.

输入输出格式

输入格式:

  • 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.

输出格式:

  • 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.

输入输出样例

输入样例#1: 复制

3 0 4

0 2 3

3 4 2

0 0 1

输出样例#1: 复制

5

说明

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

Solution

线段树优化DP。

设\(f[x]\)表示\(s~x\)都有牛工作的最小费用。\(s\)表示题目要求的总起始时间,我们考虑一头牛\(i\),先按右端点排序牛,如果牛工作的停止时间为 \(r[i]\),起始时间为\(l[i]\),费用为\(w[i]\),那么我们可以这样转移方程。

\[f[r[i]]=min(f[r[i]],min(f[l[i]-1]~f[r[i]-1])+w[i])$$.
初始化$f[s-1]=0$,其他赋值为正无穷。
因为考虑到,不能O(n)查找最小值,并且还带修改,自然而然就想到线段树了,线段树存区间$f$数组最小值。
**细节:$s,t,l[i],r[i]$可能减1后为负数,线段树会死循环,所以初始值+1.**
代码如下
```cpp
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll(x) (x*2)
#define rr(x) (x*2+1)
using namespace std;
typedef long long lol;
struct Node
{
lol l,r,w;
}a[101000];
lol f[100101],mn[1001010],s,t;
bool cmp(Node x,Node y)
{
return x.r<y.r;
}
void pushup(lol node) {mn[node]=min(mn[ll(node)],mn[rr(node)]);}
void build(lol node,lol l,lol r)
{
if(l==r)
{
mn[node]=2000000000007;
if(l==s-1) mn[node]=0;
return;
}
lol mid=(l+r)/2;
build(ll(node),l,mid);
build(rr(node),mid+1,r);
pushup(node);
}
void gai(lol node,lol l,lol r,lol pos,lol x)
{
if(l==r)
{
mn[node]=x;
return;
}
lol mid=(l+r)/2;
if(pos<=mid) gai(ll(node),l,mid,pos,x);
else gai(rr(node),mid+1,r,pos,x);
pushup(node);
}
lol cha(lol node,lol l,lol r,lol left,lol right)
{
if(l>=left&&r<=right)
return mn[node];
lol mid=(l+r)/2,ans=2e16;
if(left<=mid) ans=min(ans,cha(ll(node),l,mid,left,right));
if(right>mid) ans=min(ans,cha(rr(node),mid+1,r,left,right));
return ans;
}
int main()
{
memset(f,0x3f,sizeof(f));
lol n,mx=-1001000000000,m=100100000001,anss=10100000000000;
cin>>n>>s>>t;
s++,t++;
for(lol i=1;i<=n;i++)
scanf("%lld%lld%lld",&a[i].l,&a[i].r,&a[i].w),a[i].l++,a[i].r++,mx=max(mx,a[i].r),m=min(m,a[i].l);
build(1,m-1,mx);
sort(a+1,a+1+n,cmp);
for(lol i=1;i<=n;i++)
{
if(a[i].r<s||a[i].l>t) continue;
lol k=a[i].r;
f[k]=min(f[k],cha(1,m-1,mx,a[i].l-1,a[i].r-1)+a[i].w);
gai(1,m-1,mx,k,f[k]);
}
for(lol i=t;i<=mx;i++)
anss=min(anss,f[t]);
if(anss>=1e10) cout<<-1;
else cout<<anss;
}
```
### 博主蒟蒻,可以随意转载,但必须附上原文链接[k-z-j](https://www.cnblogs.com/kzj-pwq/)。\]

[Usaco2005 Dec]Cleaning Shifts 清理牛棚的更多相关文章

  1. BZOJ1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚

    1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 414  Solved: ...

  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. 【BZOJ】1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚(dp/线段树)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1672 dp很好想,但是是n^2的..但是可以水过..(5s啊..) 按左端点排序后 f[i]表示取第 ...

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

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

随机推荐

  1. 定位 UNIX 上常见问题的经验总结

    本文主要对 UNIX 平台常见的问题进行了分类,介绍一些常见问题分析时使用的方法和命令,对以下三种常见问题的分析方法做了简单介绍:UNIX 下 Crash 问题的分析方法.UNIX 下内存泄露问题的分 ...

  2. Understanding Linux CPU Load - when should you be worried?

    http://blog.scoutapp.com/articles/2009/07/31/understanding-load-averages

  3. 推荐一款基于 AI 开发的 IDE 插件,帮助提升编码效率

    最近在浏览技术社区的时候,发现了一款神奇 IDE 插件,官网称可以利用 AI 帮助程序员写代码,一下子吸引了我的好奇心.赶紧下载下来使用一番,感觉确实蛮神奇,可以火速提升编程效率. 这款插件叫做 ai ...

  4. 采集网站特殊文件Meta信息

    采集网站特殊文件Meta信息   元(Meta)信息是描述文件的属性的特殊信息,如文件的所有者.联系方式.机构名.邮件地址等信息.而网站中常常会有共享的文档文件,如PDF.Excel.Word.这些文 ...

  5. Java判断语句中判断条件的执行顺序

    if判断里面如果是多个条件的判断,会按照从前往后的顺序执行.基本所有语言都是一致的设计,这样做能提高程序的效率. 比如: if ( conditionA && conditionB ) ...

  6. Maven依赖机制理解

    假设一个项目需要用到日志组件Log4j,那么有如下方式添加这个组件. 一.传统方式: 1.访问官网https://logging.apache.org/log4j/2.x/download.html, ...

  7. 我们为什么要把Dagger2,MVP以及Rxjava引入项目中?

    1Why? 我们为什么要把Dagger2,MVP以及Rxjava引入项目中? 毫无疑问在Android开发圈中这三个技术是经常被提及的,如此多的文章和开源项目在介绍他们,使用他们,开发者也或多或少的被 ...

  8. epoll 浅析以及 nio 中的 Selector

    首先介绍下epoll的基本原理,网上有很多版本,这里选择一个个人觉得相对清晰的讲解(详情见reference): 首先我们来定义流的概念,一个流可以是文件,socket,pipe等等可以进行I/O操作 ...

  9. 开始我的GL离屏渲染绑定[转]

    地址: http://wiki.woodpecker.org.cn/moin/lilin/swig-glBmpContext 呵呵,有了第一次的经验,我们就要开始我们的GL离屏渲染的绑定了. 关 于O ...

  10. 2.SOAP 语法

    SOAP 消息的基本结构 <?xml version="1.0"?> <soap:Envelope xmlns="http://www.w3.org/2 ...