|
| 1 | +```java |
| 2 | +import java.util.*; |
| 3 | + |
| 4 | +class UserSolution |
| 5 | +{ |
| 6 | + static class Product implements Comparable<Product>{ |
| 7 | + int mID; |
| 8 | + int mPrice; |
| 9 | + |
| 10 | + Product(int mID, int mPrice){ |
| 11 | + this.mID = mID; |
| 12 | + this.mPrice = mPrice; |
| 13 | + } |
| 14 | + @Override |
| 15 | + public int compareTo(Product other){ |
| 16 | + if(this.mPrice == other.mPrice){ |
| 17 | + return Integer.compare(this.mID, other.mID); |
| 18 | + }else{ |
| 19 | + return Integer.compare(this.mPrice, other.mPrice); |
| 20 | + } |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + static class ProductInfo{ |
| 25 | + int mCategory; |
| 26 | + int mCompany; |
| 27 | + |
| 28 | + ProductInfo(int mCategory, int mCompany){ |
| 29 | + this.mCategory = mCategory; |
| 30 | + this.mCompany = mCompany; |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + static Map<Integer, ProductInfo> productInfoByID; |
| 35 | + static Map<Integer, Product> productByID; |
| 36 | + static TreeSet<Product>[][] productByCateCom; |
| 37 | + static int[][] diff; |
| 38 | + |
| 39 | + public void init() |
| 40 | + { |
| 41 | + productInfoByID = new HashMap<>(); |
| 42 | + productByID = new HashMap<>(); |
| 43 | + productByCateCom = new TreeSet[6][6]; |
| 44 | + for (int i = 1; i <= 5; i++) { |
| 45 | + for (int j = 1; j <= 5; j++) { |
| 46 | + productByCateCom[i][j] = new TreeSet<>(); |
| 47 | + } |
| 48 | + } |
| 49 | + diff = new int[6][6]; |
| 50 | + } |
| 51 | + |
| 52 | + public int sell(int mID, int mCategory, int mCompany, int mPrice) |
| 53 | + { |
| 54 | + Product newProduct = new Product(mID, mPrice - diff[mCategory][mCompany]); |
| 55 | + ProductInfo newProductInfo = new ProductInfo(mCategory, mCompany); |
| 56 | + |
| 57 | + productInfoByID.put(mID, newProductInfo); |
| 58 | + productByID.put(mID, newProduct); |
| 59 | + productByCateCom[mCategory][mCompany].add(newProduct); |
| 60 | + |
| 61 | + return productByCateCom[mCategory][mCompany].size(); |
| 62 | + } |
| 63 | + |
| 64 | + public int closeSale(int mID) |
| 65 | + { |
| 66 | + if(!productInfoByID.containsKey(mID)) return -1; |
| 67 | + |
| 68 | + ProductInfo info = productInfoByID.get(mID); |
| 69 | + Product product = productByID.get(mID); |
| 70 | + |
| 71 | + int realPrice = product.mPrice + diff[info.mCategory][info.mCompany]; |
| 72 | + |
| 73 | + productByCateCom[info.mCategory][info.mCompany].remove(product); |
| 74 | + productInfoByID.remove(mID); |
| 75 | + productByID.remove(mID); |
| 76 | + |
| 77 | + return realPrice; |
| 78 | + } |
| 79 | + |
| 80 | + public int discount(int mCategory, int mCompany, int mAmount) |
| 81 | + { |
| 82 | + diff[mCategory][mCompany] -= mAmount; |
| 83 | + |
| 84 | + TreeSet<Product> categoryProducts = productByCateCom[mCategory][mCompany]; |
| 85 | + |
| 86 | + while (!categoryProducts.isEmpty()) { |
| 87 | + Product first = categoryProducts.first(); |
| 88 | + if (first.mPrice + diff[mCategory][mCompany] <= 0) { |
| 89 | + categoryProducts.pollFirst(); |
| 90 | + productInfoByID.remove(first.mID); |
| 91 | + productByID.remove(first.mID); |
| 92 | + } else { |
| 93 | + break; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return categoryProducts.size(); |
| 98 | + } |
| 99 | + |
| 100 | + Solution.RESULT show(int mHow, int mCode) |
| 101 | + { |
| 102 | + Solution.RESULT res = new Solution.RESULT(); |
| 103 | + res.cnt = 0; |
| 104 | + |
| 105 | + TreeSet<Product> candidates = new TreeSet<>((p1, p2) -> { |
| 106 | + int price1 = p1.mPrice + diff[productInfoByID.get(p1.mID).mCategory][productInfoByID.get(p1.mID).mCompany]; |
| 107 | + int price2 = p2.mPrice + diff[productInfoByID.get(p2.mID).mCategory][productInfoByID.get(p2.mID).mCompany]; |
| 108 | + |
| 109 | + if (price1 == price2) { |
| 110 | + return Integer.compare(p1.mID, p2.mID); |
| 111 | + } |
| 112 | + return Integer.compare(price1, price2); |
| 113 | + }); |
| 114 | + |
| 115 | + for (int i = 1; i <= 5; i++) { |
| 116 | + if (mHow == 1 && i != mCode) continue; |
| 117 | + |
| 118 | + for (int j = 1; j <= 5; j++) { |
| 119 | + if (mHow == 2 && j != mCode) continue; |
| 120 | + |
| 121 | + TreeSet<Product> categoryProducts = productByCateCom[i][j]; |
| 122 | + if (categoryProducts.isEmpty()) continue; |
| 123 | + |
| 124 | + for (Product p : categoryProducts) { |
| 125 | + int realPrice = p.mPrice + diff[i][j]; |
| 126 | + if (realPrice <= 0) continue; |
| 127 | + |
| 128 | + if (candidates.size() < 5) { |
| 129 | + candidates.add(p); |
| 130 | + } else { |
| 131 | + Product last = candidates.last(); |
| 132 | + int lastPrice = last.mPrice + diff[productInfoByID.get(last.mID).mCategory][productInfoByID.get(last.mID).mCompany]; |
| 133 | + |
| 134 | + if (realPrice < lastPrice || (realPrice == lastPrice && p.mID < last.mID)) { |
| 135 | + candidates.pollLast(); |
| 136 | + candidates.add(p); |
| 137 | + } else { |
| 138 | + break; |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + for (Product p : candidates) { |
| 146 | + res.IDs[res.cnt++] = p.mID; |
| 147 | + } |
| 148 | + |
| 149 | + return res; |
| 150 | + } |
| 151 | +} |
| 152 | +``` |
0 commit comments