题解 | #购物单#

购物单

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

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String Nm = scan.nextLine().trim();
        int N = Integer.valueOf(Nm.split(" ")[0]); // N -> 总钱数
        int m = Integer.valueOf(Nm.split(" ")[1]); // m -> 可购买的物品的个数
        int[] prices = new int[m + 1]; // 定义一个整型数组,用于存放每一个物品的价格
        int[] importance = new int[m + 1]; // 定义一个整型数组,用于存放每一个物品的重要程度
        int[] devices = new int[m + 1]; // 定义一个整型数组,用于存放每一个物品到底是主件还是附件
        int masterDevices = 0; // 定义一个整型变量,用于存放主件的个数
        HashMap<Integer, ArrayList<Integer>> hashMap = new HashMap<>(); // 定义一个 HashMap,用于存放每一个主件,它的附件有哪些
        HashMap<Integer, ArrayList<ArrayList<Integer>>> masterGoods = new HashMap<>(); // 定义一个 HashMap,用于存放每一个主件,它的购买组合有哪一些
        for (int i = 1; i <= m; i++) { // 获取 m 个物品的信息
            String tmp = scan.nextLine();
            String[] strs = tmp.split(" ");
            prices[i] = Integer.valueOf(strs[0]); // 获取该物品的价格
            importance[i] = Integer.valueOf(strs[1]); // 获取该物品的重要程度
            devices[i] = Integer.valueOf(strs[2]); // 获取该物品到底是主件还是附件
            if (devices[i] != 0) { // 如果当前物品是附件
                ArrayList<Integer> tmpArr = new ArrayList<>();
                ArrayList<Integer> masterArr = hashMap.getOrDefault(devices[i], tmpArr);
                masterArr.add(i); // 将当前附件添加到其主件所拥有的附件列表中去
                hashMap.put(devices[i], masterArr);
            } else { // 如果当前物品是主件
                masterDevices++;
            }
        }
        for (int i = 1; i <= m; i++) {
            if (devices[i] == 0) { // 如果当前物品是主件
                ArrayList<ArrayList<Integer>> tmpArr = new ArrayList<>();
                ArrayList<ArrayList<Integer>> currentMasterGoods = masterGoods.getOrDefault(i, tmpArr);
                ArrayList<Integer> currentGoodsInfo = new ArrayList<>(); // 定义一个一维数组,用于存放当前 组合 的信息
                currentGoodsInfo.add(prices[i]); // 单个物品的价格
                currentGoodsInfo.add(importance[i] * prices[i]); // 单个物品所能带来的满足感
                currentMasterGoods.add(currentGoodsInfo); // 将当前组合添加到集合当中去
                ArrayList<Integer> tmp = new ArrayList<>();
                ArrayList<Integer> masterArr = hashMap.getOrDefault(i, tmp); // 获取当前主件所拥有的附件列表
                if (masterArr.size() == 1) { // 如果只有一个附件
                    currentGoodsInfo = new ArrayList<>();
                    currentGoodsInfo.add(prices[i] + prices[masterArr.get(0)]);
                    currentGoodsInfo.add(importance[i] * prices[i] + importance[masterArr.get(0)] * prices[masterArr.get(0)]);
                    currentMasterGoods.add(currentGoodsInfo);
                } else if (masterArr.size() == 2) { // 如果有两个附件
                    currentGoodsInfo = new ArrayList<>();
                    currentGoodsInfo.add(prices[i] + prices[masterArr.get(0)]);
                    currentGoodsInfo.add(importance[i] * prices[i] + importance[masterArr.get(0)] * prices[masterArr.get(0)]);
                    currentMasterGoods.add(currentGoodsInfo);
                    currentGoodsInfo = new ArrayList<>();
                    currentGoodsInfo.add(prices[i] + prices[masterArr.get(1)]);
                    currentGoodsInfo.add(importance[i] * prices[i] + importance[masterArr.get(1)] * prices[masterArr.get(1)]);
                    currentMasterGoods.add(currentGoodsInfo);
                    currentGoodsInfo = new ArrayList<>();
                    currentGoodsInfo.add(prices[i] + prices[masterArr.get(0)] + prices[masterArr.get(1)]);
                    currentGoodsInfo.add(importance[i] * prices[i] + importance[masterArr.get(0)] * prices[masterArr.get(0)] + importance[masterArr.get(1)] * prices[masterArr.get(1)]);
                    currentMasterGoods.add(currentGoodsInfo);
                }
                masterGoods.put(i, currentMasterGoods);
            }
        }
        int[][] dp = new int[masterDevices + 1][N + 1]; // 定义一个二维数组    dp[i][j] -> 当物品数为 i、金钱数为 j 时,所能获得的最大的满足度
        int index = 1;
        for (int Goods = 1; Goods <= m; Goods++) { // 当前的物品
            if (devices[Goods] != 0) { // 如果当前物品不是主件,直接跳过当次循环
                continue;
            }
            for (int Money = 0; Money <= N; Money++) { // 当前的金钱
                dp[index][Money] = dp[index - 1][Money]; // 对于当前的主件,我们可以选择不买
                ArrayList<ArrayList<Integer>> currentMasterGoods = masterGoods.get(Goods);
                for (ArrayList<Integer> tmpArr : currentMasterGoods) { // 获取每一种组合
                    if (tmpArr.get(0) <= Money) {
                        dp[index][Money] = Math.max(dp[index][Money], tmpArr.get(1) + dp[index - 1][Money - tmpArr.get(0)]);
                    }
                }
            }
            index++;
        }
        System.out.println(dp[masterDevices][N]); // 输出最终的结果
    }
}
全部评论

相关推荐

最近群里有很多同学找我看简历,问问题,主要就是集中在明年三月份的暑期,我暑期还能进大厂嘛?我接下来该怎么做?对于我来说,我对于双非找实习的一个暴论就是title永远大于业务,你在大厂随随便便做点慢SQL治理加个索引,可能就能影响几千人,在小厂你从零到一搭建的系统可能只有几十个人在使用,量级是不一样的。对双非来说,最难的就是约面,怎么才能被大厂约面试?首先这需要一点运气,另外你也需要好的实习带给你的背书。有很多双非的同学在一些外包小厂待了四五个月,这样的产出有什么用呢?工厂的可视化大屏业务很广泛?产出无疑是重要的,但是得当你的实习公司到了一定的档次之后,比如你想走后端,那么中厂后端和大厂测开的选择,你可以选择中厂后端(注意,这里的中厂也得是一些人都知道的,比如哈啰,得物,b站之类,不是说人数超过500就叫中厂),只有这个时候你再去好好关注你的产出,要不就无脑大厂就完了。很多双非同学的误区就在这里,找到一份实习之后,就认为自己达到了阶段性的任务,根本不再投递简历,也不再提升自己,玩了几个月之后,美其名曰沉淀产出,真正的好产出能有多少呢?而实际上双非同学的第一份实习大部分都是工厂外包和政府外包!根本无产出可写😡😡😡!到了最后才发现晚了,所以对双非同学来说,不要放过任何一个从小到中,从中到大的机会,你得先有好的平台与title之后再考虑你的产出!因为那样你才将将能过了HR初筛!我认识一个双非同学,从浪潮到海康,每一段都呆不久,因为他在不断的投递和提升自己,最后去了美团,这才是双非应该做的,而我相信大部分的双非同学,在找到浪潮的那一刻就再也不会看八股,写算法,也不会打开ssob了,这才是你跟别人的差距。
迷茫的大四🐶:我也这样认为,title永远第一,只有名气大,才有人愿意了解你的简历
双非本科求职如何逆袭
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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