LOJ1417

题目链接: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]

发现只要满足这个条件的物品对(x,y),x在y前的代价永远更优。

因此可以根据这个条件进行排序,之后就是简单的01背包了。

 

 

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
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<<"="<> 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;
}

 


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部