poj 3616 Milking Time(dp)

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

Source

USACO 2007 November Silver

dp,注意初始化,

for(int i=1;i<=m;i++){
dp[i]=cows[i].c;
}

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<queue>
 5 #include<stdlib.h>
 6 using namespace std;
 7 #define N 1000006
 8 #define M 1006
 9 int n,m,r;
10 struct Node{
11     int s,e;
12     int c;
13 }cows[M];
14 bool cmp(Node a,Node b){
15     if(a.s!=b.s)
16       return a.s<b.s;
17      return a.e<b.e;
18 }
19 int dp[M];//dp[i]表示取到第i段时间段时的最大值
20 int main()
21 {
22     while(scanf("%d%d%d",&n,&m,&r)==3){
23         for(int i=1;i<=m;i++){
24             scanf("%d%d%d",&cows[i].s,&cows[i].e,&cows[i].c);
25         }
26         sort(cows+1,cows+m+1,cmp);
27         memset(dp,0,sizeof(dp));
28         for(int i=1;i<=m;i++){
29             dp[i]=cows[i].c;
30         }
31         //dp[1]=cows[1].c;
32         int ans=0;
33         for(int i=1;i<=m;i++){
34             for(int j=1;j<i;j++){
35                 if(cows[i].s>=cows[j].e+r){
36                     dp[i]=max(dp[i],dp[j]+cows[i].c);
37                 }
38             }
39             ans=max(dp[i],ans);
40         //    printf("---%d\n",ans);
41         }
42         printf("%d\n",ans);
43     }
44     return 0;
45 }

时间: 2024-10-21 19:47:50

poj 3616 Milking Time(dp)的相关文章

POJ 2948 Martian Mining(DP)

题目链接 题意 : n×m的矩阵,每个格子中有两种矿石,第一种矿石的的收集站在最北,第二种矿石的收集站在最西,需要在格子上安装南向北的或东向西的传送带,但是每个格子中只能装一种传送带,求最多能采多少矿. 思路 :记忆化搜索.也可以用递推. //2948 #include <stdio.h> #include <string.h> #include <iostream> using namespace std ; int yeye[510][510] ,blog[510]

【POJ 3071】 Football(DP)

[POJ 3071] Football(DP) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4350   Accepted: 2222 Description Consider a single-elimination football tournament involving 2n teams, denoted 1, 2, -, 2n. In each round of the tournament, all tea

【POJ 3034】 Whac-a-Mole(DP)

[POJ 3034] Whac-a-Mole(DP) Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 3621   Accepted: 1070 Description While visiting a traveling fun fair you suddenly have an urge to break the high score in the Whac-a-Mole game. The goal of the W

POJ 3280 Cheapest Palindrome(DP)

题目链接 题意 :给你一个字符串,让你删除或添加某些字母让这个字符串变成回文串,删除或添加某个字母要付出相应的代价,问你变成回文所需要的最小的代价是多少. 思路 :DP[i][j]代表的是 i 到 j 这一段位置变成回文所需的最小的代价. 1 //3280 2 #include <stdio.h> 3 #include <string.h> 4 #include <iostream> 5 6 using namespace std ; 7 8 char sh[2100]

POJ 3616 Milking Time(加掩饰的LIS)

传送门: http://poj.org/problem?id=3616 Milking Time Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13406   Accepted: 5655 Description Bessie is such a hard-working cow. In fact, she is so focused on maximizing her productivity that she dec

POJ 3616 Milking Time(最大递增子序列变形)

题目链接:http://poj.org/problem?id=3616 题目大意:给你时间N,还有M个区间每个区间a[i]都有开始时间.结束时间.生产效率(时间都不超过N),只能在给出的时间段内生产,要求合理安排时间求出最大生产价值. 解题思路:把区间按开始时间排序,于是有状态转移方程:dp[i]=max(dp[i],dp[j]+a[i].val)(前提是a[j].end+r<=a[i].start,i是区间的序号,j是i前面的区间) 相当于最大递增子序列的变形,写法差不多. 代码: 1 #in

poj - 1093 - Formatting Text(dp)

题意:输入一段短文(所有字符总数不超过10000),要求格式化成两端对齐(每行长度为n,1 <= n <= 80)的方式输出并使得总坏值最小(一个空隙的坏值是这个空隙的空格总数减1后的平方),若有多种方案输出空格数字典序最小方案. 题目链接:http://poj.org/problem?id=1093 -->>状态:dp[i]表示从第i个单词开始到最后一个单词的最小总坏值(第i个单词是这一行的第1个单词) 状态转移方程:dp[i] = min(dp[i], dp[j + 1] +

POJ 3616 Milking Time 简单DP

题目链接:http://poj.org/problem?id=3616 题目大意:M个区间,每个区间一个对应一个效率值-多少升牛奶,区间可能重复,现要求取出来一些区间,要求是区间间隔不能小于R,问所能得到的牛奶量的最大值. 解题思路:决策:当前区间用或者不用.区间个数M≤1000,因此直接双循环递推即可. dp[i]:=选第i个区间情况下前i个区间能获得的牛奶最大值 dp[i] = max(dp[i], dp[j] + a[i].eff) a[j].end + r <= a[i].start 代

POJ 1260:Pearls(DP)

http://poj.org/problem?id=1260 Pearls Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8474   Accepted: 4236 Description In Pearlania everybody is fond of pearls. One company, called The Royal Pearl, produces a lot of jewelry with pearls