#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
class Node{
public:
string op;
float p;
int s;
int index;
Node(string _op, float _p, int _s, int _index) {
op = _op;
p = _p;
s = _s;
index = _index;
}
};
bool comp(Node* n1, Node* n2) {
return n1->p > n2->p;
}
int main() {
vector<Node*> buys;
vector<Node*> sells;
//输入数据
int lineNo = 0;
string line;
string op;
int s;
float p;
while (getline(cin,line)){
lineNo++;
stringstream ss(line);
ss >> op;
if (op == "buy") {
ss >> p >> s;
buys.push_back(new Node(op,p,s,lineNo));
}
else if (op == "sell"){
ss >> p >> s;
sells.push_back(new Node(op,p,s,lineNo));
}
else {
ss >> s;
int i;
for (i = 0; i < buys.size(); i++) {
if (buys[i]->index == s) {
buys.erase(buys.begin()+i);
break;
}
}
for (i = 0; i < sells.size(); i++) {
if (sells[i]->index == s) {
sells.erase(sells.begin()+i);
break;
}
}
}
}
sort(sells.begin(),sells.end(),comp);
sort(buys.begin(),buys.end(),comp);
/*
int i;
for (i = 0; i < nodes.size(); i++) {
cout << nodes[i]->op << " " << nodes[i]->p << " " << nodes[i]->s << endl;
}
*/
//开始算法,从大到小遍历每一个buy的价格
//以第一个buy价格为开盘成交价,统计此时低于成交价的sell
float price = buys[0]->p;
ll Nsell = 0;
int i = 0;//i为指向sells的指针,向0移动
while (i < sells.size() && sells[i]->p <= price) {
Nsell += sells[i]->s;
i++;
}
i--;
ll Nbuy = buys[0]->s;
ll amount = (Nbuy > Nsell) ? Nsell : Nbuy;
ll maxAmount = amount;
float pres = price;
int j = 1;//j为指向buys的指针,向buys.size()移动
while (j < buys.size()) {
price = buys[j]->p;
Nbuy += buys[j]->s;
while (i >=0 && sells[i]->p >= price) {
Nsell -= sells[i]->s;
i--;
}
amount = (Nbuy > Nsell) ? Nsell : Nbuy;
if (amount > maxAmount) {
pres = price;
maxAmount = amount;
}
j++;
}
cin.ignore();
printf("%.2f %d",pres,maxAmount);
return 0;
}
2021-03-03
最新推荐文章于 2021-11-21 12:10:25 发布