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 | typedef struct Hash Hash; const int LM = 102; const int ALM = LM * LM; const int TLM = 1 << 15; const int MASK = TLM - 1; int R, C, N; // N:테이블 길이 int idCnt, cur; int tree[TLM + 5]; struct App { int id, runcnt; Hash* ptr; void setApp(int nid, int nr, Hash* np) { id = nid, runcnt = nr, ptr = np; } bool operator<(const App& r)const { if (id == 0 || r.id == 0) return id > r.id; if (runcnt != r.runcnt) return runcnt > r.runcnt; return id < r.id; } }idList[ALM], trr[ALM]; int bcnt; struct Hash { int appid, sn; Hash* prev, *next; void alloc(int nid, int nsn, Hash* p) { appid = nid, sn = nsn, prev = p, next = NULL; idList[sn].setApp(appid, 0, this), idCnt++; if (prev) next = prev->next, prev->next = this; if (next) next->prev = this; } void erase() { idList[sn].setApp(0, 0, NULL), idCnt--; if (prev) prev->next = next; if (next) next->prev = prev; prev = next = NULL; } }buf[TLM * 10], htab[TLM]; void buildTree(int now, int s, int e) { if (s == e) { tree[now] = s; return; } int lch = now * 2, rch = lch + 1; int m = (s + e) / 2; buildTree(lch, s, m); buildTree(rch, m + 1, e); tree[now] = tree[lch]; } void init(int row, int col) { R = row, C = col; N = row * col; buildTree(1, 0, N - 1); cur = 0; idCnt = 0; int i; for (i = 0; i < ALM; ++i) idList[i].setApp(0, 0, NULL); bcnt = 0; for (i = 0; i < TLM; ++i) htab[i].next = NULL; } Hash* probing(int hidx, int appid) { Hash* p = htab[hidx].next; for (; p; p = p->next) { if (p->appid == appid) return p; } return NULL; } inline int min(int a, int b) { return a < b ? a : b; } int query(int now, int s, int e, int si, int ei) { if (e < si || ei < s) return TLM; if (si <= s && e <= ei) return tree[now]; int lch = now * 2, rch = lch + 1, m = (s + e) / 2; int lv = query(lch, s, m, si, ei); int rv = query(rch, m + 1, e, si, ei); return min(lv, rv); } void update(int now, int s, int e, int tg, int w) { if (s == e) { tree[now] = w; return; } int lch = now * 2, rch = lch + 1, m = (s + e) / 2; if (tg <= m) update(lch, s, m, tg, w); else update(rch, m + 1, e, tg, w); tree[now] = min(tree[lch], tree[rch]); } int installApp(int appid) { /// 테이블에 빈 공간이 없는 경우 if (idCnt == N) return 0; int hidx = appid & MASK; /// 이미 설치된 앱 if (probing(hidx, appid)) return 0; /// 빈 공간 찾기 int tg = query(1, 0, N - 1, cur, N - 1); if (tg == TLM) { tg = query(1, 0, N - 1, 0, cur - 1); } cur = tg; buf[bcnt++].alloc(appid, cur, htab + hidx); update(1, 0, N - 1, cur, TLM); return 1; } int uninstallApp() { if (idList[cur].id == 0) return 0; int ret = idList[cur].id; idList[cur].ptr->erase(); update(1, 0, N - 1, cur, cur); return ret; } void moveDirection(int dir) { if (dir == 0) cur = (cur - C + N) % N; else if (dir == 1) { cur = (cur + C) % N; } else if (dir == 2) { cur = (cur - 1 + N) % N; } else if (dir == 3) { cur = (cur + 1) % N; } } int moveToApp(int appid) { int hidx = appid & MASK; Hash* p = probing(hidx, appid); if (p == NULL) return 0; cur = p->sn; return 1; } int runApp() { if (idList[cur].id == 0) return 0; idList[cur].runcnt++; return idList[cur].id; } void mergeSort(App* arr, int s, int e) { if (s >= e) return; int m = (s + e) / 2, i = s, j = m + 1, k = s; mergeSort(arr, s, m); mergeSort(arr, m + 1, e); for (; k <= e; ++k) { if (i > m) trr[k] = arr[j++]; else if (j > e) trr[k] = arr[i++]; else if (arr[i] < arr[j]) trr[k] = arr[i++]; else trr[k] = arr[j++]; } for (i = s; i <= e; ++i) arr[i] = trr[i]; } void sortApp() { mergeSort(idList, 0, N - 1); buildTree(1, 0, N - 1); for (int i = 0; i < idCnt; ++i) { if (idList[i].ptr) { idList[i].ptr->sn = i; update(1, 0, N - 1, i, TLM); } } cur = 0; } | cs |