POJ3616(KB12-R dp)

Milking Time

Time Limit: 1000MS    Memory Limit: 65536K

Total Submissions: 9459  Accepted: 3935

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: NM, 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

有将近一个月没做题,练一下手感。

 1 //2017-05-16
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <iostream>
 5 #include <algorithm>
 6
 7 using namespace std;
 8
 9 const int M = 1005;
10 int dp[M];
11 struct Milk
12 {
13     int bg, ed, eff;
14     bool operator<(Milk x) const{
15         return ed < x.ed;
16     }
17 }milk[M];
18
19 int main()
20 {
21     int n, m, r;
22     while(scanf("%d%d%d", &n, &m, &r)!=EOF)
23     {
24         memset(dp, 0, sizeof(dp));
25         for(int i = 0; i < m; i++){
26             scanf("%d%d%d", &milk[i].bg, &milk[i].ed, &milk[i].eff);
27         }
28         sort(milk, milk+m);
29         int ans = 0;
30         for(int i = 0; i < m; i++){
31             if(milk[i].ed > n)break;
32             dp[i] = milk[i].eff;
33             for(int j = 0; j < i; j++){
34                 if(milk[i].bg-r >= milk[j].ed){
35                     dp[i] = max(dp[i], dp[j]+milk[i].eff);//dp[i]表示到第i个区间的最大值
36                 }else break;
37             }
38             if(dp[i] > ans)ans = dp[i];
39         }
40         printf("%d\n", ans);
41     }
42
43     return 0;
44 }
时间: 2024-10-11 14:28:20

POJ3616(KB12-R dp)的相关文章

POJ3616 Milking Time 【DP】

Milking Time Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4917   Accepted: 2062 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 ≤

CF149D. Coloring Brackets[区间DP !]

不知道为什么居中了,先把代码放这 #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; const int N=705,MOD=1e9+7; char s[N]; long long n,f[N][N][5][5]; int st[N],top=0,m[N]; void match(){ for(int i=1;i<=

矩形嵌套-记忆化搜索(dp动态规划)

矩形嵌套 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描写叙述 有n个矩形,每个矩形能够用a,b来描写叙述,表示长和宽. 矩形X(a,b)能够嵌套在矩形Y(c,d)中当且仅当a<c,b<d或者b<c,a<d(相当于旋转X90度).比如(1,5)能够嵌套在(6,2)内,但不能嵌套在(3,4)中. 你的任务是选出尽可能多的矩形排成一行,使得除最后一个外,每个矩形都能够嵌套在下一个矩形内. 输入 第一行是一个正正数N(0<N<10).表示測试数据组

基础dp

队友的建议,让我去学一学kuangbin的基础dp,在这里小小的整理总结一下吧. 首先我感觉自己还远远不够称为一个dp选手,一是这些题目还远不够,二是定义状态的经验不足.不过这些题目让我在一定程度上加深了对dp的理解,但要想搞好dp,还需要多多练习啊. HDU - 1024 开场高能 给出一个数列,分成m段,求这m段的和的最大值,dp[i][j]表示遍历到第i个数,已经划分了j段,对于每一个数有两种决策,加入上一个区间段,自己成为一个区间段,故dp[i][j] = max(dp[i-1][j]+

bzoj-4518 4518: [Sdoi2016]征途(斜率优化dp)

题目链接: 4518: [Sdoi2016]征途 Description Pine开始了从S地到T地的征途. 从S地到T地的路可以划分成n段,相邻两段路的分界点设有休息站. Pine计划用m天到达T地.除第m天外,每一天晚上Pine都必须在休息站过夜.所以,一段路必须在同一天中走完. Pine希望每一天走的路长度尽可能相近,所以他希望每一天走的路的长度的方差尽可能小. 帮助Pine求出最小方差是多少. 设方差是v,可以证明,v×m^2是一个整数.为了避免精度误差,输出结果时输出v×m^2. In

lightoj1025_区间dp

题目链接:http://lightoj.com/volume_showproblem.php?problem=1025 题目描述: 给出一个字符串,可以任意删除位置的字符,也可以删除任意多个.问能组成多少个回文串? 解题思路: 自从开始学dp,感觉自己智商一直处于离线状态.席八啊啊啊啊啊啊!今天随机到这个题目,看了好久竟然没有看出来是区间DP.知道是区间DP后立马感觉明白. 情景设定 dp[l][r] 表示 区间 [l, r] 内的回文串数目. 状态转移:dp[l][r] = dp[l][r-1

HDU 5115 Dire Wolf 区间DP

Dire Wolf Problem Description Dire wolves, also known as Dark wolves, are extraordinarily large and powerful wolves. Many, if not all, Dire Wolves appear to originate from Draenor.Dire wolves look like normal wolves, but these creatures are of nearly

【BZOJ-1492】货币兑换Cash DP + 斜率优化 + CDQ分治

1492: [NOI2007]货币兑换Cash Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 3396  Solved: 1434[Submit][Status][Discuss] Description 小Y最近在一家金券交易所工作.该金券交易所只发行交易两种金券:A纪念券(以下简称A券)和 B纪念券(以下 简称B券).每个持有金券的顾客都有一个自己的帐户.金券的数目可以是一个实数.每天随着市场的起伏波动, 两种金券都有自己当时的价值,即每一单位金

BZOJ 1040: [ZJOI2008]骑士( 树形dp )

这是一个森林中, 每棵树上都有一个环...每棵树单独处理, 找出环上任意一条边断开, 限制一下这条边两端点的情况, 然后就可以树dp了.. ------------------------------------------------------------------------ #include<cstdio> #include<algorithm> #include<cstring> #include<cctype> using namespace

数位DP小结

如果以下错的地方,谢谢提出. 1.HDU - 2089 不要62 解题思路:这题的限制条件是不能出现4,和数字中不能包含62,那么就对这两个进行特判即可 #include<cstdio> #include<algorithm> #include<cstring> using namespace std; #define N 12 int n, m; int dp[N][N]; int data[N]; //zero_flag是前导零的标记,为0时表示有前导0 //bor