hihoCoder 1416 --- Inventory is Full【贪心】

Inventory is Full
Description

You’re playing your favorite RPG and your character has just found a room full of treasures.
You have N inventory slots. Luckily, items of the same type stack together, with the maximum size of the stack depending on the type (e.g. coins might stack to 10, diamonds to 5, armor to 1, etc.). Each stack (or partial stack) takes up 1 inventory slot. Each item has a selling value (e.g. a single diamond might be worth 10, so a stack of 5 diamonds would be worth 50). You want to maximize the total selling value of the items in your inventory. Write a program to work it out.

Input

The first line contains 2 integers N and M denoting the number of inventory slots and the number of unique item types in the room.
The second line contains M integers, A1, A2, … AM, denoting the amounts of each type.
The third line contains M integers, P1, P2, … PM, denoting the selling value per item of each type.
The fourth line contains M integers, S1, S2, … SM, denoting the maximum stack size of each type.
For 80% of the data: 1 <= A i <= 100000
For 100% of the data: 1 <= N, M <= 10000 1 <= A i <= 1000000000 1 <= P i <= 1000000 1 <= S i <= 100

Output

The maximum total selling value.

Sample Input

5 10
10 10 10 10 10 10 10 10 10 10
1 2 3 4 5 6 7 8 9 10
10 9 8 7 6 5 4 3 2 1

Sample Output

146

题意

给定n个格子,m种物品,每种物品有相应的件数,价格,每个物品都规定一个格子只能装多少件。求出n个格子能装的最大价值。

思路

我们要让这些格子装得物品最大化,就需要对这些物品能分成几个格子装,然后对这些格子装得价值排序,进行贪心即可。

代码
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<map>
#include<queue>
#include<string>
#include<vector>
#include<utility>
#include<algorithm>
using namespace std;
const int MAXN=1e4+4;
typedef long long ll;
typedef pair<ll,int> pll;
int A[MAXN],P[MAXN],S[MAXN];
inline int read(){
	int s=0,w=1;
	char ch=getchar();
	for(;ch>'9' || ch<'0';ch=getchar()){
		if(ch=='-')	w=-1;
	}
	for(;ch<='9' && ch>='0';ch=getchar()){
		s=(s<<3)+(s<<1)+(ch^'0');
	}
	return s*w;
}
int main(){
	int N,M;
	N=read();M=read();
	for(int i=0;i<M;i++)	A[i]=read();
	for(int i=0;i<M;i++)	P[i]=read();
	for(int i=0;i<M;i++)	S[i]=read();
	vector<pll> v;
	for(int i=0;i<M;i++){
		int x=A[i]/S[i];
		int y=A[i]%S[i];
		if(x>0)
			v.push_back(pll((ll)S[i]*P[i],x));
		if(y>0)
			v.push_back(pll((ll)y*P[i],1));
	}
	sort(v.begin(),v.end());
	ll ans=0;
	for(int i=v.size()-1;i>=0;i--){
		int x=v[i].first;
		int y=v[i].second;
		if(N>y){
			ans+=(ll)x*y;
			N-=y;
		}else{
			ans+=(ll)N*x;
			break;
		}
	}
	cout<<ans<<endl;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星空皓月

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值