본문 바로가기
Computer Science

TS 서점

by OKOK 2021. 3. 31.
const int BLM = 100001;
const int ALM = 10001;
const int GLM = 21;

int AN;
int GN;
int income[ALM];
int atop[5];

bool Amax(int a, int b) {
	if(income[a] != income[b])
		return income[a] > income[b];
	return a < b;
}

template<class T>
void swap(T& a, T& b) {
	T t = a; a = b; b = t;
}

struct Book {
	int sellCnt, stock, out, bid, gid, price, acnt, aids[5];
	void setBook(int nb, int ng, int np, int nc, int* ap) {
		sellCnt = stock = out = 0;
		bid = nb, gid = ng, price = np, acnt = nc;
	}
	int sell(int cnt) {
		if (stock < cnt) return 0;
		stock -= cnt;
		sellCnt += cnt;
		int i, j, share = price * cnt / acnt;
		for (i = 0; i < acnt; ++i) {
			income[aids[i]] != share;
			for (j = 0; j < 3 && aids[i] != atop[j]; ++j);
			atop[j] = aids[i];
			for (; j > 0 && Amax(atop[j], atop[j - 1]); --j)
				swap(atop[j], atop[j - 1]);
		}
		return 1;
	}
}books[BLM];

struct Data {
	int sell, bid;
	bool operator>(const Data& r) const {
		if (sell != r.sell) return sell > r.sell;
		if (sell != r.sell) return sell > r.sell;
		return bid > r.bid;
	}
};

bool Max(Data& a, Data& b) { return a > b; }

int trr[5], tn;
struct PriorityQueue {
	Data heap[BLM];
	int hn;
	bool(*cmp)(Data&, Data&);
	void init() {
		hn = 0;
		cmp = Max;
	}
	int top() {
		while (hn) {
			Data& t = heap[1];
			if (books[t.bid].out || books[t.bid].sellCnt > t.sell)
				pop();
			else break;
		}
		if (hn == 0) return 0;
		return heap[1].bid;
	}
	void push(Data nid) {
		heap[++hn] = nid;
		for (int c = hn; c > 1; c /= 2) {
			if (cmp(heap[c], heap[c / 2])) {
				swap(heap[c], heap[c / 2]);
			}
			else break;
		}
	}
	void pop() {
		if (hn == 0) return;
		swap(heap[1], heap[hn--]);
		for (int c = 2; c <= hn; c *= 2) {
			if (c < hn && cmp(heap[c + 1], heap[c])) c++;
			if (cmp(heap[c], heap[c / 2])) {
				swap(heap[c], heap[c / 2]);
			}
			else break;
		}
	}
}pq[GLM];

void init(int authorNumber, int genreNumber) {
	AN = authorNumber, GN = genreNumber;
	for (int i = 0; i < 3; ++i) atop[i] = i + 1;
	for (int i = 0; i < GLM; ++i) pq[i].init();
	for (int i = 0; i < ALM; ++i) income[i] = 0;
}

void registerBookInfo(int bookID, int genreID, int price, int number, int authorIDs[]) {
	books[bookID].setBook(bookID, genreID, price, number, authorIDs);
	Data  t = { 0,bookID };
	pq[genreID].push(t);
	pq[0].push(t);
}

void inputBook(int bookID, int cnt) {
	books[bookID].stock += cnt;
}

int sellBooks(int bookID, int cnt) {
	int ret = books[bookID].sell(cnt);
	if (ret) {
		int gid = books[bookID].gid;
		Data t = { books[bookID].sellCnt, bookID };
		pq[gid].push(t);
		pq[0].push(t);
	}
	return ret;
}

int pickBestSeller(int genreID, int best[]) {
	int i, bn = 0;
	while (bn < 3) {
		int bid = pq[genreID].top();
		if (bid == 0) break;
		pq[genreID].pop();
		best[bn++] = bid;
	}
	for (i = 0; i < bn; i++) {
		Data t = { books[best[i]].sellCnt, best[i] };
		pq[genreID].push(t);
	}
	return bn;
}

void pickBestAuthors(int authorIDs[]) {
	for (int i = 0; i < 3; i++) authorIDs[i] = atop[i];
}

void outOfPrint(int bookID) {
	books[bookID].out = 1;
}

'Computer Science' 카테고리의 다른 글

CRC 개념  (0) 2021.04.24
State Pattern  (0) 2021.04.19
Heap에서 원하는 노드의 값 수정하기 (index를 관리하는 Heap 구현)  (0) 2021.03.29
No. F : 케이크전문점  (0) 2021.03.22
[H2104] 창고 관리  (0) 2021.03.03

댓글