AcWing 5. 奶酪宝宝来了!看完y总的课真是太透彻啦哈哈哈!
原题链接
中等
作者:
奶酪宝宝
,
2025-04-23 20:14:37
· 山东
,
所有人可见
,
阅读 3
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int maximumNumber = 1000 + 10;
const int maximumVolume = 2000 + 10;
struct Good{
int kindVolume, kindValue;
};
int kindVolume, kindValue, kindNumber;
int totalNumber, totalVolume;
void initialization(){
scanf("%d%d", &totalNumber, &totalVolume);
}
void inputAKind(){
scanf("%d%d%d", &kindVolume, &kindValue, &kindNumber);
}
vector<Good> goods;
void splitAStuff(){
for (int item = 1; item <= kindNumber; item *= 2){
kindNumber -= item;
goods.push_back({item * kindVolume, item * kindValue});
}
if (kindNumber > 0) goods.push_back({kindNumber * kindVolume, kindNumber * kindValue});
}
void splitAll(){
for (int counter = 0; counter < totalNumber; counter ++){
inputAKind();
splitAStuff();
}
}
int DPRecord[maximumVolume];
void DPKnapsack(){
for (int numbers = 0; numbers < goods.size(); numbers ++){
int currentVolume = goods[numbers].kindVolume;
int currentValue = goods[numbers].kindValue;
for (int volumes = totalVolume; volumes >= currentVolume; volumes --){
DPRecord[volumes] = max(DPRecord[volumes], DPRecord[volumes - currentVolume] + currentValue);
}
}
}
void outputResult(){
printf("%d", DPRecord[totalVolume]);
}
int main(){
initialization();
splitAll();
DPKnapsack();
outputResult();
return 0;
}