POJ 3616 Milking Time(加掩饰的LIS)
传送门:
http://poj.org/problem?id=3616
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 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
挤奶工每次挤奶必须挤完完整的时间段,且每次挤完需要休息r时,求最终可获得的牛奶最大值
然后用动态规划做。
dp[i]代表第i个时间区间挤奶可获得的最大产奶量,
需要遍历前i-1个时间区间中结束时间小于等于第i个时间区间中开始时间,
求出最大值加上第i个时间区间的产奶量就是dp[i],
最后去dp数组最大值即可。
#include<stdio.h> #include<algorithm> #include<iostream> using namespace std; #define max_v 1005 struct node { int s,f,v; }p[max_v]; bool cmp(node a,node b) { return a.f<b.f; } int dp[max_v]; int main() { /* 先按照贪心的想法按照结束时间(输入的结束时间+R)升序排序, 然后用动态规划做。 dp[i]代表第i个时间区间挤奶可获得的最大产奶量, 需要遍历前i-1个时间区间中结束时间小于等于第i个时间区间中开始时间, 求出最大值加上第i个时间区间的产奶量就是dp[i], 最后去dp数组最大值即可。 */ int n,m,r; while(cin>>n>>m>>r) { for(int i=0;i<m;i++) { scanf("%d %d %d",&p[i].s,&p[i].f,&p[i].v); p[i].f+=r; } sort(p,p+m,cmp); for(int i=0;i<max_v;i++) dp[i]=0; dp[0]=p[0].v; int ans=dp[0]; for(int i=1;i<m;i++) { int t=0; for(int j=0;j<i;j++) { if(p[j].f<=p[i].s) { t=max(t,dp[j]); } } dp[i]=t+p[i].v; ans=max(ans,dp[i]); } printf("%d\n",ans); } return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南