Selling CPUs(DP)

[提交] [状态] [讨论版] [命题人: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

题意:这个人有c件商品,有m个买家,每个买家对着c件商品的出价不同,一个买家最多只能买一件,而且是严格的从1到m个买家卖出,c件商品的卖出顺序 第 j 个买家买了第 i 个商品,那么第 j +1 个买家必须买大于 i 的商品,商品可以不都卖出,问最多可以卖多少钱

使用DP ,maze[ i ] [ j ] 代表第 i 个买家买了第 j 件商品所得钱数,然后DP 即可得到最大钱数

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int maze[110][110];
int a[110][110];
int main()
{
	int c,m;
	scanf("%d %d",&c,&m);
	for(int i=1;i<=m;i++){
		for(int j=1;j<=c;j++){
			scanf("%d",&a[i][j]);
		}
	}
	
	memset(maze,0,sizeof(maze));
   	int temp=a[m][1];
    for(int i=1;i<=c;i++){
        temp=max(temp,a[m][i]);
		maze[m][i]=temp;
    }
    
    for(int i=m;i>=1;i--){
        for(int j=c;j>=1;j--){
			maze[i][j]=maze[i+1][j];
            for(int t=1;t<=j;t++)
                maze[i][j]=max(maze[i+1][j-t]+a[i][t],maze[i][j]);
        }
    }

	int maxn=0;
	for(int i=1;i<=c;i++)
		maxn=max(maxn,maze[1][i]);
		
	printf("%d\n",maxn);
	
	return 0;
 } 

猜你喜欢

转载自blog.csdn.net/dong_qian/article/details/82946969
DP
DP?