ccf 201412-3 集合竞价

本文通过几个测试样例详细介绍了集合竞价的过程,包括买入、卖出、取消订单的处理,以及最终确定的价格和数量。关键在于理解在处理cancel命令时需要保留信息,以便正确计算竞价结果。
摘要由CSDN通过智能技术生成

链接

http://115.28.138.223/view.page?gpid=T19


问题描述
  某股票交易所请你编写一个程序,根据开盘前客户提交的订单来确定某特定股票的开盘价和开盘成交量。
  该程序的输入由很多行构成,每一行为一条记录,记录可能有以下几种:
  1. buy p s 表示一个购买股票的买单,每手出价为p,购买股数为s。
  2. sell p s 表示一个出售股票的卖单,每手出价为p,出售股数为s。
  3. cancel i表示撤销第i行的记录。
  如果开盘价为p 0,则系统可以将所有出价至少为p 0的买单和所有出价至多为p 0的卖单进行匹配。因此,此时的开盘成交量为出价至少为p 0的买单的总股数和所有出价至多为p 0的卖单的总股数之间的较小值。
  你的程序需要确定一个开盘价,使得开盘成交量尽可能地大。如果有多个符合条件的开盘价,你的程序应当输出最高的那一个。
输入格式
  输入数据有任意多行,每一行是一条记录。保证输入合法。股数为不超过10 8的正整数,出价为精确到恰好小数点后两位的正实数,且不超过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。

测试样例

buy 9.25 100
buy 8.88 175
buy 9.00 400
sell 8.88 400
cancel 1
buy 100.00 50
答案是9.00 400


sell 8.88 100
sell 8.88 175
sell 9.00 400
buy 8.88 400
cancel 1
sell 100.00 50
答案是8.88 175


buy 9.25 100
buy 8.88 175
buy 9.00 400
sell 8.79 1501
cancel 1
cancel 2
q
答案是9.00 400


buy 9.25 110
buy 8.88 300
buy 18.88 200
sell 8.88 201
sell 9.25 100
q
答案是9.25 301




//1.这题一直错,原因在于一开始处理命令的时候,cancel没存储,而是直接拿去执行了。

//这样的话,由于cancel没有存储,cancel后面的所有话的下标都错了,因为cance本来要占一个位置的


//2.第二个点在于,完全没有考虑到如果多个cancel对同一条命令作用会怎样
//事实上,这题里,也没有说得很清楚。
//正确的做法是,如果后面cancel作用在前面cancel上,则不用管。
//如果后面的cancel作用在前面已经被cancel作用过的sell和buy上,则每次取反(即,两次cancel相抵消)
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <queue>
#include <stack>
#include <map>
#define INF 123123123
#define INS 0.000005
#define MAX_INDEX 5016
using namespace std;


struct node
{
    double p;
    long long s;
    int status;
    //指令:0表示cancel,1表示sell,2表示buy
    int useful;
    //0表示作废作废本句话


};
struct todeal
{
    double p;
    long long s;
};
node order[MAX_INDEX];
todeal buy[MAX_INDEX],sell[MAX_INDEX];
int buy_index,sell_index,order_index;
bool cmp2(node a,node b);
bool cmp1(node a,node b);
int main()
{
    string temp;
    double p;
    long long s,line_index;
    order_index = 1;
    for(int i=0; i<MAX_INDEX; i++)
        order[i].useful = 0;
    while(cin >> temp)
    {
        if(temp=="cancel")
        {
            cin >> line_index;
            order[order_index].useful = 1;
            order[order_index].s = line_index;
            order[order_index].status = 0;
            order_index++;
        }
        else if(temp=="sell" || temp=="buy")
        {
            cin >> p >> s;
            order[order_index].p = p;
            order[order_index].s = s;
            if(temp=="sell")
                order[order_index].status = 1;
            else if(temp=="buy")
                order[order_index].status = 2;
            order[order_index].useful = 1;
            order_index++;
        }
        else
            break;
    }
    for(int i=order_index-1;i>=1;i--)
    {
        if(order[i].status == 0)
        {
            order[order[i].s].useful = 1 - order[order[i].s].useful;
        }
    }
    long long flag_sell = 0;
    long long flag_buy = 0;
    buy_index = sell_index = 0;
    sort(order,order+MAX_INDEX,cmp2);
    for(int i=0; i<MAX_INDEX; i++)
    {
        if(order[i].useful == 1 && order[i].status == 2)
        {
            buy[buy_index].p = order[i].p;
            flag_buy += order[i].s;
            buy[buy_index].s = flag_buy;
            buy_index++;
        }
    }
    sort(order,order+MAX_INDEX,cmp1);
    for(int i=0; i<MAX_INDEX; i++)
    {
        if(order[i].useful == 1 && order[i].status == 1)
        {
            sell[sell_index].p = order[i].p;
            flag_sell += order[i].s;
            sell[sell_index].s = flag_sell;
            sell_index++;
        }
    }
    double deal_cost,result_cost = 0;
    long long deal_liang = -INF;
    long long result_liang = -1;
    for(int i=0; i<buy_index; i++)
    {
        int j = 0;
        for( j=0 ; j<sell_index; j++)
        {
            if(sell[j].p-buy[i].p>INS)
                break;
            else
            {
                deal_liang = min(buy[i].s,sell[j].s);
            }
        }
        if(deal_liang>result_liang)
        {
            result_liang = deal_liang;
            result_cost = buy[i].p;
        }
        /*
                if(j==0&&sell[0].p-buy[i].p>INS)
                    continue;
                if(j==0&&sell[0].p-buy[i].p<INS)
                    j++;
                deal_liang = sell[j-1].s>buy[i].s?buy[i].s:sell[j-1].s;
                if(deal_liang>result_liang)
                {
                    result_liang = deal_liang;
                    result_cost = buy[i].p;
                }
                else if(deal_liang==result_liang&&deal_liang!=-INF)
                {
                    if(result_cost - buy[i].p < INS)
                            result_cost = buy[i].p;
                }*/
    }
    if(result_liang<=0)
    {
        result_cost = 0;
        result_liang = 0;
    }
    printf("%.2lf %lld\n",result_cost,result_liang);
    return 0;
}
bool cmp2(node a,node b)
{
    if(fabs(a.p-b.p)>INS)
        return a.p-b.p>INS;
    return false;
}
bool cmp1(node a,node b)
{
    if(fabs(a.p-b.p)>INS)
        return a.p-b.p<INS;
    return false;

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值