#include <iostream>
#include <istream>
#include <sstream>
#include <vector>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <numeric>
#include <chrono>
#include <ctime>
#include <cmath>
#include <cctype>
#include <string>
#include <cstdio>
#include <iomanip>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <functional>
#include <iterator>
using namespace std;
const int MAXSIZE = 1e4 + 7;
struct Node
{
int price;
int cnt;
int id;
int type;
Node() {}
Node(int a, int b,int i,int t) :price(a),cnt(b),id(i),type(t){}
inline friend bool operator<(const Node& a, const Node& b) {
if (a.price != b.price) return a.price < b.price;
return a.id > b.id;
}
inline friend bool operator>(const Node& a, const Node& b) {
if (a.price != b.price) return a.price > b.price;
return a.id > b.id;
}
};
char vis[MAXSIZE];
priority_queue<Node> buy;
priority_queue<Node, vector<Node>, greater<Node>> sell;
Node orders[MAXSIZE];
map<int, int> buyCnt;
map<int, int > sellCnt;
void Init()
{
memset(vis, 0, sizeof(vis));
while (!buy.empty()) buy.pop();
while (!sell.empty()) sell.pop();
buyCnt.clear();sellCnt.clear();
buyCnt[0] = sellCnt[99999] = 0;
buy.push(Node(0,0, MAXSIZE, 0));
sell.push(Node(99999,0, MAXSIZE, 0));
}
int CheckCancel() {
int ret = 0;
while (buy.size() > 1 && vis[buy.top().id]) { ret = 1; buy.pop(); }
while (sell.size() > 1 && vis[sell.top().id]) { ret = 1; sell.pop();}
return ret;
}
void DoDeal(int type) {
while (buy.top().price>= sell.top().price)
{
if (buy.size() <= 1 || sell.size() <= 1) break;
if(CheckCancel()) continue;
Node &a = orders[buy.top().id], &b = orders[sell.top().id];
buy.pop(); sell.pop();
int cut = min(a.cnt, b.cnt);
a.cnt -= cut; buyCnt[a.price] -= cut;
b.cnt -= cut; sellCnt[b.price] -= cut;
if (a.cnt) buy.push(a);
if (b.cnt) sell.push(b);
if (type == 1) cout << "TRADE " << cut << " " << b.price << endl;
else if(type == 2) cout << "TRADE " << cut << " " << a.price << endl;
}
CheckCancel();
}
int main()
{
int n,t = 0;
string temp;
while (cin >> n && n) {
if (t++) cout << endl;
int p, c, id, cnt = 1,type = 0;
Init();
while (n--) {
cin >> temp;
orders[cnt].type = 0;
if (temp.front() == 'B' || temp.front() == 'S') {
cin >> c >> p;
orders[cnt] = Node(p, c, cnt, 1);
if (temp.front() == 'B') {
buy.push(orders[cnt]);
buyCnt[p] += c;
}
else {
orders[cnt].type = 2;
sell.push(orders[cnt]);
sellCnt[p] += c;
}
}
else {
cin >> id;
if (!vis[id]) {
if (orders[id].type == 1) {
buyCnt[orders[id].price] -= orders[id].cnt;
}
else if (orders[id].type == 2)
sellCnt[orders[id].price] -= orders[id].cnt;
}
vis[id] = 1;
}
DoDeal(orders[cnt].type);
cout << "QUOTE " << buyCnt[buy.top().price] << " " << buy.top().price << " - " << sellCnt[sell.top().price] << " " << sell.top().price << endl;
cnt++;
}
}
return 0;
}
习题5-14(uva-1598)
最新推荐文章于 2021-03-09 22:41:07 发布