2021-03-03

#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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值