试题编号: 201412-3
试题名称: 集合竞价
时间限制: 1.0s
内存限制: 256.0MB
问题描述:
问题描述
某股票交易所请你编写一个程序,根据开盘前客户提交的订单来确定某特定股票的开盘价和开盘成交量。
该程序的输入由很多行构成,每一行为一条记录,记录可能有以下几种:
1. buy p s 表示一个购买股票的买单,每手出价为p,购买股数为s。
2. sell p s 表示一个出售股票的卖单,每手出价为p,出售股数为s。
3. cancel i表示撤销第i行的记录。
如果开盘价为p0,则系统可以将所有出价至少为p0的买单和所有出价至多为p0的卖单进行匹配。因此,此时的开盘成交量为出价至少为p0的买单的总股数和所有出价至多为p0的卖单的总股数之间的较小值。
你的程序需要确定一个开盘价,使得开盘成交量尽可能地大。如果有多个符合条件的开盘价,你的程序应当输出最高的那一个。
输入格式
输入数据有任意多行,每一行是一条记录。保证输入合法。股数为不超过108的正整数,出价为精确到恰好小数点后两位的正实数,且不超过10000.00。
输出格式
你需要输出一行,包含两个数,以一个空格分隔。第一个数是开盘价,第二个是此开盘价下的成交量。开盘价需要精确到小数点后恰好两位。
样例输入
buy 9.25 100
buy 8.88 175
sell 9.00 1000
buy 9.00 400
sell 8.92 400
cancel 1
buy 100.00 50
样例输出
9.00 450
评测用例规模与约定
对于100%的数据,输入的行数不超过5000。
代码:
#include <iostream>
#include <queue>
#include <cstring>
#include <cstdio>
using namespace std;
const int N = 5000;
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 // t == 'b'
return price < n.price;
}
};
bool cancelflag[N+1];
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.trading[0] == 's'
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 { // b.quantity < s.quantity
buy.pop();
s.quantity -= b.quantity;
sell.pop();
sell.push(s);
}
} else
break;
}
// 输出结果
printf("%.2f", t.price);
cout << " " << t.quantity << endl;
return 0;
}
/*
测试样例:
sell 8.88 100
sell 8.88 175
sell 9.00 400
buy 8.88 400
cancel 1
sell 100.00 50
8.88 175
buy 9.25 100
buy 8.88 175
buy 9.00 400
sell 8.88 400
cancel 1
buy 100.00 50
9.00 400
buy 9.25 100
buy 8.88 175
buy 9.00 400
sell 8.79 1501
cancel 1
cancel 2
9.00 400
buy 9.25 110
buy 8.88 300
buy 18.88 200
sell 8.88 201
sell 9.25 100
9.25 301
*/
/* 顺序有问题,头疼
#include<string.h>
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<vector>
#include<map>
#include<iomanip>
#include<fstream>
#include<queue>
#include<set>
#include<stack>
using namespace std;
const int N = 5000;
typedef struct{
string flag;
double price;
long long count;
}Trade;
Trade trade[N];
bool cmp(Trade t1 , Trade t2){
if(t1.price != t2.price){
return t1.price > t2.price;
}else{
return t1.flag < t2.flag;
}
}
bool cmp2(pair<double,long long> p1 , pair<double,long long> p2){
if(p1.second != p2.second){
return p1.second > p2.second;
}else{
return p1.first > p2.second;
}
}
int main(){
int i = 0 , index;
string flag;
while(cin >> flag){
if(flag == "cancel"){
cin >> index;
trade[index - 1].price = 0;
trade[index - 1].count = 0;
i--;
}else if(flag == "buy" || flag == "sell"){
trade[i].flag = flag;
cin >> trade[i].price >> trade[i].count;
}else{
break;
}
i++;
}
sort(trade , trade + i, cmp);
vector<pair<double,long long>> vc;
for(int j = 0 ; j < i ; j++){
long long sellcount = 0;
long long buycount = 0;
double buyprice = 0;
int n = 0;
if(trade[j].flag == "sell"){
for(int m = j ; m >= 0 ; m--){
if(trade[m].flag == "sell"){
sellcount += trade[m].count;
}
}
for(int m = 0 ; m < j ; m++){
if(trade[m].flag == "buy"){
buycount += trade[m].count;
}
if(buycount >= sellcount){
buyprice = trade[m].price;
break;
}else{
buyprice = trade[m].price;
}
}
int count = min(buycount , sellcount);
vc.push_back(make_pair( buyprice , count));
}
}
sort(vc.begin() , vc.end() , cmp2);
printf("%.2lf ", vc[0].first);
cout << vc[0].second << endl;
vc.clear();
return 0;
}
*/
本文介绍了一个股票交易所的集合竞价算法,旨在确定特定股票的开盘价和开盘成交量。通过匹配客户提交的买单和卖单,算法寻找能实现最大成交量的开盘价,并在多个符合条件的价格中选择最高的一个。
2327

被折叠的 条评论
为什么被折叠?



