[题目链接]
http://poj.org/problem?id=1821
[算法]
单调队列优化DP
[代码]
#include <algorithm> #include <bitset> #include <cctype> #include <cerrno> #include <clocale> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <exception> #include <fstream> #include <functional> #include <limits> #include <list> #include <map> #include <iomanip> #include <ios> #include <iosfwd> #include <iostream> #include <istream> #include <ostream> #include <queue> #include <set> #include <sstream> #include <stdexcept> #include <streambuf> #include <string> #include <utility> #include <vector> #include <cwchar> #include <cwctype> #include <stack> #include <limits.h> using namespace std; #define MAXM 110 #define MAXN 16010 int i,j,k,n,m; int f[MAXM][MAXN]; deque< int > q; struct info { int l,p,s; } a[MAXM]; inline bool cmp(info a,info b) { return a.s < b.s; } int main() { scanf("%d%d",&n,&m); for (i = 1; i <= m; i++) scanf("%d%d%d",&a[i].l,&a[i].p,&a[i].s); sort(a+1,a+m+1,cmp); for (i = 1; i <= m; i++) { q.clear(); for (k = max(a[i].s - a[i].l,0); k < a[i].s; k++) { while (!q.empty() && f[i-1][k] - a[i].p * k >= f[i-1][q.back()] - a[i].p * q.back()) q.pop_back(); q.push_back(k); } for (j = 1; j <= n; j++) { f[i][j] = max(f[i-1][j],f[i][j-1]); if (j >= a[i].s) { while (!q.empty() && q.front() < j - a[i].l) q.pop_front(); if (!q.empty()) f[i][j] = max(f[i][j],f[i-1][q.front()] - a[i].p * q.front() + a[i].p * j); } } } printf("%d ",f[m][n]); return 0; }