UPC——2789: Selling CPUs(DP)

2789: Selling CPUs

传送门

时间限制: 1 Sec  内存限制: 64 MB
提交: 164  解决: 58
[提交] [状态] [讨论版] [命题人:admin]

 

题目描述

You are very happy, that you got a job at ACME Corporation’s CPU factory. After a hard month of labor, your boss has given you c identical CPUs as payment. Apparently, ACME Corporation is low on cash at the moment.
Since you can’t live on CPUs alone, you want to sell them on the market and buy living essentials from the money you make. Unfortunately, the market is very restrictive in the way you are allowed to conduct business. You are only allowed to enter the market once, you can only trade with each merchant once, and you have to visit the traders in a specific order. The market organizers have marked each merchant with a number from 1 to m (the number of merchants) and you must visit them in this order. Each trader has his own price for every amount of CPUs to buy.

 

输入

The input consists of:
• one line with two integers c and m (1 ≤ c, m ≤ 100), where c is the number of CPUs and m is the number of merchants;
• m lines describing the merchants. Each merchant is described by:
    – one line with c integers p 1 , . . . , p c (1 ≤ p i ≤ 10 5 for all 1 ≤ i ≤ c), where p i is the amount of money the merchant will give you for i CPUs.

 

输出

Output the maximum amount of money you can make by selling the CPUs you have.
Note that you don’t have to sell all CPUs.

 

样例输入

5 3
1 4 10 1 1
1 1 8 1 1
1 1 9 1 1

 

样例输出

13

 

来源/分类

GCPC2016

 

 

解题思路:

本题需求出最多能获得的钱。由于只能在每个商人那里卖一次CPU,卖的数量决定这些CPU的总价格。可以选择不去某些商人,也可以任意决定在商人那里卖出多少,当然,前题是还剩多少_(:з」∠)_

emmmmmDFS减枝暴力2s以上妥妥的.....还是DP来得爽快 (^-^)V

假设我们当前有5个CPU,在第一个商人那里我们能获得的最大收益一定是卖出0个、1个、2个、3个、4个、5个的价格的最大值(因为存在卖得多价值反而少的情况_(:з」∠)_)   将其记录为dp[1][5]   。 同理,假设只有拥有3个CPU,在第一个商人那里能最多获得收益为dp[1][3],那么可以将拥有个数为1~C 的最佳状态都保存~     dp[1][1~C] 

那么当我们在第二个商人那里再想卖东西时问题就出现了,如果上一次卖出过CPU这次肯定只能在剩下的CPU中选择几个卖,假设总的有5个。能尝试卖出的为0个(不卖)、1个、2个、3个、4个、5个(如果还能剩下这么多)。卖出0个显然目前的收益依旧是dp[2][5] = dp[1][5] + 0(假设所有的5个都拿去第一个商家最佳出售了) ,而卖出1个时,假设剩下的4个都已经完美运用过了也就是dp[1][4](上次拥有4个可以卖出的最大收益) +  在第二个商人卖出1个CPU的价格cost[2][1] ,同理我们可以得到:

dp[1][5] + 0

dp[1][4] + cost[2][1]

dp[1][3] + cost[2][2]

dp[1][2] + cost[2][3] 

dp[1][1] + cost[2][4]

dp[1][0] + cost[2][5]   

 

具体在第二个商人那里获得的最大收益为多少便是以上6种情况的最大值,该状态记录在dp[2][5]中。又因为只能从第i个商家到i+1个,将最佳的dp[i][j] 状态可以不断更新下去~

可以得到状态转换方程 dp[i][j] = (dp[i][j] , dp[i-1][j-k] + cost[i][k] )   ,其中 dp[i][j] 表示在第i个商人那里有j个物品能够使用时能获得的最大收益,k表示第i个商家给出的k个CPU价格。

如果有3个商人的话那么答案就是dp[3][5]即dp[m][c]

 

AC代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int c,m; 
int mp[105][105];
ll dp[105][105];
int main(void)
{
    cin>>c>>m;	
    memset(mp,0,sizeof(mp)); 
    memset(dp,0,sizeof(dp));

    for(int i=1;i<=m;i++)
    	for(int j =1;j<=c;j++)
    		cin>>mp[i][j]; 

 	for(int i=1;i<=m;i++)
	{
		for(int j=1;j<=c;j++)
		{
			for(int k =0;k<=j;k++)    
				dp[i][j] = max(dp[i][j] , dp[i-1][j-k] + mp[i][k]);    
		}
	}		
	cout<<dp[m][c]<<endl;  
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值