poj3616 Milking Time(区间dp)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 14332 | Accepted: 6071 |
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
这次做题主要知道了:一定要明确dp数组的定义再去做题!!!

1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 struct node 7 { 8 int st,et,v; 9 }cow[1002]; 10 int t,n,r,dp[1002]; 11 bool cmp(node a,node b) 12 { 13 if(a.st==b.st) return a.et<b.et; 14 return a.st<b.st; 15 } 16 int main() 17 { 18 scanf("%d%d%d",&t,&n,&r); 19 for(int i=1;i<=n;i++) 20 { 21 scanf("%d%d%d",&cow[i].st,&cow[i].et,&cow[i].v); 22 cow[i].et+=r;//因为考虑间隔,所以直接把区间拉长 23 } 24 sort(cow+1,cow+1+n,cmp); 25 for(int i=1;i<=n;i++) 26 { 27 dp[i]=cow[i].v; 28 for(int j=1;j<i;j++) 29 { 30 if(cow[j].et<=cow[i].st)//首先保证上一个区间处理完了,如果上个区间还没结束,就开始计算这个区间,会重复计算,可以去掉这句用样例看一下 31 dp[i]=max(dp[i],dp[j]+cow[i].v); 32 } 33 } 34 //for(int i=1;i<=n;i++) printf("%d ",dp[i]); 35 printf("%d\n",*max_element(dp+1,dp+1+n)); 36 /*选取最大的dp[i],可以试一下这样的样例: 37 100 2 2 38 1 100 99 39 5 6 1 40 就知道为什么了(还可以根据dp定义) 41 */ 42 return 0; 43 }
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 为什么构造函数需要尽可能的简单
· 探秘 MySQL 索引底层原理,解锁数据库优化的关键密码(下)
· 大模型 Token 究竟是啥:图解大模型Token
· 35岁程序员的中年求职记:四次碰壁后的深度反思
· 继承的思维:从思维模式到架构设计的深度解析
· 【保姆级教程】windows 安装 docker 全流程
· 基于Docker+DeepSeek+Dify :搭建企业级本地私有化知识库超详细教程
· 由 MCP 官方推出的 C# SDK,使 .NET 应用程序、服务和库能够快速实现与 MCP 客户端
· 电商平台中订单未支付过期如何实现自动关单?
· X86-64位简易系统开发 - 从BIOS阶段开始