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
|
#include<functional> #include<algorithm> #include<iostream> #include<utility> #include<vector> #include<queue> #include<map> #include<set> using namespace std;
struct INFO { char type; int size, price; }for_push_back; int N, T(0); char type[20]; vector<INFO> LIST; map<int, set<int>, greater<int> > BUY; map<int, set<int>, less<int> > SELL; map<int, int> BUY_VAL; map<int, int> SELL_VAL;
inline int sum(set<int>& now) { int re(0); for (const auto& r : now) re += LIST[r].size; return re; }
void trade(bool flag) { int bid_size, bid_price, ask_size, ask_price; while (true) { auto bid(BUY.begin()), ask(SELL.begin()); if (bid == BUY.end()) bid_size = 0, bid_price = 0; else bid_size = BUY_VAL[bid->first], bid_price = bid->first; if (ask == SELL.end()) ask_size = 0, ask_price = 999999; else ask_size = SELL_VAL[ask->first], ask_price = ask->first; if (bid_price < ask_price) { printf("QUOTE %d %d - %d %d\n", bid_size, bid_price, ask_size, (ask_price == 999999 ? 99999 : ask_price)); return; } auto sizeb(LIST[*bid->second.begin()].size), sizea(LIST[*ask->second.begin()].size); const auto mini(min(sizeb, sizea)); printf("TRADE %d %d\n", mini, flag ? ask_price : bid_price);
auto& b(bid->second), &a(ask->second); BUY_VAL[bid->first] -= mini; if (!b.empty() && (LIST[*b.begin()].size -= mini) == 0) b.erase(b.begin()); if (b.empty()) BUY.erase(bid); SELL_VAL[ask->first] -= mini; if (!a.empty() && (LIST[*a.begin()].size -= mini) == 0) a.erase(a.begin()); if (a.empty()) SELL.erase(ask); } }
int main() { #ifdef _XIENAOBAN_ #define gets(T) gets_s(T, 66666) freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); #endif
while (scanf("%d", &N) != EOF) { if (T++) puts(""); LIST.clear(), BUY.clear(), SELL.clear(), BUY_VAL.clear(), SELL_VAL.clear(); LIST.push_back(for_push_back); for (int n(1);n <= N;++n) { INFO tmp; scanf("%s", type); if (*type == 'C') { int id; scanf("%d", &id); auto& csl(LIST[id]); if (csl.type == 'B') { BUY_VAL[csl.price] -= csl.size; LIST[id].size = 0; auto& now(BUY[csl.price]); now.erase(id); if (now.empty()) BUY.erase(csl.price); } else { SELL_VAL[csl.price] -= csl.size; LIST[id].size = 0; auto& now(SELL[csl.price]); now.erase(id); if (now.empty()) SELL.erase(csl.price); } } else { tmp.type = *type; scanf("%d%d", &tmp.size, &tmp.price); if (*type == 'B') BUY[tmp.price].insert(n), BUY_VAL[tmp.price] += tmp.size; else SELL[tmp.price].insert(n), SELL_VAL[tmp.price] += tmp.size; } LIST.push_back(std::move(tmp)); trade(*type == 'B'); } } return 0; }
|