1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
| public class Program { public static void Main(string[] args) { int[] arr = { 3, 3, 1, 2, 1, 2, 5 }; bool[] op = { true, true, true, true, false, true, false }; int k = 2;
var result = topK(arr, op, k); foreach (var list in result) { Console.WriteLine(string.Join(", ", list)); } } public static List<List<int>> topK(int[] arr, bool[] op,int k) { List<List<int>> ans = new List<List<int>>(); WhosPrize who = new WhosPrize(k); for (int i = 0; i < arr.Length; i++) { who.Operator(i, arr[i], op[i]); } return ans; }
}
class Customer:IComparable<Customer> { public int id; public int buy; public int enterTime; public Customer(int v,int b,int o) { id = v; buy = b; enterTime = o; }
public int CompareTo(Customer? other) { if (other == null) return 1;
if (this.buy != other.buy) { return other.buy.CompareTo(this.buy); }
return this.enterTime.CompareTo(other.enterTime); } } class WhosPrize { private Dictionary<int, Customer> customers; private EnhancedHeap<Customer> candHeap; private EnhancedHeap<Customer> prizeHeap; private int Limit; public WhosPrize(int Limit) { customers = new Dictionary<int, Customer>(); candHeap = new EnhancedHeap<Customer>(); prizeHeap = new EnhancedHeap<Customer>(); this.Limit = Limit; } public void Operator(int Time,int id,bool buyOrRefund) { if(!buyOrRefund&&!customers.ContainsKey(id)) return;
if (!customers.ContainsKey(id)) { customers.Add(id,new Customer(id,0,0)); } Customer c = customers[id];
if (buyOrRefund) c.buy++; else c.buy--;
if(c.buy == 0) customers.Remove(id); if(!candHeap.Contains(c) && !prizeHeap.Contains(c)) { if(prizeHeap.Count < Limit) { c.enterTime = Time; prizeHeap.Push(c); } else { c.enterTime = Time; candHeap.Push(c); } } else if (candHeap.Contains(c)) { if(c.buy == 0) customers.Remove(id) ; else { candHeap.Push(c); } } else { if (c.buy == 0) customers.Remove(id); else { prizeHeap.Push(c) ; } }
MaintainHeap(Time); }
private void MaintainHeap(int Time) { if (prizeHeap.IsEmpty) return; if (prizeHeap.Count < Limit) { Customer c = candHeap.Pop(); c.enterTime = Time; prizeHeap.Push(c); } else { if(candHeap.Peek().buy> prizeHeap.Peek().buy) { Customer cand = candHeap.Pop(); Customer prize = prizeHeap.Pop(); cand.enterTime = Time; prize.enterTime = Time; prizeHeap.Push(cand); candHeap.Push(prize);
} } } private List<int> Return() {
List<Customer> Temp = new List<Customer>(); List<int> result = new List<int>(); while(prizeHeap.Count > 0 && result.Count < Limit) { Customer c = prizeHeap.Pop(); Temp.Add(c); result.Add(c.id); } foreach(var i in Temp) { prizeHeap.Push(i); } return result; }
} class EnhancedHeap<T> where T : IComparable<T> { private List<T> heap; private Dictionary<T, int> indexMap;
public int Count => heap.Count; public bool IsEmpty => Count == 0;
public EnhancedHeap() { heap = new List<T>(); indexMap = new Dictionary<T, int>(); }
public void Push(T item) { if (indexMap.ContainsKey(item)) { throw new ArgumentException("堆中不允许重复元素"); }
heap.Add(item); indexMap[item] = heap.Count - 1; HeapInsert(heap.Count - 1); }
public T Pop() { if (IsEmpty) { throw new InvalidOperationException("堆为空"); }
T top = heap[0]; Remove(top); return top; }
public T Peek() { if (IsEmpty) { throw new InvalidOperationException("堆为空"); } return heap[0]; }
public void Update(T oldItem, T newItem) { if (!indexMap.TryGetValue(oldItem, out int index)) { throw new ArgumentException("元素不在堆中"); }
heap[index] = newItem; indexMap.Remove(oldItem); indexMap[newItem] = index;
int cmp = newItem.CompareTo(oldItem); if (cmp < 0) HeapInsert(index); else if (cmp > 0) Heapify(index); }
public void Remove(T item) { if (!indexMap.TryGetValue(item, out int index)) { return; }
Swap(index, heap.Count - 1); heap.RemoveAt(heap.Count - 1); indexMap.Remove(item);
if (index < heap.Count) { HeapInsert(index); Heapify(index); } }
private void HeapInsert(int index) { while (index > 0 && heap[index].CompareTo(heap[(index - 1) / 2]) < 0) { int parent = (index - 1) / 2; Swap(index, parent); index = parent; } }
private void Heapify(int index) { int left = 2 * index + 1; while (left < heap.Count) { int smallest = left; if (left + 1 < heap.Count && heap[left + 1].CompareTo(heap[left]) < 0) { smallest = left + 1; }
if (heap[index].CompareTo(heap[smallest]) <= 0) { break; }
Swap(index, smallest); index = smallest; left = 2 * index + 1; } } public bool Contains(T item) { return indexMap.ContainsKey(item); } private void Swap(int i, int j) { if (i == j) return;
(heap[i], heap[j]) = (heap[j], heap[i]); indexMap[heap[i]] = i; indexMap[heap[j]] = j; } }
|