"POJ 3616" milking time

Source: Internet
Author: User

Milking time
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6129 Accepted: 2571

Description

Bessie is such a hard-working cow.  In fact, she's so focused on maximizing her productivity that she decides to schedule her next N (1≤ n ≤1,000,000) hours (conveniently labeled 0.. N-1) So, she produces as much milk as possible.

Farmer John has a list of , M   (1≤  m  ≤1,000) possibly overlapping intervals in WHI Ch He is available for milking. Each interval : i  has a starting hour (0≤  starting_houri  ≤  N ), an Ending hour ( starting_houri  < ending_houri  ≤  N ), and a corresponding Efficiency (1≤  efficiencyi  ≤1,000,000) which indicates how many gallons of milk so he can get out O F Bessie in that interval. Farmer John starts and stops milking at the beginning of the starting hour and ending hour, respectively. When being milked, Bessie must is milked through an entire interval.

Even Bessie had her limitations, though. After being milked during any interval, she must rest R (1≤ RN) hours before she can start Milking again. Given Farmer Johns List of intervals, determine the maximum amount of milk that Bessie can produce in the N hours .

Input

* Line 1:three space-separated integers: N, M, and R
* Lines 2. M+1:line i+1 describes FJ ' s ith milking interval withthree space-separated integers: starting_hour I , Ending_houri , and efficiencyi

Output

* Line 1:the Maximum number of gallons of milk which Bessie can product in the N hours

Sample Input

12 4 21 2 810 12 193 6 247 10 31

Sample Output

43

Source

Usaco November Silver Simple DP question. The first thing to do is sort by end time. Then we begin to establish the state transition equation. F[i] represents the maximum value of milking during the milking stage at the end time of I. Then obviously, the equation is: f[i] = Max{f[j] | 0 <= J <= end time corresponding to the start time-r} + end time corresponding to the number of milk, if the start time-R is less than 0, directly take f[0] (that is, 0). Pure DP time complexity is O (NM), n is 106,m is 1000, will be dead ... Well, now look at the DP equation. We're just looking for the maximum value in the 1~ start time-R range. So very simple, segment tree optimization. Update on the update i~i this range, no need I say more.
#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;Const intMAXN =1000005;Const intMAXM =1005;structmilk{intS, E, p; BOOL operator< (ConstMilk &b)Const    {        return(E < B.E) | | (E = = B.E && s <B.S); }}MILK[MAXM];intMAXV[MAXN <<2];intF[MAXN];intN, M, R, ans;int_max, y1, y2;voidQueryintOintLintR) {    if(Y1 <= L && R <=y2) {_max=Max (_max, Maxv[o]); return; }    intMid = (L + R) >>1; intLC = O <<1, rc = LC +1; if(Mid >=y1) query (LC, L, mid); if(Mid +1<= y2) query (RC, Mid +1, R);}voidUpdateintOintLintRintVintDelx) {    if(L = =R) {Maxv[o]=Max (Maxv[o], delx); return; }    intMid = (L + R) >>1; intLC = O <<1, rc = LC +1; if(Mid >=v) Update (LC, L, Mid, V, DELX); if(Mid +1<= v) Update (RC, Mid +1, R, V, DELX); Maxv[o]=Max (MAXV[LC], MAXV[RC]);}intMain () {scanf ("%d%d%d", &n, &m, &R);  for(inti =0; I < m; ++i) scanf ("%d%d%d", &milk[i].s, &AMP;MILK[I].E, &MILK[I].P); Sort (milk, milk+m); memset (MAXV,0,sizeof(MAXV)); Memset (F,0,sizeof(f));  for(inti =0; I < m; ++i) {_max=0; if(Milk[i].s-r >0) {y1=1; Y2= Milk[i].s-R; Query (1,1N1); } F[MILK[I].E]= Max (F[MILK[I].E], _max +MILK[I].P); Update (1,1N1, MILK[I].E, F[MILK[I].E]); } ans=0;  for(inti =1; i < MAXN; ++i) ans =Max (ans, f[i]); printf ("%d\n", ans); return 0; }

"POJ 3616" milking time

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.