zoukankan      html  css  js  c++  java
  • 洛谷 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
    ", ans);
    32     return 0;
    33 }
  • 相关阅读:
    python进阶学习chapter04(字符串相关)
    python进阶学习chapter03(迭代相关)
    python学习笔记之collections模块的使用
    python进阶学习chapter02(列表、字典、集合操作)
    python接口测试之json模块的使用
    python接口测试之如何发送邮件
    python接口测试之如何操作excel
    python unittest库的入门学习
    python requests库学习笔记
    重建二叉树*
  • 原文地址:https://www.cnblogs.com/QAQorz/p/9594470.html
Copyright © 2011-2022 走看看