暴力求解
超时
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let money = 0
let inputCount = 0
let needLine = 0
let prices = []
let satisfy = []
let belonged = []
let dp = {}
function fn(money, arr){
let key = arr.join('-')
let res = 0;
if(arr.length <= 0){
return res;
}
if(dp[key] != null){
return dp[key]
}
for(let i = 0; i < arr.length; i++){
let pos = arr[i] - 1
let pos1 = - 1
let weight = prices[pos] * satisfy[pos];
let curMoney = prices[pos]
let nextArr = []
if(belonged[pos] !== 0){
pos1 = belonged[pos] - 1
}
for(let j = 0; j < arr.length; j ++){
if(arr[j] !== pos + 1 && arr[j] !== pos1 + 1){
nextArr.push(arr[j])
}
if(arr[j] === pos1 + 1){
weight = weight + prices[pos1] * satisfy[pos1]
curMoney = curMoney + prices[pos1]
}
}
if(money - curMoney >= 0 && nextArr.length){
res = Math.max(res, fn(money - curMoney, nextArr) + weight)
}
}
dp[key] = res
return res
}
rl.on('line', function (line) {
const tokens = line.split(' ');
inputCount++
if(inputCount === 1){
money = parseInt(tokens[0])
needLine = parseInt(tokens[1])
}else{
prices.push(+tokens[0])
satisfy.push(+tokens[1])
belonged.push(+tokens[2])
if(inputCount === needLine + 1){
console.log(fn(money, prices.map((i, index) => index + 1)))
}
}
});
动态规划
通过
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let money = 0;
let inputCount = 0;
let needLine = 0;
let goods = {};
let appendGoods = {};
let dp = [];
let w = [];
let v = [];
function fn() {
let res = 0;
for (let i in goods) {
let tmpW = [];
let tmpV = [];
tmpW.push(goods[i][0] / 10);
tmpV.push(goods[i][0] * goods[i][1]);
if (appendGoods[i] && appendGoods[i].length >= 1) {
tmpW.push(tmpW[0] + appendGoods[i][0][0] / 10);
tmpV.push(tmpV[0] + appendGoods[i][0][0] * appendGoods[i][0][1]);
if (appendGoods[i].length > 1) {
tmpW.push(tmpW[0] + appendGoods[i][1][0] / 10);
tmpV.push(tmpV[0] + appendGoods[i][1][0] * appendGoods[i][1][1]);
tmpW.push(tmpW[1] + appendGoods[i][1][0] / 10);
tmpV.push(tmpV[1] + appendGoods[i][1][0] * appendGoods[i][1][1]);
}
}
w.push(tmpW)
v.push(tmpV)
}
for(let i = 1; i <= w.length; i++){
for(let j = 0; j <= (money / 10); j++){
if(!dp[i - 1]){
dp[i - 1] = []
}
if(!dp[i - 1][j]){
dp[i - 1][j] = 0
}
let maxVal = dp[i - 1][j];
for(let k = 0; k < w[i - 1].length; k++){
let leftVal = j - w[i - 1][k]
if( leftVal >= 0){
maxVal = Math.max(maxVal, dp[i - 1][leftVal] + v[i - 1][k])
}
}
if(!dp[i]){
dp[i] = []
}
dp[i][j] = maxVal
}
}
return dp[w.length][Math.floor(money / 10)];
}
rl.on("line", function (line) {
const tokens = line.split(" ");
inputCount++;
if (inputCount === 1) {
money = parseInt(tokens[0]);
needLine = parseInt(tokens[1]);
} else {
if (+tokens[2]) {
if (appendGoods[+tokens[2]]) {
appendGoods[+tokens[2]].push([+tokens[0], +tokens[1]]);
} else {
appendGoods[+tokens[2]] = [[+tokens[0], +tokens[1]]];
}
} else {
goods[inputCount - 1] = [+tokens[0], +tokens[1]];
}
if (inputCount === needLine + 1) {
console.log(fn());
}
}
});