Inventory is Full

本文探讨了一个经典的背包问题,通过贪心算法来最大化角色在RPG游戏中找到的宝藏的总价值。角色拥有有限的库存空间,面对各种类型的物品,每种物品有其堆叠限制和出售价值。文章提供了一个Java程序示例,展示了如何计算在给定库存空间下,能够获得的最大总价值。

描述

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.  

输入

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 <= Ai <= 100000  
For 100% of the data: 1 <= N, M <= 10000 1 <= Ai <= 1000000000 1 <= Pi <= 1000000 1 <= Si <= 100  

输出

The maximum total selling value.

样例输入

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

样例输出

146 

贪心

import java.util.*;

public class Main{
    static class Node{
        int value;
        int num;
        public Node(int value,int num){
            this.value=value;
            this.num=num;
        }
    }

    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        int n=in.nextInt();
        int m=in.nextInt();
        int arr[][]=new int[3][m];
        for (int i = 0; i <3 ; i++) {
            for (int j = 0; j <m ; j++) {
                arr[i][j]=in.nextInt();
            }
        }
        int amount[]=arr[0];
        int value[]=arr[1];
        int maxStack[]=arr[2];

        PriorityQueue<Node> priorityQueue=new PriorityQueue<>(new Comparator<Node>() {
            @Override
            public int compare(Node o1, Node o2) {
                return o2.value-o1.value;
            }
        });
        for (int i = 0; i < m; i++) {
            int count1=amount[i]/maxStack[i];
            int v1=maxStack[i]*value[i];
            int v2=(amount[i]%maxStack[i])*value[i];
            priorityQueue.add(new Node(v1,count1));
            if(v2!=0)
                priorityQueue.add(new Node(v2,1));
        }

        long max=0;
        while (priorityQueue.size()>0 && n>0){
            Node node=priorityQueue.poll();
            int c=Integer.min(node.num,n);
            max+=(node.value*(long)c);
            n-=c;
        }
        System.out.println(max);

    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值