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
| import java.util.ArrayList; import java.util.Comparator; import java.util.HashMap; import java.util.List;
public class LotteryGameMethod2 { public static List<List<Integer>> getLotteryUidList(int[] ids, int[] ops, int k){ HashMap<Integer,User> map = new HashMap<>(); MyHeap<User> lotteryArea = new MyHeap<>((a,b)->a.cnt!= b.cnt?b.cnt-a.cnt:b.time-a.time), candidateArea = new MyHeap<>((a,b)->a.cnt!=b.cnt?a.cnt-b.cnt:b.time-a.time); List<List<Integer>> ans = new ArrayList<>(); int i=0,len=ids.length; for(;i<len;++i){ User user = map.get(ids[i]); if(!(user==null && ops[i] ==0 )){ if(user == null){ user = new User(ids[i],i); map.put(ids[i],user); } if(ops[i]==0)--user.cnt; else ++user.cnt; if(user.cnt == 0){ map.remove(ids[i]); lotteryArea.remove(user); candidateArea.remove(user); if (lotteryArea.size()<k && candidateArea.size() >0) { User poll = candidateArea.poll(); poll.time = i; lotteryArea.add(poll); } }else{ if(lotteryArea.size()<k){ if(!lotteryArea.contains(user))lotteryArea.add(user); else lotteryArea.update(user); }else{ if(!lotteryArea.contains(user) && !candidateArea.contains(user)){ candidateArea.add(user); } candidateArea.update(user); lotteryArea.update(user); if(candidateArea.size() > 0 && candidateArea.peek().cnt > lotteryArea.peek().cnt){ User candidate = candidateArea.poll(); User lottery = lotteryArea.poll(); candidate.time = lottery.time = i; candidateArea.add(lottery); lotteryArea.add(candidate); } } } } List<Integer> ansItem = new ArrayList<>(lotteryArea.size()); for (User lotteryUser : lotteryArea.getAll()) { ansItem.add(lotteryUser.id); } ans.add(ansItem); } return ans; }
static class User{ int id, cnt,time; public User(int id,int t){ this.id=id; time=t; } }
static class MyHeap<T>{
private ArrayList<T> arrayList;
private HashMap<T,Integer> indexMap;
private Comparator<T> comparator;
private int size;
public MyHeap(Comparator<T> comparator){ this.comparator = comparator; arrayList = new ArrayList<>(); indexMap = new HashMap<>(); }
public void add(T data){ indexMap.put(data,size); arrayList.add(data); heapInsert(size++); }
public void remove(T data){ Integer index = indexMap.get(data); if(index!=null){ if(index == size-1){ arrayList.remove(--size); }else { swap(arrayList.get(index),arrayList.get(--size)); resign(index); arrayList.remove(size); } indexMap.remove(data); } }
public T poll(){ T data = arrayList.get(0); --size; if(size==0){ arrayList.remove(0); }else{ swap(arrayList.get(0),arrayList.get(size)); resign(0); arrayList.remove(size); } indexMap.remove(data); return data; }
public T peek(){ return arrayList.get(0); }
public boolean contains(T data){ return indexMap.containsKey(data); }
public void update(T data){ Integer index = indexMap.get(data); if(index!=null)resign(index); }
public int size(){ return size; }
public ArrayList<T> getAll(){ return arrayList; }
private void resign(int index){ heapInsert(index); heapify(index); }
private void heapInsert(int index){ int parent; while(comparator.compare(arrayList.get(index),arrayList.get(parent = (index-1)/2)) > 0){ swap(arrayList.get(index),arrayList.get(parent)); index = parent; } }
private void heapify(int index){ int child, bnd = size>>1; while(index<bnd){ child = (index<<1)|1; if(child+1<size && comparator.compare(arrayList.get(child),arrayList.get(child+1)) < 0)++child; if(comparator.compare(arrayList.get(index),arrayList.get(child)) < 0){ swap(arrayList.get(index),arrayList.get(child)); index = child; }else return; } }
private void swap(T t1, T t2){ Integer index1 = indexMap.get(t1), index2 = indexMap.get(t2); arrayList.set(index1,t2); arrayList.set(index2,t1); indexMap.put(t1,index2); indexMap.put(t2,index1); } }
public static void main(String[] args) { int[] ids = {1,2,3,3,2,4,5,7,1,1,2,2,2}, ops={1,1,1,1,1,1,1,1,0,1,0,0,0}; int k = 2;
List<List<Integer>> lotteryUidList = getLotteryUidList(ids, ops, k); for (List<Integer> integers : lotteryUidList) { System.out.println(integers); } } }
|