Milking Time (POJ 3616 simple DP)

Source: Internet
Author: User

Language:DefaultMilking time
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5290 Accepted: 2183

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≤& nbsp 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

Test instructions: Give a time length n,m work time period and each time period can complete the work, can only do one job and once started to do it, require the choice of the two working time between the interval of at least R time (in the middle need to rest) to choose those work n time to complete the maximum workload. The maximum output value.

Idea: first by the end time of the work from small to large sequencing, and then dynamic planning. Dp[i] Indicates the maximum value obtained from starting from scratch to paragraph I. In a double loop, update dp[i] If the end time of a segment before paragraph I is added to the start time of paragraph I, if R is less than or equal to.

Code:

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < cmath> #include <string> #include <map> #include <stack> #include <vector> #include <set > #include <queue> #pragma comment (linker, "/stack:102400000,102400000") #define MAXN 1005#define MAXN 2005# Define mod 1000000009#define INF 0x3f3f3f3f#define pi ACOs ( -1.0) #define EPS 1e-6#define Lson rt<<1,l,mid#define RSO N rt<<1|1,mid+1,r#define FRE (i,a,b) for (i = A, I <= b; i++) #define FRL (i,a,b) for (i = A; I < b; i++) #define Mem (T, v) memset ((t), V, sizeof (t)) #define SF (n) scanf ("%d", &n) #define SFF (A, b) scanf ("%d%d", &a, & AMP;B) #define SFFF (a,b,c) scanf ("%d%d%d", &a, &b, &c) #define PF printf#define DBG pf ("hi\n" ) typedef long Long ll;using namespace std;struct st{int s,t,w;} St[maxn];int dp[maxn];int n,m,r;int cmp (St a,st b) {return a.t<b.t;}    int main () {int i,j; while (~SFFF (N,m,r)) {FRL (i,0,m) sfff (ST[I].S,ST[I].T,ST[I].W);        Sort (st,st+m,cmp);        int ans=-1;            FRL (i,0,m) {DP[I]=ST[I].W;            FRL (j,0,i) {if (ST[J].T+R&LT;=ST[I].S) Dp[i]=max (DP[I],DP[J]+ST[I].W);        } ans=max (Ans,dp[i]);    } PF ("%d\n", ans); } return 0;}



Milking Time (POJ 3616 simple DP)

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.