标签:
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.
* 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
* Line 1: The maximum number of gallons of milk that Bessie can product in the N hours
12 4 2 1 2 8 10 12 19 3 6 24 7 10 31
43
题目大意:有N个时间点可以紧挨,M个任务可以挤奶,R是每次挤奶完要休息的时间。
对于每个挤奶的任务,有一个开始时间,结束时间,还有一个挤奶量。
动态规划,定义DP[i][0],dp[i][1]分别表示第i次任务不取或者取。具体看代码
#include<iostream> #include<queue> #include<stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #include <stack> #include <algorithm> //STL 通用算法 #include <bitset> //STL 位集容器 #include <cctype> #include <cerrno> #include <clocale> #include <cmath> #include <complex> //复数类 #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> //STL 双端队列容器 #include <exception> //异常处理类 #include <fstream> #include <functional> //STL 定义运算函数(代替运算符) #include <limits> #include <list> //STL 线性列表容器 #include <map> //STL 映射容器 #include <iomanip> #include <ios> //基本输入/输出支持 #include <iosfwd> //输入/输出系统使用的前置声明 #include <iostream> #include <istream> //基本输入流 #include <ostream> //基本输出流 #include <queue> //STL 队列容器 #include <set> //STL 集合容器 #include <sstream> //基于字符串的流 #include <stack> //STL 堆栈容器 #include <stdexcept> //标准异常类 #include <streambuf> //底层输入/输出支持 #include <string> //字符串类 #include <utility> //STL 通用模板类 #include <vector> //STL 动态数组容器 #include <cwchar> #include <cwctype> using namespace std; typedef struct { int s; int e; int v; }node; vector <node> q; int dp[1005][5]; int cmp(node a,node b) { return a.e<b.e; } int main() { int i,j,n,m,r,a,b,c,maxn; node t; while(scanf("%d %d %d",&n,&m,&r)!=EOF) { q.clear(); for(i=0;i<m;i++) { scanf("%d %d %d",&t.s,&t.e,&t.v); q.push_back(t); } sort(q.begin(),q.end(),cmp); memset(dp,0,sizeof(dp)); maxn=0; dp[0][1]=q[0].v; for(i=1;i<m;i++) { for(j=0;j<i;j++) { dp[i][0]=max(dp[i][0],dp[j][0]); dp[i][0]=max(dp[i][0],dp[j][1]); if(q[j].e+r<=q[i].s) { dp[i][1]=max(dp[i][1],dp[j][0]); dp[i][1]=max(dp[i][1],dp[j][1]); } } dp[i][1]+=q[i].v; maxn=max(dp[i][0],maxn); maxn=max(dp[i][1],maxn); } printf("%d\n",maxn); } }
标签:
原文地址:http://www.cnblogs.com/Qiao994255978/p/4718516.html