⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 3993504_ac_500ms_5464k.java

📁 北大大牛代码 1240道题的原代码 超级权威
💻 JAVA
字号:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import java.util.TreeMap;

public class Main {

	private Scanner in;
	private int m, n, k;
	private Cocktail[] car;
	private Ingredient[] iar;
	private TreeMap<String, Integer> tm;

	class Cocktail implements Comparable <Cocktail> {
		ArrayList<CockIngredient> e;
		String name;
		int mixedTime;
		int price;
		int id;

		public int compareTo(Cocktail that) {
			if (this.mixedTime != that.mixedTime) {
				return that.mixedTime - this.mixedTime;
			}
			return this.id - that.id;
		}

		public Cocktail(int id) {
			name = in.next();
			this.id = id;
			mixedTime = price = 0;
			int total = in.nextInt();

			e = new ArrayList<CockIngredient>();
			for (int i = 0; i < total; i++) {
				e.add(new CockIngredient());
			}
		}

		private void getPrice() {
			try {
				for (int i = 0; i < e.size(); i++) {
					price += iar[tm.get("-" + e.get(i).name)].price
							* e.get(i).need;
				}
				price = price * 3 + 10000;
			} catch (Exception e) {
				price = 0;
			}
		}

		private boolean mixable() {
			for (int i = 0; i < e.size(); i++) {
				if (!tm.containsKey("-" + e.get(i).name)
						|| e.get(i).need > iar[tm.get("-" + e.get(i).name)].volume) {
					return false;
				}
			}
			return true;
		}

		private void mix() {
			for (int i = 0; i < e.size(); i++) {
				iar[tm.get("-" + e.get(i).name)].volume -= e.get(i).need;
			}
			mixedTime++;
		}
	}

	class CockIngredient {
		String name;
		int need;

		public CockIngredient() {
			name = in.next();
			need = in.nextInt();
		}
	}

	class Ingredient {
		String name;
		int volume;
		int price;

		public Ingredient() {
			name = in.next();
			volume = in.nextInt();
			price = in.nextInt();
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new Main().run();
	}

	private void run() {
		in = new Scanner(System.in);
		int cas;

		cas = in.nextInt();
		for (int now = 1; now <= cas; now++) {
			System.out.println("Scenario " + now + " top cocktails:");
			solve();
		}
	}

	private void init() {
		tm = new TreeMap<String, Integer>();
		for (int i = 0; i < n; i++) {
			tm.put("+" + car[i].name, i);
		}

		for (int i = 0; i < m; i++) {
			tm.put("-" + iar[i].name, i);
		}
	}

	private void solve() {
		n = in.nextInt();
		car = new Cocktail[n];
		for (int i = 0; i < n; i++) {
			car[i] = new Cocktail(i);
		}

		m = in.nextInt();
		iar = new Ingredient[m];
		for (int i = 0; i < m; i++) {
			iar[i] = new Ingredient();
		}

		init();
		for (int i = 0; i < n; i++) {
			car[i].getPrice();
		}

		String last = "";
		k = in.nextInt();
		while (k-- != 0) {
			String order = in.next();

			if (!tm.containsKey("+" + order)) {
				continue;
			}

			int id = tm.get("+" + order);

			for (int i = 0; i < n; i++) {

				if (!car[id].name.equals(last) && car[id].mixable()) {
					car[id].mix();
					last = car[id].name;
					break;
				}
				id++;
				if (id == n) {
					id = 0;
				}
			}
		}
		Arrays.sort(car);
		for (int i = 0, j = 0; i < n && j < 10; i++, j++) {
			if (car[i].mixedTime == 0) {
				break;
			}
			System.out.println((j + 1) + " " + car[i].name + " "
					+ car[i].mixedTime + " " + car[i].price);
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -