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;
}