题目链接:https://www.luogu.org/problemnew/show/P1417
复制一下别人的题解:
如果没有b[i]这个属性的话就是明显的01背包问题。
现在考虑相邻的两个物品x,y。假设现在已经耗费p的时间,那么分别列出先做x,y的代价:
a[x]-(p+c[x])*b[x]+a[y]-(p+c[x]+c[y])*by
a[y]-(p+c[y])*b[y]+a[x]-(p+c[y]+c[x])*bx
对这两个式子化简,得到①>②的条件是c[x]*b[y]<c[y]*b[x].
发现只要满足这个条件的物品对(x,y),x在y前的代价永远更优。
因此可以根据这个条件进行排序,之后就是简单的01背包了。
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <cassert>
#include <string.h>
using namespace std;
#define rep(i, a, b) for(int i = (a); i <= (b); i++)
#define reps(i, a, b) for(int i = (a); i < (b); i++)
#define pb push_back
#define ps push
#define mp make_pair
#define CLR(x,t) memset(x,t,sizeof x)
#define LEN(X) strlen(X)
#define F first
#define S second
#define Debug(x) cout<<#x<<"="<<x<<endl;
const double euler_r = 0.57721566490153286060651209;
const double pi = 3.141592653589793238462643383279;
const double E = 2.7182818284590452353602874713526;
const int inf = ~0U >> 1;
const int MOD = int (1e9) + 7;
const double EPS = 1e-6;
typedef long long LL;
const int maxn = 100001;
struct Node {
int a, b, c;
} node[maxn];
LL f[maxn], ans;
int T, n, i, j;
bool cmp (Node node1, Node node2) {
return (LL) node1.c * (LL) node2.b < (LL) node2.c * (LL) node1.b;
}
int main() {
cin >> T >> n;
reps (i, 0, n) cin >> node[i].a;
reps (i, 0, n) cin >> node[i].b;
reps (i, 0, n) cin >> node[i].c;
sort (node, node + n, cmp);
CLR (f, -1);
f[0] = 0;
for (int i = 0; i < n ; i++) {
for (int j = T; j >= 0; j--) {
int newt = j + node[i].c;
if (f[j] != -1 && newt <= T) {
f[newt] = max (f[newt], f[j] + (LL) node[i].a - (LL) newt * (LL) node[i].b);
}
}
}
for (int i = 0; i <= T; i++) ans = max (ans, f[i]);
cout << ans << endl;
return 0;
}