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.node
約翰的奶牛們從小養尊處優,她們沒法容忍牛棚裏的任何髒東西.約翰發現,若是要使這羣有潔癖的奶牛滿意,他不得不僱傭她們中的一些來清掃牛棚, 約翰的奶牛中有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秒.約翰一旦決定僱傭某一頭奶牛,就必須付給她全額的工資,而不能只讓她工做一段時間,而後再按這段時間在她願意工做的總時間中所佔的百分比來決定她的工資.如今請你幫約翰決定該僱傭哪些奶牛以保持牛棚的清潔,固然,在能讓奶牛們滿意的前提下,約翰但願使總花費盡可能小.ios
輸入格式:數組
第1行:3個正整數N,M,E,用空格隔開.app
第2到N+1行:第i+l行給出了編號爲i的奶牛的工做計劃,即3個用空格隔開的正整數Ti,T2,S.優化
輸出格式:ui
輸出一個整數,表示約翰須要爲牛棚清理工做支付的最少費用.若是清理工做不可能完成,那麼輸出-1.spa
輸入樣例#1: 複製code
3 0 4
0 2 3
3 4 2
0 0 1blog
輸出樣例#1: 複製排序
5
約翰有3頭牛,牛棚在第0秒到第4秒之間須要打掃.第1頭牛想要在第0,1,2秒內工做,爲此她要求的報酬是3美圓.其他的依此類推. 約翰僱傭前兩頭牛清掃牛棚,能夠只花5美圓就完成一成天的清掃.
線段樹優化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.
代碼以下
#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; }