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;
    }
  • 相关阅读:
    JVM(一)--Java内存区域
    leetcode33 搜索旋转排序数组
    二叉树的相关算法(一)
    分别求二叉树前、中、后序的第k个节点
    【蓝桥杯】历届试题 买不到的数目
    JAVA的JVM的内存可分为3个区:堆(heap)、栈(stack)和方法区(method)
    【蓝桥杯】核桃的数量
    Java内存分析之对象实例化操作初步分析
    【蓝桥杯】算法训练 K好数
    【蓝桥杯】基础练习试题
  • 原文地址:https://www.cnblogs.com/YY56/p/5477526.html
Copyright © 2011-2022 走看看