http://poj.org/problem?id=3616
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.
题解:先按照结束时间将m个时间段排序,在找到每个时间段之前与其不冲突的时间段,储存在p[]数组中(p[i]表示在i段之前与i不冲突的最大时间段)。那样可以得到递推关系式dp[i]=max(dp[i-1],dp[p[i]]+intre[i].w)
http://www.hankcs.com/program/cpp/poj-3616-milking-time.html
2.3 记录结果再利用的“动态规划” 基础的动态规划算法
奶牛Bessie在0~N时间段产奶。农夫约翰有M个时间段可以挤奶,时间段f,t内Bessie能挤到的牛奶量e。奶牛产奶后需要休息R小时才能继续下一次产奶,求Bessie最大的挤奶量。
http://blog.csdn.net/u014688145/article/details/71091333
static int[] dp = new int[MAX_M]; public static void main(String[] args) { Scanner in = new Scanner(System.in); int N = in.nextInt(); int M = in.nextInt(); int R = in.nextInt(); Interval[] intervals = new Interval[M]; for (int i = 0; i < M; i++){ int start = in.nextInt(); int end = in.nextInt(); int eff = in.nextInt(); intervals[i] = new Interval(start, end, eff); } Arrays.sort(intervals, new Comparator<Interval>() { @Override public int compare(Interval o1, Interval o2) { return o1.start != o2.start ? o1.start-o2.start : o1.end - o2.end; } }); for (int i = 0; i < M; i++){ dp[i] = intervals[i].efficiency; for (int j = 0; j < i; j++){ if (intervals[j].end + R <= intervals[i].start){ dp[i] = Math.max(dp[i],intervals[i].efficiency+dp[j]); } } } int max = 0; for (int i = 0; i < M; i++){ max = Math.max(max, dp[i]); } System.out.println(max); in.close(); } } class Interval{ int start; int end; int efficiency; Interval(int start,int end,int efficiency){ this.start = start; this.end = end; this.efficiency = efficiency; } }
Read full article from 3616 -- Milking Time
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
* 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
题解:先按照结束时间将m个时间段排序,在找到每个时间段之前与其不冲突的时间段,储存在p[]数组中(p[i]表示在i段之前与i不冲突的最大时间段)。那样可以得到递推关系式dp[i]=max(dp[i-1],dp[p[i]]+intre[i].w)
http://www.hankcs.com/program/cpp/poj-3616-milking-time.html
2.3 记录结果再利用的“动态规划” 基础的动态规划算法
奶牛Bessie在0~N时间段产奶。农夫约翰有M个时间段可以挤奶,时间段f,t内Bessie能挤到的牛奶量e。奶牛产奶后需要休息R小时才能继续下一次产奶,求Bessie最大的挤奶量。
定义dp[i]表示第i个时间段挤奶能够得到的最大值,拆开来说,就是前面 i – 1个时间段任取0到i – 1个时间段挤奶,然后加上这个时间段(i)的产奶量之和。dp[i]满足如下递推关系:
第i个时间段挤奶的最大值 = 前 i – 1 个时间段挤奶最大值中的最大值 + 第i次产奶量。
注意此处的第i个时间段不等同于第i次。
最大值产生在生成过程当中,递推式相当简单。
- 1
- 1
但该递推式需要符合
static int MAX_M = 1000;j.endTime + R >= i.startTime
,代码如下:static int[] dp = new int[MAX_M]; public static void main(String[] args) { Scanner in = new Scanner(System.in); int N = in.nextInt(); int M = in.nextInt(); int R = in.nextInt(); Interval[] intervals = new Interval[M]; for (int i = 0; i < M; i++){ int start = in.nextInt(); int end = in.nextInt(); int eff = in.nextInt(); intervals[i] = new Interval(start, end, eff); } Arrays.sort(intervals, new Comparator<Interval>() { @Override public int compare(Interval o1, Interval o2) { return o1.start != o2.start ? o1.start-o2.start : o1.end - o2.end; } }); for (int i = 0; i < M; i++){ dp[i] = intervals[i].efficiency; for (int j = 0; j < i; j++){ if (intervals[j].end + R <= intervals[i].start){ dp[i] = Math.max(dp[i],intervals[i].efficiency+dp[j]); } } } int max = 0; for (int i = 0; i < M; i++){ max = Math.max(max, dp[i]); } System.out.println(max); in.close(); } } class Interval{ int start; int end; int efficiency; Interval(int start,int end,int efficiency){ this.start = start; this.end = end; this.efficiency = efficiency; } }
struct
Interval
{
int
starting_hour, ending_hour, efficiency;
bool
operator < (
const
Interval& i)
const
{
return
starting_hour < i.starting_hour;
}
};
Interval interval[1024];
int
dp[1024];
///////////////////////////SubMain//////////////////////////////////
int
main(
int
argc,
char
*argv[])
{
#ifndef ONLINE_JUDGE
freopen
(
"in.txt"
,
"r"
, stdin);
freopen
(
"out.txt"
,
"w"
, stdout);
#endif
int
N, M, R;
cin >> N >> M >> R;
for
(
int
i = 0; i < M; ++i)
{
cin >> interval[i].starting_hour >> interval[i].ending_hour >> interval[i].efficiency;
// 实际的结束时间还需要加上休息时间
interval[i].ending_hour += R;
}
sort(interval, interval + M);
for
(
int
i = 0; i < M; ++i)
{
dp[i] = interval[i].efficiency;
for
(
int
j = 0; j < i; ++j)
{
if
(interval[j].ending_hour <= interval[i].starting_hour)
{
dp[i] = max(dp[i], dp[j] + interval[i].efficiency);
}
}
}
cout << *max_element(dp, dp + M) << endl;
#ifndef ONLINE_JUDGE
fclose
(stdin);
fclose
(stdout);
system
(
"out.txt"
);
#endif
return
0;
}