洛谷 P1417 邻项交换贪心+DP

一共有n件食材,每件食材有三个属性,ai,bi和ci,如果在t时刻完成第i样食材则得到ai-t*bi的美味指数,用第i件食材做饭要花去ci的时间。输出最大美味指数

【数据范围】

对于40%的数据1<=n<=10

对于100%的数据1<=n<=50

所有数字均小于100,000

思路:看了51nod贪心专题视频后学习的一种方法,同萌@Frenix告诉我这种思想叫邻项交换法

就是说取任意两个项x和y,列出x在前的贡献和y在前的贡献,比较大小,如果可以确定在什么情况下有一种严格优于另一种,就可以得到贪心策略

这题容易发现当c[x] * b[y] < c[y] * b[x]时x一定比y好,然后直接01背包

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<iostream>
 5 #define LL long long
 6 #define debug(x) cout << "[" << x << "]" << endl
 7 using namespace std;
 8 
 9 const int mx = 1e5+10;
10 
11 struct node{
12     LL a, b, c;
13     bool operator < (const node& k) const{
14         return c*k.b < k.c*b;
15     }
16 }a[55];
17 LL dp[mx];
18 
19 int main(){
20     int t, n;
21     LL ans = 0;
22     scanf("%d%d", &t, &n);
23     for (int i = 1; i <= n; i++) scanf("%lld", &a[i].a);
24     for (int i = 1; i <= n; i++) scanf("%lld", &a[i].b);
25     for (int i = 1; i <= n; i++) scanf("%lld", &a[i].c);
26     sort(a+1, a+n+1);
27     for (int i = 1; i <= n; i++)
28         for (int j = t; j >= a[i].c; j--)
29             dp[j] = max(dp[j], dp[j-a[i].c]+a[i].a-j*a[i].b);
30     for (int i = 1; i <= t; i++) ans = max(ans, dp[i]);
31     printf("%lld\n", ans);
32     return 0;
33 }

猜你喜欢

转载自www.cnblogs.com/QAQorz/p/9594470.html
今日推荐