问题描述
某股票交易所请你编写一个程序,根据开盘前客户提交的订单来确定某特
定股票的开盘价和开盘成交量。
该程序的输入由很多行构成,每一行为一条记录,记录可能有以下几种:
为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<vector>
#include<string>
#include<algorithm>
using namespace std;
struct N {
string type;
double price;
long long num;
};
bool cmp(N n1,N n2) {
if (n1.price != n2.price)
return n1.price < n2.price;
else
return n1.num > n2.num;
}
vector<N> goods;
vector<N> B,S;
bool flag[5007];
int main()
{
string type; double price; long long num;
while (cin>>type)
{
if (type != "cancel") {
cin >> price >> num;
N wupin;
wupin.type = type; wupin.price = price; wupin.num = num;
goods.push_back(wupin);
}
else
{
cin >> num;
goods[num-1].num = -1; goods[num-1].price = -1;
}
}
//sort(goods.begin(), goods.end(), cmp1);
for (int i = 0; i < goods.size(); i++) {
if (goods[i].type == "buy"&&goods[i].num!=-1&&goods[i].price!=-1)
B.insert(B.begin(),goods[i]);
else if (goods[i].type == "sell" && goods[i].num !=-1 && goods[i].price!=-1)
S.insert(S.begin(),goods[i]);
}
sort(B.begin(), B.end(), cmp);
sort(S.begin(), S.end(), cmp);
long long max=0,temp;
double P=0,p;
long long bnum = 0,snum;
for (int i=B.size()-1; i >=0; i--) {
bnum += B[i].num;
p = B[i].price;
snum = 0.0;
for (int j = 0; j <S.size(); j++) {
if (S[j].price > p) break;
snum += S[j].num;
}
temp = min(snum, bnum);
if (max < temp) {
max = temp;
P = p;
}
}
printf("%.2lf %lld", P,max);
return 0;
}
感觉没有问题,然而一直三十分,看了一天了,求大神解答!!