zoukankan      html  css  js  c++  java
  • (01背包 先排序)Proud Merchants (hdu 3466)

     

    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

    题意: 有 n 个物品,每个物品都有一定的价值和花费,而且买的时候自己的钱不能低于那个物品的指标,问最后可以买到的物品的最大价值是多少。

    思路: 需要对物品按 q-p 的值从小到大排序,因为这样可以保证每次更新的状态值从小到大递增,前面更新过的状态不会影响后面更新的状态。

     

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <queue>
    #include <vector>
    #include <map>
    #include <algorithm>
    using namespace std;
    
    const int N = 6600;
    const int INF = 0x3fffffff;
    const long long MOD = 1000000007;
    typedef long long LL;
    #define met(a,b) (memset(a,b,sizeof(a)))
    
    struct node
    {
        int p, q, v;
        bool friend operator < (node n1, node n2)
        {
            return (n1.q-n1.p)<(n2.q-n2.p);
        }
    }a[N];
    
    int dp[N];
    
    
    int main()
    {
        int n, m;
    
        while(scanf("%d%d", &n, &m)!=EOF)
        {
            int i, j;
    
            met(a, 0);
            met(dp, 0);
    
            for(i=1; i<=n; i++)
                scanf("%d%d%d", &a[i].p, &a[i].q, &a[i].v);
    
            sort(a, a+n);
    
            for(i=1; i<=n; i++)
            {
                for(j=m; j>=a[i].q; j--)
                {
                    dp[j] = max(dp[j], dp[j-a[i].p]+a[i].v);
                }
            }
    
            printf("%d
    ", dp[m]);
        }
        return 0;
    }
  • 相关阅读:
    Java第9次作业--接口及接口回调
    Java第8次作业--继承
    软件工程第三次作业——关于软件质量保障初探
    Java第7次作业--访问权限、对象使用
    Java第6次作业--static关键字、对象
    Java第5次作业--对象的创建与使用
    20194629 自动生成四则运算题第一版报告
    软件工程第一次作业
    今天开通博客啦!
    1170. Compare Strings by Frequency of the Smallest Character
  • 原文地址:https://www.cnblogs.com/YY56/p/5477526.html
Copyright © 2011-2022 走看看