Inventory is Full

描述

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

    }
}

猜你喜欢

转载自blog.csdn.net/weixin_38970751/article/details/85531865