题目链接:http://poj.org/problem?id=3616
题目大意:奶牛有m段时间产奶,每段起始时间s,结束时间e,产奶量val,如果选择当前段产奶,则要休息r时间才能开始下一次产奶,求最大产奶量
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
思路:按开始时间排一下序,然后就相当于转换成了一个最大上升子序列,判断一下当前起始时间和前一个结束时间,dp[i][0]代表以i结尾最大值,dp[i][1]代表以i结尾结束时间
代码:
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<queue>
#include<stack>
#include<map>
using namespace std;
#define FOU(i,x,y) for(int i=x;i<=y;i++)
#define FOD(i,x,y) for(int i=x;i>=y;i--)
#define MEM(a,val) memset(a,val,sizeof(a))
#define PI acos(-1.0)
const double EXP = 1e-9;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const ll MINF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9+7;
const int N = 1e6+5;
struct node
{
int s,e,val;
}a[1005];
int dp[1005][2]; //dp[i][0]代表大小,dp[i][1]记录当前结束时间
bool cmp(node aa,node bb)
{
if(aa.s==bb.s)
return aa.e<bb.e;
return aa.s<bb.s;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
std::ios::sync_with_stdio(false);
int n,m,r;
while(~scanf("%d%d%d",&n,&m,&r))
{
MEM(dp,0);
for(int i=1;i<=m;i++)
scanf("%d%d%d",&a[i].s,&a[i].e,&a[i].val);
sort(a+1,a+1+m,cmp);
for(int i=1;i<=m;i++)
{
for(int j=0;j<i;j++)
{
if(dp[j][1]==0)
{
dp[i][0]=dp[j][0]+a[i].val;
dp[i][1]=a[i].e;
}
else
{
if(a[i].s-dp[j][1]>=r&&dp[j][0]+a[i].val>dp[i][0])
{
dp[i][0]=dp[j][0]+a[i].val;
dp[i][1]=a[i].e;
}
}
}
}
int ans=0;
for(int i=1;i<=m;i++)
ans=max(ans,dp[i][0]);
printf("%d\n",ans);
}
return 0;
}