题意:奶牛Bessie在0~N时间段产奶。农夫约翰有M个时间段可以挤奶,时间段f,t内Bessie能挤到的牛奶量e。奶牛产奶后需要休息R小时才能继续下一次产奶,
求Bessie最大的挤奶量。
析:一个很水的DP,就是不能再表示时刻了,而是区间,dp[i] 第 i 个区间 最大是多少。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1000 + 10;
const int mod = 1000007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
struct Node{
int s, t, e;
bool operator < (const Node &p) const{
return s < p.s || (s == p.s && t < p.t);
}
};
Node a[maxn];
int dp[maxn];
int main(){
int r;
while(scanf("%d %d %d", &n, &m, &r) == 3){
for(int i = 0; i < m; ++i) scanf("%d %d %d", &a[i].s, &a[i].t, &a[i].e);
sort(a, a + m);
int ans = 0;
for(int i = 0; i < m; ++i){
dp[i] = 0;
for(int j = 0; j < i; ++j)
if(a[i].s >= a[j].t + r) dp[i] = max(dp[i], dp[j]);
dp[i] += a[i].e;
ans = max(ans, dp[i]);
}
printf("%d
", ans);
}
return 0;
}