问题描述
试题编号: | 201412-3 |
试题名称: | 集合竞价 |
时间限制: | 1.0s |
内存限制: | 256.0MB |
问题描述: | 问题描述 某股票交易所请你编写一个程序,根据开盘前客户提交的订单来确定某特定股票的开盘价和开盘成交量。 输入格式 输入数据有任意多行,每一行是一条记录。保证输入合法。股数为不超过108的正整数,出价为精确到恰好小数点后两位的正实数,且不超过10000.00。 输出格式 你需要输出一行,包含两个数,以一个空格分隔。第一个数是开盘价,第二个是此开盘价下的成交量。开盘价需要精确到小数点后恰好两位。 样例输入 buy 9.25 100 样例输出 9.00 450 评测用例规模与约定 对于100%的数据,输入的行数不超过5000。 |
思路
使用两个优先队列,一个用于存储购入订单,按价格从大到小排列;另外一个用于存储卖出订单,按价格从小到大排列;然后进行价格的匹配处理。
代码
#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std;
struct trading {
int orderno;
char t;
float price;
long long quantity;
bool operator < (const trading& n) const {
if(t == 's')
return price > n.price;
else if(t == 'b')
return price < n.price;
}
};
bool cancelflag[5001];
int main()
{
trading t;
priority_queue<trading> sell, buy;
string strading;
memset(cancelflag, false, sizeof(cancelflag));
int no = 0, tno;
while(cin >> strading) {
if(strading[0] == 'c') {
// 设置交易号
no++;
cin >> tno;
// 设置取消标志
cancelflag[tno] = true;
} else if(strading[0] == 'b' || strading[0] == 's') {
t.orderno = ++no;
cin >> t.price >> t.quantity;
// 将交易分别放入买入和卖出的优先队列
if(strading[0] == 'b') {
t.t = strading[0];
buy.push(t);
} else {
t.t = strading[0];
sell.push(t);
}
} else
break;
}
// 集合竞价处理
t.price = 0;
t.quantity = 0;
trading b, s;
for(;;) {
// 清除被取消的订单(同时将队头放在b和s中)
while(!buy.empty()) {
b = buy.top();
if(cancelflag[b.orderno])
buy.pop();
else
break;
}
while(!sell.empty()) {
s = sell.top();
if(cancelflag[s.orderno])
sell.pop();
else
break;
}
// 买卖队列只要有一个为空,则处理结束
if(buy.empty() || sell.empty())
break;
// 集合竞价处理
if(b.price >= s.price) {
t.quantity += min(b.quantity, s.quantity);
t.price = b.price;
if(b.quantity == s.quantity) {
buy.pop();
sell.pop();
} else if(b.quantity > s.quantity) {
b.quantity -= s.quantity;
buy.pop();
buy.push(b);
sell.pop();
} else {
buy.pop();
s.quantity -= b.quantity;
sell.pop();
sell.push(s);
}
} else
break;
}
// 输出结果
printf("%.2f", t.price);
cout << " " << t.quantity << endl;
return 0;
}