题目描述

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. ROS学习网址【原创】

    ROS学习网址 http://www.ros.org/ http://www.ros.org/news/book/ http://wiki.ros.org/ http://blog.exbot.net ...

  2. Android之framework修改底部导航栏NavigationBar动态显示和隐藏

     原文链接 http://blog.csdn.net/way_ping_li/article/details/45727335 git diff diff --git a/frameworks/bas ...

  3. chpasswd、dd命令、find实战、添加系统服务、buffer、cached

    1.如果两个文件的每一行想一一对应 paste 1.txt 2.txt # 文件3.txt中存放着用户跟密码,想要添加用户并设置密码: # 用户必须存在,文件格式必须是--用户名:密码 chpassw ...

  4. Parameter Binding in ASP.NET Web API #Reprinted

    http://www.asp.net/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api

  5. Maven更新POM中的JDK版本(比如更新为JDK1.8)

    默认POM如果不指定JDK版本为1.5,而有些项目需要使用泛型这些,就必须使用1.8版本的JDK,所以需要手动修改POM. 而所涉及到的还是插件maven-compiler-plugin,官方参考:h ...

  6. Android--------------几个ADB经常使用命令

    1. 显示当前执行的所有模拟器:     adb devices 2. 安装应用程序:     adb install -r 123.apk 3. 获取模拟器中的文件:     adb pull &l ...

  7. Odoo 运费

    模块delievery可以将运费Charge给客户     安装delivery模块                 Delivery method     在做订单的时候,选择相应的运输方法, 系统 ...

  8. 安装下载MySQL

    下载MySQL的地址:下面两个都行 http://dev.mysql.com/downloads/windows/ http://dev.mysql.com/downloads/installer/5 ...

  9. python(24)- 面向对象进阶

    面向对象基础知识: 1.面向对象是一种编程方式,此编程方式的实现是基于对类和对象的使用: 2.类是一个模板,模板中包装了多个‘函数’供使用(可以将多函数中公用的变量封装到对象中): 3.对象,根据模板 ...

  10. 线程安全使用(四) [.NET] 简单接入微信公众号开发:实现自动回复 [C#]C#中字符串的操作 自行实现比dotcore/dotnet更方便更高性能的对象二进制序列化 自已动手做高性能消息队列 自行实现高性能MVC WebAPI 面试题随笔 字符串反转

    线程安全使用(四)   这是时隔多年第四篇,主要是因为身在东软受内网限制,好多文章就只好发到东软内部网站,懒的发到外面,现在一点点把在东软写的文章给转移出来. 这里主要讲解下CancellationT ...