邻项交互贪

一共有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背包

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#define LL long long
#define debug(x) cout << "[" << x << "]" << endl
using namespace std;

const int mx = 1e5+10;

struct node{
    LL a, b, c;
    bool operator < (const node& k) const{
        return c*k.b < k.c*b;
    }
}a[55];
LL dp[mx];

int main(){
    int t, n;
    LL ans = 0;
    scanf("%d%d", &t, &n);
    for (int i = 1; i <= n; i++) scanf("%lld", &a[i].a);
    for (int i = 1; i <= n; i++) scanf("%lld", &a[i].b);
    for (int i = 1; i <= n; i++) scanf("%lld", &a[i].c);
    sort(a+1, a+n+1);
    for (int i = 1; i <= n; i++)
        for (int j = t; j >= a[i].c; j--)
            dp[j] = max(dp[j], dp[j-a[i].c]+a[i].a-j*a[i].b);
    for (int i = 1; i <= t; i++) ans = max(ans, dp[i]);
    printf("%lld
", ans);
    return 0;
}
原文地址:https://www.cnblogs.com/DWVictor/p/10324785.html