【dp每日一题】POJ 3616 Milking Time
POJ 3616 Milking Time
大意:
m个牛需要挤奶,他们都有一个开始时间结束时间以及价值,对于每只牛,挤完奶需要休息k分钟,最后问能挤多少价值的奶。
思路:
排序后从左到右更新即可
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
const int N = 1e6 + 5;
int dp[N], res;
struct node {
int l, r, v;
} a[N];
int n, m, r;
bool cmp(node a, node b) {
if (a.l == b.l) return a.r < b.r;
return a.l < b.l;
}
int main() {
cin >> n >> m >> r;
for (int i = 0; i < m; i++) {
cin >> a[i].l >> a[i].r >> a[i].v;
dp[i] = a[i].v;
}
int res = 0;
sort(a, a + m, cmp);
for (int i = 0; i < m; i++) {
dp[i] = a[i].v;
}
for (int i = 0; i < m; i++) {
for (int j = i + 1; j < m; j++) {
if (a[j].l >= a[i].r + r) dp[j] = max(dp[j], dp[i] + a[j].v);
}
res = max(res, dp[i]);
}
cout << res << endl;
}