zoukankan      html  css  js  c++  java
  • 邻项交互贪

    一共有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;
    }
  • 相关阅读:
    使用Linq to Sqlite 出现异常Object already attached
    CSS 嵌套DIV布局
    《面试笔记》——MySQL终结篇(30问与答)
    PotPlayer播放器下载
    博客圆的RSS怎么不能用呢
    OPC在自控系统的应用
    TAPI的使用
    刷iPAQ为Linux(zz HiPDA)
    再论软工
    Silverlight的大小自适应中存在的一个问题
  • 原文地址:https://www.cnblogs.com/DWVictor/p/10324785.html
Copyright © 2011-2022 走看看