https://cn.vjudge.net/problem/UVA-1598#author=0
题意:你的任务是为交易所设计一个订单处理系统。要求支持以下3种指令。
- BUY p q:有人想买,数量为p,价格为q
- SELL p q:有人想卖,数量为p,价格为q
- CANCEL i:取消第i条指令对应的订单(输入保证该指令是BUY或者SELL)
交易规则如下: 对于当前买订单,若当前最低卖价(ask price) 低于当前出价,则发生交易;对于当前卖订单,若当前最高买价(bid price)高于当前价格,则发生交易。发生交易时,按供需物品个数的最小值交易。交易后,需修改订单的供需物品个数。当出价或者价格相同时,按订单产生的先后顺序发生交易。输入输出细节请参考原题。
如果没有买的订单,价格数量都认为是0,如果没有卖的订单,价格认为99999,数量认为0。
提示: 本题是一个不错的优先队列练习题。 以上解释为COPY自算法竞赛入门。
分析:
- 定义一个结构体Order,为每个订单,里面有重载运算符来在优先队列中排序订单。
- 定义了两个map,一个名为buy用于存储buy订单中价格对应的数量,另一个为sell存储sell订单中价格对应的数量。
- 定义了两个优先队列,一个buy订单队列,另一个为sell订单。
- 定义了bool类型数组来判断是否被cancle。
- 定义了一个Order类型数组,里面存储每个订单信息,数组下标+1为订单编号。
然后根据题目模拟交易的进行,要足够细心。不要忘记队列,map清零。
还要考虑canle队首订单时的情况。
代码:
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5;
struct Order {
int id, num, price;
string choose;
Order() {}
Order(int _id, int _num, int _price, string _choose):
id(_id), num(_num), price(_price), choose(_choose) {}
bool operator < (const Order& order) const {
if(order.price!=price) return order.price>price;
else return order.id<id;
}
bool operator > (const Order& order) const {
if(order.price!=price) return order.price<price;
else return order.id<id;
}
};
Order orders[MAXN];
bool del[MAXN];
priority_queue<Order> buy_q;
priority_queue<Order, vector<Order>, greater<Order>> sell_q;
map<int, int> buy;
map<int, int> sell;
void Exchange(string str) {
while(buy_q.top().price>=sell_q.top().price) {
if(buy_q.size()==1 || sell_q.size()==1) break;
if(del[buy_q.top().id]) { buy_q.pop(); continue;}
if(del[sell_q.top().id]) { sell_q.pop(); continue;}
Order Buy = buy_q.top();
buy_q.pop();
Order Sell = sell_q.top();
sell_q.pop();
int num = min(Buy.num, Sell.num);
Buy.num-=num, Sell.num-=num;
orders[Buy.id].num-=num, orders[Sell.id].num-=num;
buy[Buy.price]-=num, sell[Sell.price]-=num;
if(Buy.num) buy_q.push(Buy);
if(Sell.num) sell_q.push(Sell);
if(str[0]=='B') cout << "TRADE " << num << " " << Sell.price << endl;
if(str[0]=='S') cout << "TRADE " << num << " " << Buy.price << endl;
}
}
int main() {
freopen("i.txt", "r", stdin);
int n,a,b;
string str;
while(cin >> n) {
memset(del, false, sizeof(del));
buy.clear();
sell.clear();
while (!buy_q.empty()) buy_q.pop();
while (!sell_q.empty()) sell_q.pop();
buy_q.push(Order(0, 0, 0, ""));
sell_q.push(Order(0, 0, 99999, ""));
for (int i = 1; i <= n; i++) {
cin >> str;
if (str[0] == 'B') {
cin >> a >> b;
buy_q.push(Order(i, a, b, str));
orders[i] = Order(i, a, b, str);
buy[b] += a;
} else if (str[0] == 'S') {
cin >> a >> b;
sell_q.push(Order(i, a, b, str));
orders[i] = Order(i, a, b, str);
sell[b] += a;
} else {
int id;
cin >> id;
if (orders[id].choose[0] == 'B')
buy[orders[id].price] -= orders[id].num;
else if (orders[id].choose[0] == 'S')
sell[orders[id].price] -= orders[id].num;
del[id] = true;
}
Exchange(str);
//考虑队首
while (buy_q.size() > 1 && del[buy_q.top().id]) buy_q.pop();
while (sell_q.size() > 1 && del[sell_q.top().id]) sell_q.pop();
cout << "QUOTE " << buy[buy_q.top().price] << " " << buy_q.top().price << " - " << sell[sell_q.top().price] << " " << sell_q.top().price << endl;
}
}
return 0;
}