记得排序啊
#include#include #include #include #include using namespace std;typedef pair PairII;inline int dist(int start, int end) { return end - start + 1; }template void pushElem(DQ & q, PairII p) { while (!q.empty() && q.back().second <= p.second) q.pop_back(); q.push_back(p);}struct Worker { int L, S, P;};bool cmpWorker(const Worker & a, const Worker & b) { return a.S < b.S;}deque Q;int N, K;int f[101][16001];Worker a[101];void dp() { int i, j; for (i = 0; i <= K; ++i) { int t = 0; for (j = 0; j <= N; ++j) { int & ans = f[i][j]; ans = 0; if (i == 0) { ans = 0; } else if (j == 0) { ans = 0; } else if (j < a[i].S) { ans = f[i - 1][j]; } else if (a[i].S <= j && dist(a[i].S, j) < a[i].L) { ans = f[i - 1][j]; int rest = a[i].L - dist(a[i].S, j); int left = max(0, a[i].S - rest - 1); while (t <= a[i].S - 1) { pushElem(Q, make_pair(t, f[i - 1][t] + a[i].P * dist(t + 1, a[i].S))); t += 1; } while (!Q.empty() && Q.front().first < left) Q.pop_front(); if (!Q.empty()) { ans = max(ans, Q.front().second + dist(a[i].S + 1, j) * a[i].P); } } else if (dist(a[i].S, j) >= a[i].L) { ans = max(f[i - 1][j], f[i - 1][a[i].S - 1] + a[i].L * a[i].P); } else { assert(0); } } }}void input() { scanf("%d %d", &N, &K); int i, j; for (i = 1; i <= K; ++i) { scanf("%d %d %d", &(a[i].L), &(a[i].P), &(a[i].S)); } //因为尼玛忘记排序,wa了好多次!!! sort(a + 1, a + K + 1, cmpWorker);}int main() { input(); dp(); printf("%d\n", f[K][N]); return 0;}