问题描述
某股票交易所请你编写一个程序,根据开盘前客户提交的订单来确定某特定股票的开盘价和开盘成交量。
该程序的输入由很多行构成,每一行为一条记录,记录可能有以下几种:
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 <cstdio>
#include <algorithm>
#include <vector>
#include <string>
#include <iomanip>
#include <map>
#include <cmath>
#include <list>
using namespace std;
struct Stake{
bool flag, isCancel; // 标记是buy or call, 是否被cancel
int line; // 记录所在第几行
double p;
int s;
Stake(bool f, double p, int s, int l):flag(f), p(p), s(s), line(l), isCancel(false){}
};
typedef long long LL;
const int inf = 0x7f7f7f7f; // 2139062143
const double eps = 1e-8;
list<Stake> stakes;
int main(){
#ifdef LOCAL
freopen("data.in", "r", stdin);
#endif // LOCAL
string op;
double p;
int s;
int lineCnt = 1;
while(cin >> op){
if(op[0] == 'b') {
cin >> p >> s;
stakes.push_back(Stake(true, p, s, lineCnt++));
}else if(op[0] == 's'){
cin >> p >> s;
stakes.push_back(Stake(false, p, s, lineCnt++));
}
else{
cin >> s;
list<Stake>::iterator it = stakes.begin();
for(; it != stakes.end() && it->line != s; ++it);
it->isCancel = true; //标记被Cancel
lineCnt++;
}
}
// for(list<Stake>::iterator it = stakes.begin(); it != stakes.end(); ++it){
// cout << it->flag << " " << it->p << " " << it->s << " " << it->line << " " << it->isCancel << endl;
// }
map<double, bool> vis;
for(list<Stake>::iterator it = stakes.begin(); it != stakes.end(); ++it){
if(!it->isCancel) vis[it->p] = false;
}
LL ans_s = 0;
double ans_p = 0;
// 暴力,枚举每一个出价
for(list<Stake>::iterator it = stakes.begin(); it != stakes.end(); ++it){
if(!vis[it->p]){
vis[it->p] = true;
double p0 = it->p;
LL buy = 0, sell = 0;
for(list<Stake>::iterator it1 = stakes.begin(); it1 != stakes.end(); ++it1){
if( it1->flag && !it1->isCancel && it1->p >= p0) buy += it1->s;
if(!it1->flag && !it1->isCancel && it1->p <= p0) sell += it1->s;
}
LL t = min(buy, sell);
if(ans_s < t){
ans_s = t;
ans_p = p0;
} else if(ans_s == t){
ans_p = max(p0, ans_p);
}
}
}
cout << fixed << setprecision(2) << ans_p << " " << ans_s << endl;
return 0;
}