试题编号: | 201412-3 |
试题名称: | 集合竞价 |
时间限制: | 1.0s |
内存限制: | 256.0MB |
问题描述: | 问题描述 某股票交易所请你编写一个程序,根据开盘前客户提交的订单来确定某特定股票的开盘价和开盘成交量。 输入格式 输入数据有任意多行,每一行是一条记录。保证输入合法。股数为不超过108的正整数,出价为精确到恰好小数点后两位的正实数,且不超过10000.00。 输出格式 你需要输出一行,包含两个数,以一个空格分隔。第一个数是开盘价,第二个是此开盘价下的成交量。开盘价需要精确到小数点后恰好两位。 样例输入 buy 9.25 100 样例输出 9.00 450 评测用例规模与约定 对于100%的数据,输入的行数不超过5000。 |
#include <map>
#include <cmath>
#include <queue>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <time.h>
#include <vector>
#include <list>
using namespace std;
//201412-3 集合竞价
struct SS
{
bool Flag;
double Pos;
int Num;
};
int main()
{
int i = 0, j, N;
char s[8];
long long Buy_Num = 0, Sell_Num = 0 ,Min_Num = 0, Char_Max = 0;
double Buy_Price, Out = 0;
bool Flag;
map<double, long long>Buy;
map<double, long long>Sell;
vector<SS> Jishu;
SS Value;
cin >> N;
while (cin >> s)
{
if (s[0] == 'c')
{
cin >> N;
if (Jishu[N -1].Flag == 0)
Buy[Jishu[N - 1].Pos] -= Jishu[N - 1].Num;
else
Sell[Jishu[N - 1].Pos] -= Jishu[N - 1].Num;
//cout << "0" << endl;
}
else if (s[0] == 'b')
{
Value.Flag = 0;
cin >> Value.Pos >> Value.Num;
Buy[Value.Pos] += Value.Num;
Jishu.push_back(Value);
//cout << " 1 :" << Value.Pos << endl;
}
else if (s[0] == 's')
{
Value.Flag = 1;
cin >> Value.Pos >> Value.Num;
Sell[Value.Pos] += Value.Num;
Jishu.push_back(Value);
//cout << "2" << endl;
}
else
break;
}
for (map<double, long long>::iterator it = Sell.begin(); it != Sell.end(); it++)
{
Sell_Num += (*it).second;
if (--Buy.end() == Buy.begin())
{
if ((*Buy.begin()).first >= (*it).first)
if (Buy_Num < Sell_Num )
{
Buy_Num += (*Buy.begin()).second;
if((*Buy.begin()).second != 0)
Buy_Price = (*Buy.begin()).first;
}
//cout << "1" << endl;
}
else
{
//cout << "2" << endl;
for (map<double, long long>::iterator m = --Buy.end(); m != Buy.begin(); )
{
if ((*m).first >= (*it).first)
{
if (Buy_Num < Sell_Num)
{
Buy_Num += (*m).second;
if ((*m).second != 0)
Buy_Price = (*m).first;
}
else
break;
}
else
break;
m--;
if (m == Buy.begin())
{
if ((*m).first >= (*it).first)
{
if (Buy_Num < Sell_Num)
{
Buy_Num += (*m).second;
if ((*m).second != 0)
Buy_Price = (*m).first;
}
}
break;
}
}
}
//cout << Sell_Num << " " << Buy_Num << endl;
if (Sell_Num < Buy_Num)
Min_Num = Sell_Num;
else
Min_Num = Buy_Num;
if (Min_Num > Char_Max)
{
Char_Max = Min_Num;
Out = Buy_Price;
}
else if (Min_Num == Char_Max)
{
if (Buy_Price > Out)
{
Char_Max = Min_Num;
Out = Buy_Price;
}
}
Buy_Num = 0;
//cout << (*it).first << endl;
}
printf("%.2f", Out);
cout << " " << Char_Max << endl;
//cin >> N;
system("pause");
//return 0;
}