Milking Time poj——3616(dp)

Milking Time
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 17585 Accepted: 7546
Description

Bessie is such a hard-working cow. In fact, she is 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 that she produces as much milk as possible.

Farmer John has a list of M (1 ≤ M ≤ 1,000) possibly overlapping intervals in which 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 that he can get out of 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 be milked through an entire interval.

Even Bessie has her limitations, though. After being milked during any interval, she must rest R (1 ≤ R ≤ N) 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_houri , ending_houri , and efficiencyi

Output

  • Line 1: The maximum number of gallons of milk that Bessie can product in the N hours

Sample Input

12 4 2
1 2 8
10 12 19
3 6 24
7 10 31
Sample Output

43

The meaning of problems

m tasks, known start and end time (less than n-), this time to produce the amount. Must perform a task from start to finish, and executing the tasks required to break a r time interval. The maximum number of times within Q n produces and.

Explanation

Sorted by start time for the first task, dp [i] .v within the maximum number of time intervals from 0 to complete the representative implementation of task i.
Transfer equation
dp [i] .v = max ( dp [i] .v, dp [j] .v + dp [j] .eff); j 0 ~ i-1 belongs to
note dp [i] .v Initial value dp [i] .eff.
ans take dp [] .v maximum

#include <cstdio>
#include <algorithm>
int const maxn = 1e3 + 2;
using namespace std;
struct node{
	int start, end;
	int eff;
	int v;
}dp[maxn];
int cmp(node a, node b){
	return a.start < b.start;
}
int main(){
	int n, m, r, i, j, ans = 0;
	scanf("%d %d %d", &n, &m, &r);
	for (i = 0; i < m; i++)
		scanf("%d %d %d", &dp[i].start, &dp[i].end, &dp[i].eff);
	sort(dp, dp + m, cmp);
	for (i = 0; i < m; i++){
		dp[i].v = dp[i].eff;
		for (j = 0; j < i; j++){
			if(dp[j].end + r <= dp[i].start)
			dp[i].v = max(dp[i].v, dp[j].v+ dp[i].eff);
		}
		ans = max(ans, dp[i].v);
	}
	printf("%d", ans); 
	return 0;
}
Published 52 original articles · won praise 2 · Views 874

Guess you like

Origin blog.csdn.net/qq_44714572/article/details/103000154