题目链接: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;
}