码迷,mamicode.com
首页 > 其他好文 > 详细

Making Money

时间:2018-07-31 23:29:09      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:with   tree   amp   sts   log   clu   ber   输出   new   

描述

FJ has gone into the curio business, buying and selling knickknacks like cow Christmas tree ornaments. He knows he will sell every single curio he can stock from a catalog of N (1 <= N <= 100) different cow curios, and he can buy as many of each kind of curio as his heart desires. He has only M (1 <= M <= 100,000) money to invest but wants to maximize his profit (which has a slightly unusual definition) at the end of his first year in business. 
Curio type i costs C_i (1 <= C_i <= 100,000) money to purchase and yields R_i (1 <= R_i <= 100,000) revenue for each curio sold (a profit of R_i-C_i). FJ can mix and match the curios he sells in any way he wishes. He need not spend all his money when purchasing curios. 
What is the greatest amount of total profit (profit = initial_cash - all_costs + all_sales) FJ can have at the end of his first year? This number is guaranteed to be less than 1,000,000,000. 
Consider the situation when FJ has just 3 kinds of curios and starts with M=17. Below are the cost and revenue numbers for each curio: 

             Curio     Cost     Revenue

# C_i R_i
1 2 4
2 5 6
3 3 7

In this case, FJ should purchase 5 curios of type 3 for 15 money and 1 more curio of type 1 for 2 money, a total of 17 money. His profit will be 5 * (7-3) + 1 * (4-2) = 5*4 + 1*2 = 22 money. He can do no better than this given the cost and revenue structure. 
NOTE: The second test case is challenging -- but our answer is correct.

输入

* Line 1: Two space-separated integers: N and M 
* Lines 2..N+1: Line i+1 contains two space-separated integers: C_i and R_i

输出

* Line 1: The maximum profit FJ can generate given the costs and revenues

样例输入

3 17
2 4
5 6
3 7

样例输出

22

题目大意:用m的钱去买一些古物, 并且卖出, 使得所获得的利润最大。 可以用01背包来做这道题。(但是要注意钱可能有剩余)

#include <bits/stdc++.h>
using namespace std;
const int Max=105;
#define INF 0x3f3f3f3f
int dp[100005],a[Max],v[Max];
int main()
{
    int n,m,k;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        int b;
        scanf("%d%d",&a[i],&b);
        v[i]=b-a[i];
    } 
    for(int i=1;i<=n;i++)
    {
        for(int j=a[i];j<=m;j++)
        dp[j]=max(dp[j],dp[j-a[i]]+v[i]);
    }  //01背包 
    int Max=0;
    for(int i=1;i<=m;i++)
    Max=max(Max,m-i+dp[i]); //  剩下的钱+收入 
    cout<<Max<<endl;
}

 

 

Making Money

标签:with   tree   amp   sts   log   clu   ber   输出   new   

原文地址:https://www.cnblogs.com/ww123/p/9398591.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!