201412-3 集合竞价

实现

#include <cstdio>
#include <cstring>
#include <algorithm>

#define MAX_ORDER_NUM 0x1400

using namespace std;

struct order {
    int type;   // 0: buy, 1: sell, 2: cancel, -1: invalid
    double price;  
    int sum;    // sum or line_num

    bool operator< (const order &right) const {
        if (this->price < right.price 
        || (this->price == right.price && this->type == 1 && right.type == 0)) {
            return true;
        } else {
            return false;
        }
    }
};

struct trading {
    double price;
    int sum;
};

order orders[MAX_ORDER_NUM];
order valid_orders[MAX_ORDER_NUM];
int order_cnt;
int valid_order_cnt;

long long sell_amount[MAX_ORDER_NUM];
long long buy_amount[MAX_ORDER_NUM];

int main() {
    char op_str[10];
    double price;
    int sum;
    
    while(scanf("%s",op_str) != EOF) {
        if (!strcmp(op_str,"cancel")) {
            scanf("%d",&sum);
            orders[order_cnt].type = 2;
            orders[order_cnt].sum = sum;
        } else {
            scanf("%lf%d",&price,&sum);
            orders[order_cnt].type = (op_str[0] == 'b') ? 0 : 1;
            orders[order_cnt].price = price;
            orders[order_cnt].sum = sum;
        }
        ++order_cnt;
    }

    for (int i = 0;i < order_cnt;++i) {
        if (orders[i].type == 2) {
            orders[orders[i].sum - 1].type = -1;
        }
    }

    for (int i = 0;i < order_cnt;++i) {
        if (orders[i].type == 0 
            || orders[i].type == 1) {
            valid_orders[valid_order_cnt++] = orders[i];
        }
    }

    sort(valid_orders,valid_orders + valid_order_cnt);

    if (valid_orders[0].type == 0) {
        buy_amount[0] = valid_orders[0].sum;
    } else {
        sell_amount[0] = valid_orders[0].sum;
    }

    for (int i = 1;i < valid_order_cnt;++i) {
        if (valid_orders[i].type == 0) {
            buy_amount[i] = buy_amount[i-1] + valid_orders[i].sum;
            sell_amount[i] = sell_amount[i-1];
        } else {
            sell_amount[i] = sell_amount[i-1] + valid_orders[i].sum;
            buy_amount[i] = buy_amount[i-1];
        }
    }

    double opening_price = 0.0f;
    long long opening_deal_amount = 0;

    for (int i = 0;i < valid_order_cnt;++i) {
        if (valid_orders[i].price > 10000.00) {
            break;
        }
        
        long long deal_amount;
        if (valid_orders[i].type == 0) {
            deal_amount 
            = (sell_amount[i] < buy_amount[valid_order_cnt -1] - buy_amount[i - 1])
              ?sell_amount[i]:buy_amount[valid_order_cnt -1] - buy_amount[i - 1];    
        } else {
            deal_amount 
            = (sell_amount[i] < buy_amount[valid_order_cnt -1] - buy_amount[i])
              ?sell_amount[i]:buy_amount[valid_order_cnt -1] - buy_amount[i]; 
        }
        
       
        if (deal_amount >= opening_deal_amount) {
            opening_deal_amount = deal_amount;
            opening_price = valid_orders[i].price;
        }
    }

    printf("%.2lf %lld",opening_price, opening_deal_amount);
}
posted @ 2020-08-26 17:23  amonqsq  阅读(149)  评论(0编辑  收藏  举报