集合竞价
试题编号: 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<cstdio>
#include<vector>
#include<cstring>
using namespace std;
#define LL long long
struct node{
int type; //1为buy, 2为sell , 3为cancle
bool effect; //是否有效
double p; //出价
LL s; //股数 或 cancel的行数
};
vector<node> act;
int main() {
char order[6];
while(scanf("%s", order) != EOF) {
node tmp;
if(strcmp(order, "buy") == 0) {
tmp.type = 1, tmp.effect = true;
scanf("%lf %lld", &tmp.p, &tmp.s);
act.push_back(tmp);
}else if(strcmp(order, "sell") == 0) {
tmp.type = 2, tmp.effect = true;
scanf("%lf %lld", &tmp.p, &tmp.s);
act.push_back(tmp);
}else{
tmp.type = 3, tmp.effect = true;
LL num;
scanf("%lld", &num);
tmp.s = num;
act.push_back(tmp);
act[num - 1].effect = !act[num - 1].effect;
}
}
LL max = 0; //存放最大开盘成交量
double p0, p0_max; //开盘价 使开盘成交量最大的开盘价
int len = act.size();
for(int i = 0; i < len; i++) {
if(act[i].type < 3 && act[i].effect){
p0 = act[i].p;
LL buy_sum = 0, sell_sum = 0;
for(int j = 0; j < len; j++) {
if(act[j].type == 1 && act[j].effect && act[j].p >= p0){ //对于buy ,不小于开盘价
buy_sum += act[j].s;
}
if(act[j].type == 2 && act[j].effect && act[j].p <= p0){ //对于sell,不大于开盘价
sell_sum += act[j].s;
}
}
LL sum;
if(buy_sum < sell_sum){
sum = buy_sum;
}else{
sum = sell_sum;
}
if(sum > max) {
p0_max = p0;
max = sum;
}else if(sum == max && p0 > p0_max){
p0_max = p0;
}
}
}
printf("%.2f %lld\n", p0_max, max);
return 0;
}
巨坑的题,题目没有说清楚重复取消操作是什么效果,最后发现对同一个买卖操作两次取消相当于没有取消
还有没注意成交量范围,用了int只能得80分