ccf 201412-3 集合竞价 (100分)

问题描述

  某股票交易所请你编写一个程序,根据开盘前客户提交的订单来确定某特定股票的开盘价和开盘成交量。
  该程序的输入由很多行构成,每一行为一条记录,记录可能有以下几种:
  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。
具体过程见代码及注释提交后得100分的C++程序如下:

#include<iostream>
#include<string>
#include<cstring>
#include<string>
#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn = 5005;
bool cancelflag[maxn];
struct trade
{
    string op;
    double p;
    ll s, no;
    trade(ll n,string o, double p1, ll s1)
    {
        no=n,op = o, p = p1, s = s1;
    }
    trade()
    {
    }
    bool operator<(const trade &t) const
    {
        if (op[0] == 'b') return p < t.p;
        else return p > t.p;
    }
};
int main()
{
    string op;
    double p;
    ll s;
    priority_queue<trade> buy, sell;
    ll no = 0;
    memset(cancelflag, false, sizeof(cancelflag));
    while (cin >> op)
    {
        if (op == "buy")
        {
            cin >> p >> s;
            buy.push(trade(++no, op, p, s));
        }
        else if (op == "sell")
        {
            cin >> p >> s;
            sell.push(trade(++no, op, p, s));
        }
        else if(op=="cancel")
        {
            cin >> s;
            no++;
            cancelflag[s] = true;
        }
        else break;
    }
    ll sum = 0;//成交的总量
    trade t, bu, se;
    t.p = 0; t.s = 0;
    while(true)
    {
        while (!buy.empty())
        {
             bu= buy.top();
            if (cancelflag[bu.no])
            {
                buy.pop();
            }
            else break;
        }
        while (!sell.empty())
        {
             se = sell.top();
            if (cancelflag[se.no])
            {
                sell.pop();
            }
            else break;
        }
        if (buy.empty() || sell.empty())
        {
            break;
        }
        if (bu.p >= se.p) //处理的必要条件是买家的出价大于等于卖家的出价
        {
            t.s += min(bu.s, se.s);
            t.p = bu.p;
            if (bu.s >se.s)
            {
                bu.s -= se.s;
                buy.pop();
                buy.push(bu);
                sell.pop();
            }
            else if(bu.s<se.s)
            {
                se.s -= bu.s;
                sell.pop();
                sell.push(se);
                buy.pop();
            }
            else if(bu.s==se.s)
            {
                sell.pop(), buy.pop();
            }
        }
        else break;
    }
    printf("%.2f %lld\n", t.p, t.s);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/jinduo16/article/details/82116996
今日推荐