【牛客竞赛】河北美食

题目链接:https://ac.nowcoder.com/acm/contest/903/K

【牛客竞赛】河北美食_第1张图片
【牛客竞赛】河北美食_第2张图片

Map,Key存储食材名字;Value 中 Integer[0]表示输入顺序,Integer[1]表示食材数量;接下来的操作就是对Value中的食材数量进行减法运算;最终结果通过Integer[0]的输入顺序进行排序输出。代码如下:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Map.Entry;

public class Main {
	public static void main(String[] args) {

		Scanner in = new Scanner(System.in);

		int n = in.nextInt();

		int m = in.nextInt();

		Map<String, Integer[]> map = new HashMap<>();

		for (int i = 0; i < n; i++) {
			map.put(in.next(), new Integer[] {i,in.nextInt()});
		}

		boolean flag = false;
		for (int i = 0; i < m; i++) {
			int k = in.nextInt();
			for(int j = 0;j<k;j++) {
				String s1 = in.next();
				int x = in.nextInt();
				if (map.get(s1)[1] >= x) {
					map.get(s1)[1]-=x;
				}
				else {
					flag = true;
					break;
				}
			}
			if (flag)
				break;
		}
		if (flag)
			System.out.println("NO");
		else {
			System.out.println("YES");
			
			List<Map.Entry<String, Integer[]>> list = new ArrayList<Map.Entry<String, Integer[]>>(map.entrySet());
			
			Collections.sort(list, new Comparator<Map.Entry<String, Integer[]>>() {

				@Override
				public int compare(Entry<String, Integer[]> o1, Entry<String, Integer[]> o2) {
					
					return o1.getValue()[0].compareTo(o2.getValue()[0]);
					
				}
			  
			});
			
			for (Map.Entry<String, Integer[]> mapping : list) {
				if(mapping.getValue()[1]>0)
					System.out.println(mapping.getKey() + " " + mapping.getValue()[1]);
			}
		}
	}
}

你可能感兴趣的:([牛客竞赛])