[Usaco2005 Dec]Cleaning Shifts 清理牛棚

题目描述

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/)。\]

posted @ 2018-09-03 21:38  k-z-j  阅读(288)  评论(0编辑  收藏  举报