zoukankan      html  css  js  c++  java
  • [CF572B]Order Book

    题目链接:http://codeforces.com/contest/572/problem/B
     
    B. Order Book
     

    In this task you need to process a set of stock exchange orders and use them to create order book.

    An order is an instruction of some participant to buy or sell stocks on stock exchange. The order number i has price pi, direction di — buy or sell, and integer qi. This means that the participant is ready to buy or sell qi stocks at price pi for one stock. A value qi is also known as a volume of an order.

    All orders with the same price p and direction d are merged into one aggregated order with price p and direction d. The volume of such order is a sum of volumes of the initial orders.

    An order book is a list of aggregated orders, the first part of which contains sell orders sorted by price in descending order, the second contains buy orders also sorted by price in descending order.

    An order book of depth s contains s best aggregated orders for each direction. A buy order is better if it has higher price and a sell order is better if it has lower price. If there are less than s aggregated orders for some direction then all of them will be in the final order book.

    You are given n stock exhange orders. Your task is to print order book of depth s for these orders.

    Input

    The input starts with two positive integers n and s (1 ≤ n ≤ 1000, 1 ≤ s ≤ 50), the number of orders and the book depth.

    Next n lines contains a letter di (either 'B' or 'S'), an integer pi (0 ≤ pi ≤ 105) and an integer qi (1 ≤ qi ≤ 104) — direction, price and volume respectively. The letter 'B' means buy, 'S' means sell. The price of any sell order is higher than the price of any buy order.

    Output

    Print no more than 2s lines with aggregated orders from order book of depth s. The output format for orders should be the same as in input.

    Sample test(s)
    Input
    6 2
    B 10 3
    S 50 2
    S 40 1
    S 50 6
    B 20 4
    B 25 10
    Output
    S 50 8
    S 40 1
    B 25 10
    B 20 4
    Note

    Denote (x, y) an order with price x and volume y. There are 3 aggregated buy orders (10, 3), (20, 4), (25, 10) and two sell orders (50, 8), (40, 1) in the sample.

    You need to print no more than two best orders for each direction, so you shouldn't print the order (10 3) having the worst price among buy orders.

       分别存'B'和'S'的订单,分别按照从小到大和从大到小排序,用hash存他们的位置,直接放入即可。

     1 #include <stdio.h>
     2 #include <cstring>
     3 #include <iostream>
     4 #include <cmath>
     5 #include <map>
     6 #include <set>
     7 #include <queue>
     8 #include <vector>
     9 #include <stack>
    10 #include <cstdlib>
    11 #include <algorithm>
    12 
    13 using namespace std;
    14 
    15 typedef long long LL;
    16 const int maxn = 111111;
    17 int hashb[maxn];
    18 int hashs[maxn];
    19 struct Node {
    20     int p;
    21     int d;
    22     char s;
    23 }blist[maxn],slist[maxn];
    24 
    25 bool cmp1(Node tp,Node td) {
    26     return tp.p > td.p;
    27 }
    28 
    29 bool cmp2(Node tp,Node td) {
    30     return tp.p < td.p;
    31 }
    32 
    33 inline int min(int tp, int td) {
    34     return tp < td ? tp : td;
    35 }
    36 
    37 int main() {
    38     int n, m;
    39     cin >> n >> m;
    40     int tb = 0,ts = 0;
    41     memset(hashb, -1, sizeof(hashb));
    42     memset(hashs, -1, sizeof(hashs));
    43     while(n--) {
    44         char ss;
    45         int tp, td;
    46         cin >> ss >> tp >> td;
    47         if(ss == 'B') {
    48             if(hashb[tp] == -1) {
    49                 blist[tb].p = tp;
    50                 blist[tb].d = td;
    51                 blist[tb].s = ss;
    52                 hashb[tp] = tb;
    53                 tb++;
    54             }
    55             else {
    56                 blist[hashb[tp]].d += td;
    57             }
    58         }
    59         else {
    60             if(hashs[tp] == -1) {
    61                 slist[ts].p = tp;
    62                 slist[ts].d = td;
    63                 slist[ts].s = ss;
    64                 hashs[tp] = ts;
    65                 ts++;
    66             }
    67             else {
    68                 slist[hashs[tp]].d += td;
    69             }
    70         }
    71     }
    72         sort(blist, blist+tb, cmp1);
    73         sort(slist, slist+ts, cmp2);
    74         for(int i = min(m, ts) - 1; i >= 0; i--) {
    75             cout << slist[i].s << " " << slist[i].p << " " << slist[i].d << endl;
    76         }
    77         for(int i = 0; i < min(m, tb); i++) {
    78             cout << blist[i].s << " " << blist[i].p << " " << blist[i].d << endl;
    79         }
    80 }
  • 相关阅读:
    MyBatis常见面试题以及解读
    如何防止sql注入攻击
    宝塔Linux面板基础命令
    Centos7配置静态ip
    宝塔Linux面板安装
    idea中安装阿里巴巴的代码规范插件
    idea中快速将类中的属性转为Json字符串的插件
    创建线程的四种方式
    sleep()方法与wait()方法的区别
    解决线程安全的几种方式
  • 原文地址:https://www.cnblogs.com/kirai/p/4752225.html
Copyright © 2011-2022 走看看