一、题目

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

二、思路&心得

  • 对于”每头奶牛挤奶后需要休息R分钟“的条件限制,直接让每头奶牛的结束时间加上R分钟,来消除这个限制。
  • 定义挤奶的结构体(开始时间、结束时间、获得收益),然后按照结束时间从小到大进行排序。
  • 这题有点像贪心中的区间问题,排完序后,定义dp[i]为:,到dp[i].end这个时间点为止,可以获得的最大收益。显然,dp[i]满足如下递推式:dp[i] = dp[k] + T[i].gollon。(其中k为dp[0 to i - 1]的最大值下标,且T[k].end <= T[i].start)
  • 注意题目不不是在最后一个时间点取到最大值,因此需要记录最大的dp。

三、代码

#include<cstdio>
#include<algorithm>
using namespace std;
const int MAX_M = 1005; int N, M, R; int dp[MAX_M]; struct times {
int start;
int end;
int gollon;
} T[MAX_M]; bool cmp(times a, times b) {
return a.end < b.end;
} int main() {
int maxGollons = 0;
scanf("%d %d %d", &N, &M, &R);
for (int i = 0; i <= M; i ++) {
scanf("%d %d %d", &T[i].start, &T[i].end, &T[i].gollon);
T[i].end += R;
}
sort(T, T + M, cmp);
for (int i = 0; i < M; i ++) {
dp[i] = T[i].gollon;
for (int j = 0; j < i; j ++) {
if (T[j].end <= T[i].start) {
dp[i] = max(dp[i], dp[j] + T[i].gollon);
}
}
maxGollons = max(maxGollons, dp[i]);
}
printf("%d\n", maxGollons);
return 0;
}

【动态规划】POJ-3616的更多相关文章

  1. POJ - 3616 Milking Time (动态规划)

    Bessie is such a hard-working cow. In fact, she is so focused on maximizing her productivity that sh ...

  2. 动态规划:POJ 3616 Milking Time

    #include <iostream> #include <algorithm> #include <cstring> #include <cstdio> ...

  3. poj 3616(动态规划)

    Milking Time Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7265   Accepted: 3043 Desc ...

  4. 【POJ - 3616】Milking Time(动态规划)

    Milking Time 直接翻译了 Descriptions 贝茜是一个勤劳的牛.事实上,她如此​​专注于最大化她的生产力,于是她决定安排下一个N(1≤N≤1,000,000)小时(方便地标记为0. ...

  5. POJ 3616 Milking Time(加掩饰的LIS)

    传送门: http://poj.org/problem?id=3616 Milking Time Time Limit: 1000MS   Memory Limit: 65536K Total Sub ...

  6. POJ 3616 Milking Time (排序+dp)

    题目链接:http://poj.org/problem?id=3616 有头牛产奶n小时(n<=1000000),但必须在m个时间段内取奶,给定每个时间段的起始时间和结束时间以及取奶质量 且两次 ...

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

    题目链接:http://poj.org/problem?id=3616 题目大意:给你时间N,还有M个区间每个区间a[i]都有开始时间.结束时间.生产效率(时间都不超过N),只能在给出的时间段内生产, ...

  8. poj 3616 Milking Time (基础dp)

    题目链接 http://poj.org/problem?id=3616 题意:在一个农场里,在长度为N个时间可以挤奶,但只能挤M次,且每挤一次就要休息t分钟: 接下来给m组数据表示挤奶的时间与奶量求最 ...

  9. 二分+动态规划 POJ 1973 Software Company

    Software Company Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 1112   Accepted: 482 D ...

  10. poj 3616 Milking Time

                                                                                                 Milking ...

随机推荐

  1. 十七、S3C2440 音频解码芯片WM8976声卡驱动移植、madplay测试

    学习目标:1. WM9876接口和工作原理:2. WM9876驱动移植:3. WM9876应用测试:4. 问题总结 1. WM9876接口和工作原理  本节使用了JZ2440开发板移植WM9876驱动 ...

  2. 飞控入门之C语言指针回顾

    指针 何为指针?来个官方定义:指针是一个值为内存地址的变量(或数据对象). 一.指针的声明 //示例 int *pi; //pi是指向int类型变量的指针 char *pc; // pi是指向char ...

  3. Web安全0003 - MySQL SQL注入 - union查询核心语法

    注:本文是学习网易Web安全进阶课的笔记,特此声明. 查库,select schema_name from information_schema.schemata; 查表,select table_n ...

  4. BZOJ 2395 [Balkan 2011]Time is money

    题面 题解 将\(\sum_i c_i\)和\(\sum_i t_i\)分别看做分别看做\(x\)和\(y\),投射到平面直角坐标系中,于是就是找\(xy\)最小的点 于是可以先找出\(x\)最小的点 ...

  5. CF 1138 E. Museums Tour

    E. Museums Tour 链接 分析: 按时间建出分层图,每个点形如(u,t),表示u在在t个时刻的点,tarjan缩点.每个强连通分量中的点都能经过,然后DAG上dp. 代码: #includ ...

  6. Gitlab+Jenkins学习之路(十)之Jenkins按角色授权和Pipeline

    一.Jenkins按角色授权 当一个公司的开发分为多个组别,或者是多个项目等等.用于公司内部测试,让开发人员自行构建测试,此时不可能让所有的开发都在公用一个构建,这样变得很混乱,为了解决这一问题,je ...

  7. Unity3d之Hash&Slash学习笔记之(二)--角色基础类的构建

    Hash&Slash学习笔记之(二)--角色基础类的构建 BaseStat类的构建 基本成员变量: _baseValue //基础属性值 _buffValue //增加的buff值 _expT ...

  8. 通过IDEA解决spring配置文件

    来自:https://blog.csdn.net/yanghanxiu/article/details/79366263 (其实尼可以使用springboot 这样就不用配置一大堆东西了喵!) 每次创 ...

  9. tcp三次握手四次挥手那些事

    建立TCP需要三次握手才能建立,而断开连接则需要四次挥手.三次握手,四次挥手流程图如下: 一.首先看下如何通过三次挥手----------建立连接 首先客户端发送连接请求报文,服务端接受连接后回复AC ...

  10. Flutter - 左右侧滑菜单:drawer和endDrawer

    侧滑菜单可以从左面滑出,也可以从右面滑出.在Scaffold中有drawer和endDrawer两个参数,分别对应左边的菜单和右边的菜单. drawer: new Drawer( child: new ...