题解 | #购物单#

购物单

https://www.nowcoder.com/practice/f9c6f980eeec43ef85be20755ddbeaf4

// 1000 5
// 800 2 0
// 400 5 1
// 300 5 1
// 400 3 0
// 500 2 0
const readline = require('readline')
const r1= readline.createInterface({
  input:process.stdin,
  output:process.stdout
})
let condition = null
let lines = []
r1.on('line',(line)=>{
  if(!condition){
    condition = line.split(' ').map(Number)
  }else{
    lines.push(line.split(' ').map(Number))
    if(lines.length===condition[1]){
      r1.close()
    }
  }
})

r1.on('close',()=>{
  let [sum,total] = condition
  sum = sum /10
  let map = {}
  let map2 = {}
  lines.forEach((item,index)=>{
    let price = item[0]
    let weight = item[0]*item[1]
    if(!item[2]){
      map[index]?map[index]=[[price,weight]]||[]:map[index]=[[price,weight]]
    }else{
      map2[item[2]]?map2[item[2]].push([price,weight]):map2[item[2]] = [[price,weight]]
    }
  })
  for(let k in map2){
    map2[k].forEach(item=>{
      let temp = []
      map[+k-1].forEach(value=>{
        temp.push([value[0]+item[0],value[1]+item[1]])
      })
      map[+k-1].push(...temp)
    })
  }
  let newArr = []
  for(let k in map){
    newArr.push(map[k])
  }
  getMax(sum,newArr)
})

function getMax(sum,newArr){
  let dp = new Array(newArr.length)
  for(let i =0;i<dp.length;i++){
    dp[i]=new Array(sum+1).fill(0)
  }
  for(let i = 0;i<dp[0].length;i++){
    let money = 10*i
    let weight = 0
    let arr = newArr[0].filter(item=>{return item[0]<=money})
    arr=arr.map(item=>item[1])
    if(arr.length>0){
      weight = Math.max(...arr)
    }
    dp[0][i]=weight
  }
  for(let i =1;i<dp.length;i++){
    for(let j=1;j<dp[i].length;j++){
      let top = dp[i-1][j]
      let temparr = []
      newArr[i].forEach(item=>{
        let mp =j-item[0]/10
        if(mp>=0){
          temparr.push(dp[i-1][mp]+item[1])
        }
      })
      dp[i][j] = Math.max(top,...temparr)
    }
  }
  console.log(dp[dp.length-1][dp[0].length-1])
}

全部评论

相关推荐

牛客38347925...:9,2学生暑期实习失利开始投小厂,给这群人整自信了
点赞 评论 收藏
分享
不要停下啊:大二打开牛客,你有机会开卷了,卷起来,去找课程学习,在牛客上看看大家面试笔试都需要会什么,岗位有什么需求就去学什么,努力的人就一定会有收获,这句话从来都经得起考验,像我现在大三了啥也不会,被迫强行考研,炼狱难度开局,啥也不会,找工作没希望了,考研有丝丝机会
点赞 评论 收藏
分享
大疆在线测评都考什么呀,会考企业概况啥的吗
又被画饼了的做题家很...:不会。刚做完,就是材料分析、态度题、算术题、逻辑题。总共60道。
投递大疆等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务