• HDU 3446 有贪心思想的01背包


    Proud Merchants

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
    Total Submission(s): 4500    Accepted Submission(s): 1873


    Problem Description
    Recently, iSea went to an ancient country. For such a long time, it was the most wealthy and powerful kingdom in the world. As a result, the people in this country are still very proud even if their nation hasn’t been so wealthy any more.
    The merchants were the most typical, each of them only sold exactly one item, the price was Pi, but they would refuse to make a trade with you if your money were less than Qi, and iSea evaluated every item a value Vi.
    If he had M units of money, what’s the maximum value iSea could get?

     
    Input
    There are several test cases in the input.

    Each test case begin with two integers N, M (1 ≤ N ≤ 500, 1 ≤ M ≤ 5000), indicating the items’ number and the initial money.
    Then N lines follow, each line contains three numbers Pi, Qi and Vi (1 ≤ Pi ≤ Qi ≤ 100, 1 ≤ Vi ≤ 1000), their meaning is in the description.

    The input terminates by end of file marker.

     
    Output
    For each test case, output one integer, indicating maximum value iSea could get.

     
    Sample Input
    2 10
    10 15 10
    5 10 5
    3 10
    5 10 5
    3 5 6
    2 7 3
     
    Sample Output
    5
    11
     
    Author
    iSea @ WHU
     
    Source
     
    题意:给定 M 元钱,有 N 件商品,价格为 Wi ,价值为 Pi ,限制条件是手中钱少于 Qi 时不能购买该件商品。求可以获得的最大价值。
     
    题解:
     

    因为这个题目增加了购买的前提条件,和普通的01背包有点不同;        

    哪里不同呢?不同的地方在于普通的01背包,购买顺序不影响其结果;         

    但是在这里,我们可以很明白的看出来,购买顺序是会影响我们的最后结果的。      

     所以我们应该确定一个正确的购买顺序;然后我们就可以想想,如果叫你判断买不买,       

     实现价值最大化,你会以怎样的顺序先后判断;        

     很显然,你会先判断q大,p小的物品买不买,对吧,因为在你价值最大,你应该尽可能的判断q大,p小的物品;      

     明白这点,这个题目基本上就解决了;       

     我们可以对数据进行排序,直接就以p,q的差值排序就可以了;       

     但是这里我们需要注意的是,要以差值由小到大排序,而不是由大到小,      

     想想dp的过程,j=m,j--;后取得数在前面判断,这样才能让我们的数据更新不被影响; 

     1 #include<iostream>
     2  #include<cstring>
     3  #include<cstdio>
     4  #include<queue>
     5  #include<stack>
     6  #include<map>
     7  #include<set>
     8  #include<algorithm>
     9  #define ll __int64
    10  #define pi acos(-1.0)
    11  #define mod 1
    12  #define maxn 10000
    13  using namespace std;
    14  int n,m;
    15  struct node
    16  {
    17      int p,q,v;
    18  }N[505];
    19  int dp[5005];
    20  bool  cmp(struct node aa,struct node bb)
    21  {
    22      if((aa.q-aa.p)<(bb.q-bb.p))
    23       return  true;
    24     return false;
    25  }
    26  int main()
    27  {
    28      while(scanf("%d %d",&n,&m)!=EOF)
    29      {
    30          memset(dp,0,sizeof(dp));
    31          memset(N,0,sizeof(N));
    32          for(int i=1;i<=n;i++)
    33           scanf("%d %d %d",&N[i].p,&N[i].q,&N[i].v);
    34           sort(N+1,N+1+n,cmp);
    35           for(int i=1;i<=n;i++)
    36           {
    37               for(int l=m;l>=N[i].q;l--)
    38                dp[l]=max(dp[l],dp[l-N[i].p]+N[i].v);    
    39          }
    40         cout<<dp[m]<<endl;
    41     }
    42      return 0;
    43  }
  • 相关阅读:
    埃尔多安回击“穆斯林都是恐怖分子”:坏人也可能是基督徒或犹太人
    通信行业并非那么”腹黑“
    苹果无人驾驶技术专家遭大众挖角
    数据结构之数组定义及基本操作(转)
    LCD浮点数显示函数的探讨
    C 数据类型
    C 基本语法
    C 标准库
    C 语言实例
    C 语言实例
  • 原文地址:https://www.cnblogs.com/hsd-/p/5441043.html
Copyright © 2020-2023  润新知