算法题——购买清单

xiaoxiao2021-02-28  75

题目描述:

过年啦!小B高兴的不行了,她收到了很多红包,可以实现好多的愿望呢。小B可是对商店货架上心仪的货物红眼好久了,只因囊中羞涩作罢,这次她可是要大大的shopping一番。小B想去购物时,总是习惯性的把要买的东西列在一个购买清单上,每个物品单独列一行(即便要买多个某种物品),这次也不例外。 

小B早早的来到了商店,由于她太激动,以至于她到达商店的时候,服务员还没有把各个商品的价签排好,所有的价签还都在柜台上。因此还需要一段时间,等服务器把价签放到对应的商品处,小B才能弄清她的购买清单所需的费用。 小B都有些迫不及待了,她希望你能够根据购买清单,帮她算算最好和最坏的情况下所需的费用,你能帮她吗?

输入中有多组测试数据。每组测试数据的第一行为两个整数n和m(1=<n, m=<1000),分别表示价签的数量以及小B的购买清单中所列的物品数。第二行为空格分隔的n个正整数,表示货架上各类物品的价格,每个数的大小不超过100000。随后的m行为购买清单中物品的名称,所有物品名称为非空的不超过32个拉丁字母构成的字符串,保证清单中不同的物品种类数不超过n,且商店有小B想要购买的所有物品。

输入:

5 3 4 2 1 10 5 apple orange mango 6 5 3 5 1 6 8 1 peach grapefruit banana orange orange

输出:

7 19 11 30

import java.util.*; public class Main{ public static void main(String[] args){ Scanner in = new Scanner(System.in); while(in.hasNext()){ int n = in.nextInt(); int m = in.nextInt(); int[] a = new int[n]; for(int i = 0; i < n; i++){ a[i] = in.nextInt(); } Map<String, Integer> goods = new HashMap<String, Integer>(); for(int i = 0; i < m; i++){ String goodname = in.next() ; if(goods.containsKey(goodname)){ goods.put(goodname, 1 +goods.get(goodname) ); }else{ goods.put(goodname, 1); } } Arrays.sort(a); int[] g = new int[goods.size()]; int t = 0; for(Map.Entry<String, Integer> entry: goods.entrySet()){ g[t] = entry.getValue(); t++; } Arrays.sort(g); int max = 0; int min =0; for(int i = 0; i < g.length; i++){ min += a[i] * g[g.length -1- i]; max += a[n-g.length+i] * g[i]; } System.out.println(min + " " + max); } } }

转载请注明原文地址: https://www.6miu.com/read-46955.html

最新回复(0)