POJ - 3616 Milking Time(dp)

题目链接:http://poj.org/problem?id=3616

题意:给许多时间段和这些时间段的收益,并且一个时间段做完后要隔r时间才能进行下一个,问你收益最大为多大

思路:仔细一想就是一个把每个时间段任务看成一个点的LIS,只不过要先按时间排一下。


代码:

#include <cstdio>
#include <cmath>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <cctype>
#include <sstream>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long LL;
typedef pair<LL, LL> P;
const int maxn = 1e3 + 5;
const int mod = 1e8 + 7;

int n,m,r;
int dp[maxn];
struct node {
    int st,ed,v;
}a[maxn];
bool cmp(node x,node y){
    return x.st<y.st;
}
int main() {
    while (~scanf ("%d%d%d",&n,&m,&r)){
        for (int i=1;i<=m;i++){
            scanf ("%d%d%d",&a[i].st,&a[i].ed,&a[i].v);
            a[i].ed+=r;
        }
        sort(a+1,a+m+1,cmp);
        for (int i=1;i<=m;i++) {
            dp[i]=a[i].v;
        }
        int Max=-INF;
        for (int i=2;i<=m;i++){
            for (int j=1;j<i;j++){
                if(a[j].ed<=a[i].st) {
                    dp[i]=max(dp[i],dp[j]+a[i].v);
                }
            }
            Max=max(Max,dp[i]);
        }
        printf ("%d\n",Max);
    }
    return 0;
}





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值