题目描述

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. LeetCode OJ——Word Ladder2

    http://oj.leetcode.com/problems/word-ladder-ii/ class Solution { public: vector<vector<string& ...

  2. AC日记——传纸条 洛谷 P1006

    题目描述 小渊和小轩是好朋友也是同班同学,他们在一起总有谈不完的话题.一次素质拓展活动中,班上同学安排做成一个m行n列的矩阵,而小渊和小轩被安排在矩阵对角线的两端,因此,他们就无法直接交谈了.幸运的是 ...

  3. kd树的构造与搜索

    学习了两篇博客,存下来以免丢失. http://blog.csdn.net/losteng/article/details/50893739 https://leileiluoluo.com/post ...

  4. Codeforces 932 B.Recursive Queries-前缀和 (ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined))

    B. Recursive Queries   time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  5. Codeforces Gym101502 K.Malek and Summer Semester

    K. Malek and Summer Semester   time limit per test 1.0 s memory limit per test 256 MB input standard ...

  6. 洛谷—— P2183 巧克力

    https://www.luogu.org/problemnew/show/P2183 题目描述 佳佳邀请了M个同学到家里玩.为了招待客人,她需要将巧克力分给她的好朋友们.她有N(1<=N< ...

  7. Codeforces Gym 100431B Binary Search 搜索+组合数学+高精度

    原题链接:http://codeforces.com/gym/100431/attachments/download/2421/20092010-winter-petrozavodsk-camp-an ...

  8. mac下 JMeter 4.0 进行多用户接口压力测试

    1.最近在做公司的内部系统,需要进行多用户压力测试,于是上网在官网下载了Jmeter 压缩包,并放在指定的目录解压,打开解压后文件夹到bin目录下: 执行sh jmeter  Jmeter就启动起来了 ...

  9. dtrace 网站

    Oracle SQL Tuning and CBO Internals: Based Optimizer with CBO Internals and SQL Tuning Optimization ...

  10. tar命令中的-C作用

    一直不知道解压命令如何指定文件夹,今天学到了一个 -C 参数 tar zxvf test.tar.gz -C test 注释:上面的命令将 test.tar.gz 这个压缩包解压到当前目录下的 tes ...