Cleaning Shifts
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 4422 |
|
Accepted: 1482 |
Description
Farmer John ' s cows, pampered since birth, has reached new heights of fastidiousness. They now require their barn to be immaculate. Farmer John, the most obliging of farmers, have no choice but hire some of the cows to clean the barn.
Farmer John had n (1 <= n <=) cows who is willing to do some cleaning. Because dust falls continuously, the cows require that the farm is continuously cleaned during the workday, which runs fro M second number m to second number E during the day (0 <= m <= E <= 86,399). Note the total number of seconds during which cleaning are to take place is e-m+1. During any given second M.. E, at least one cow must is cleaning.
Each cow have 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 one 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 are covered by at least one cow and which minimizes the total salary T Hat goes to the cows.
Input
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.
Output
Line 1:a A single integer that's either the minimum total salary to get the barn cleaned or else-1 if it's impossible to Clean the barn.
Sample Input
3 0 40 2 33 4 20 0 1
Sample Output
5
Hint
Explanation of the sample:
FJ have three cows, and the barn needs to being cleaned from second 0 to second 4. The first cow is willing to work during seconds 0, 1, and 2 for a total salary of 3, etc.
Farmer John can hire the first and the cows.
Source
Usaco 2005 December Silver
Test instructions
An interval of [L, R] with n cows, each cow can clean up a fixed interval, and it takes a certain price. Now to clean up [l,r] This interval, it takes the minimum price is how much.
Ideas:
Use f[x] to indicate the minimum price to be spent cleaning the interval [L, X].
For a cow, he can clean the interval is [a, b], then because the middle can not be interrupted, so f[b] = min (f[x]) + C where x is the [a-1,b] interval.
Each state transition takes an interval minimum and updates the value of a point so that it is maintained with a segment tree.
First, all the cows are sorted by the end of the range, then DP.
Because the time is starting from 0, so I add 2 to all the time, so A-1 is still starting from 1.
Note that when update () is not directly changing the value to Val, it takes a smaller value here, WA for a while.
1#include <iostream>2#include <Set>3#include <cmath>4#include <stdio.h>5#include <cstring>6#include <algorithm>7 using namespacestd;8typedefLong LongLL;9 #defineINF 0x7f7f7f7fTen One Const intMAXN = 1e5 +5; A Const intMaxTime =86500; - structnode{ - intSt, Ed, cost; the }COW[MAXN]; - BOOLCMP (Node A, Node B) - { - returnA.ed <B.Ed; + } -LL TREE[MAXN <<2];//interval f[] min. + intN, L, R; A at voidPushup (intRT) - { -TREE[RT] = min (tree[rt <<1], Tree[rt <<1|1]); - } - - voidBuildintRtintLintR) in { - if(L = =R) { toTREE[MAXN] =inf; + return; - } the intMid = (L + r)/2; *Build (rt<<1, L, mid); $Build (rt<<1|1, Mid +1, R);Panax Notoginseng pushup (RT); - } the + voidUpdateintX, LL Val,intLintRintRT) A { the if(L = =R) { +TREE[RT] =min (Tree[rt], Val); - return; $ } $ intm = (L + r)/2; - if(x <=m) { -Update (x, Val, L, M, rt<<1); the } - Else{WuyiUpdate (x, Val, M +1, R, rt<<1|1); the } - pushup (RT); Wu } - AboutLL Query (intLintRintLintRintRT) $ { - if(l <= l && R >=R) { - returnTree[rt]; - } A intm = (L + r)/2; +LL ans =inf; the if(L <=m) { -ans = min (ans, query (L, R, L, M, rt<<1)); $ } the if(R >m) { theans = min (ans, query (L, R, M +1, R, rt<<1|1)); the } the pushup (RT); - returnans; in } the the intMain () About { the while(SCANF ("%d%d%d", &n, &l, &r)! =EOF) { thel+=2; r+=2; thememset (Tree,0x7f,sizeof(tree)); + for(inti =1; I <= N; i++){ -scanf"%d%d%d", &cow[i].st, &cow[i].ed, &cow[i].cost); thecow[i].st+=2; cow[i].ed+=2;Bayi } theSort (cow +1, Cow +1+N, CMP); the -Build1,1, R); - theUpdate (L-1,0,1R1); the //cout<< "yes" <<endl; the intFar =L; the BOOLFlag =true; - for(inti =1; I <= N; i++){ the if(Cow[i].st > Far +1){ theFlag =false; the //Break ;94 } the intA = max (L-1, Cow[i].st-1); the intb =min (R, cow[i].ed); the //cout<<a<< "" <<b<<endl;98LL f = Query (A, B,1R1); AboutF + =Cow[i].cost; - //cout<<f<<endl;101Update (b, F,1R1);102Far =Max (FAR, Cow[i].ed);103 //cout<<far<<endl;104 } the //cout<< "yes" <<endl;106 107LL ans = query (R, R,1R1);108 if(Ans >=inf) {109printf"-1\n"); the }111 Else{ theprintf"%lld\n", ans);113 the //else{ the //printf (" -1\n"); the }117 118 }119 -}
poj3171 Cleaning Shifts "segment Tree (single-point modification interval query)" DP "