poj 3616 Milking Time(dp,类似于最长上升子序列)

本文介绍了一个基于最长上升子序列思想的挤奶调度算法,通过动态规划求解在考虑休息时间的情况下,如何安排挤奶顺序以达到最大挤奶量。算法首先按挤奶开始时间排序,然后使用动态规划进行计算。

题意:给奶牛挤奶,共m次可以挤,给出每次开始挤奶的时间st,结束挤奶的时间ed,还有挤奶的量ef,

每次挤完奶要休息r时间,问最大挤奶量.

题解:此题灵感来自于最长上升子序列的做法

#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <cmath>
#define maxn 1005
#define maxz 2005
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
int zh,m,rest;
struct node
{
	int l,r,eff;
	int len;
}cow[maxn];
int dp[maxn];
bool cmp(node a,node b)
{
	return a.l<b.l;
}
void solve()
{
	memset(dp,0,sizeof(dp));
	int ans=0;
	for(int i=0;i<m;i++)
	{
		dp[i]=cow[i].eff;
		for(int j=0;j<i;j++)
		{
			if(cow[i].l-cow[j].r>=rest)
			{
				dp[i]=max(dp[i],dp[j]+cow[i].eff);
			}
		}
		ans=max(ans,dp[i]);
	}
	cout << ans << endl;
}
int main()
{
   cin>>zh>>m>>rest;
   for(int i=0;i<m;i++)
   {
   	  int l,r,eff;
   	  cin>>l>>r>>eff;
   	  cow[i].l=l;
   	  cow[i].r=r;
   	  cow[i].eff=eff;
   	  cow[i].len=r-l;
   }
   sort(cow,cow+m,cmp);
   solve();
   return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值