Untitled
unknown
plain_text
a year ago
2.3 kB
1
Indexable
Never
package Electronic_Market; import java.util.PriorityQueue; import java.util.TreeSet; class UserSolution { class Component implements Comparable<Component> { int type; int perf; int price; public Component(int type, int perf, int price) { super(); this.type = type; this.perf = perf; this.price = price; } @Override public int compareTo(Component o) { if (o.perf == perf) { return price - o.price; } return o.perf - perf; } } int charge; int maxPerformance; TreeSet<Component>[] warehouse = new TreeSet[3]; int[][] num_component; void init(int mCharge) { charge = mCharge; maxPerformance=0; for (int i = 0; i < 3; i++) { warehouse[i] = new TreeSet<UserSolution.Component>(); } num_component = new int[2][3]; return; } int stock(int mType, int mPrice, int mPerformance, int mPosition) { num_component[mPosition][mType]++; Component com = new Component(mType, mPerformance, mPrice); if(mPerformance<mPerformance){ maxPerformance=mPerformance; } warehouse[2].add(com); warehouse[mPosition].add(com); return num_component[mPosition][mType]; } main.Result order(int mBudget) { main.Result res = new main.Result(); Component com1 = search(warehouse[0], mBudget); Component com2 = search(warehouse[1], mBudget); Component com3 = search(warehouse[2], mBudget - charge); com3.price += charge; PriorityQueue<Component> pq = new PriorityQueue<>(); pq.add(com1); pq.add(com2); pq.add(com3); res.mPerformance = pq.peek().perf; res.mPrice = pq.peek().price; return res; } Component search(TreeSet<Component> ts, int mBudget) { int[] price = new int[3]; for (int i = 0; i < 3; i++) { price[i] = 999999; } int performance = 0; for (Component com : ts) { if (price[0] + price[1] + price[2] <= mBudget && com.perf < performance) { break; } if (com.price < price[com.type]) { price[com.type] = com.price; performance = com.perf; } } if (price[0] + price[1] + price[2] <= mBudget) { Component res = new Component(0, performance, price[0] + price[1] + price[2]); return res; } else { Component res = new Component(0, 0, 0); return res; } } }