笔试的时候,没有考虑到购买多样水果的时候,每样水果的重复次数,因此没能AC。
现改正,测试了几个用例。
public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String[]s1 = sc.nextLine().split(" "); //价格数目 int n = Integer.valueOf(s1[0]); //物品数目 int m = Integer.valueOf(s1[1]); String[]prices = sc.nextLine().split(" "); int[]p = new int[n]; for(int i=0;i<prices.length;i++){ p[i] = Integer.valueOf(prices[i]); } String[]goods = new String[m]; HashMap<String,Integer> hm = new HashMap<String,Integer>(); for(int i=0;i<m;i++){ goods[i] = sc.nextLine(); if(hm.containsKey(goods[i])) hm.put(goods[i],hm.get(goods[i])+1); else hm.put(goods[i],1); } Arrays.sort(p); int max=0,min=0; int[]arr = new int[hm.size()]; int ind=0; Iterator it = hm.entrySet().iterator(); while(it.hasNext()){ Map.Entry entry = (Map.Entry) it.next(); arr[ind++] = (int)entry.getValue(); } Arrays.sort(arr); for(int i=0;i<hm.size();i++){ min += p[i]*arr[hm.size()-1-i]; } for(int i=0;i<hm.size();i++){ max += p[p.length-1-i]*arr[hm.size()-1-i]; } System.out.println(min+" "+max); } }
时间: 2024-10-14 18:13:45