• codeforces # 317 div 2 B. Order Book(模拟)


    B. Order Book
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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.

    昨天头疼。。然后觉得要掉。。就用小号打的。。。

    结果涨了200+rating。。。。

    这题就是纯模拟啊。。。

    一遍ac。。。

    需要注意的地方,如果说有。。。

    大概就是,sell的是取最小的s个,要先从小到大排序。。。但是输出的时候又是从大到小排序。。。

    昨天40分钟做完前2道题。。然后c题不会搞。。。就水群(求不鄙视> <),好像一堆人wa b wa得特别惨的样子。。。

      1 /*************************************************************************
      2     > File Name: code/cf/#317/B.cpp
      3     > Author: 111qqz
      4     > Email: rkz2013@126.com 
      5     > Created Time: 2015年08月23日 星期日 00时30分51秒
      6  ************************************************************************/
      7 
      8 #include<iostream>
      9 #include<iomanip>
     10 #include<cstdio>
     11 #include<algorithm>
     12 #include<cmath>
     13 #include<cstring>
     14 #include<string>
     15 #include<map>
     16 #include<set>
     17 #include<queue>
     18 #include<vector>
     19 #include<stack>
     20 #include<cctype>
     21 #define y1 hust111qqz
     22 #define yn hez111qqz
     23 #define j1 cute111qqz
     24 #define ms(a,x) memset(a,x,sizeof(a))
     25 #define lr dying111qqz
     26 using namespace std;
     27 #define For(i, n) for (int i=0;i<int(n);++i)  
     28 typedef long long LL;
     29 typedef double DB;
     30 const int inf = 0x3f3f3f3f;
     31 const int N=1E5+7;
     32 struct Q
     33 {
     34     int val;
     35     int num;
     36     int id;
     37 }b[N],s[N],ans[N];
     38 int id[N];
     39 int id2[N];
     40 int n,ss;
     41 
     42 bool cmp(Q a,Q b)
     43 {
     44     if (a.val<b.val)
     45     return true;
     46     return false;
     47 }
     48 bool cmp2(Q a,Q b)
     49 {
     50     if (a.val>b.val) return true;
     51     return false;
     52 }
     53 int main()
     54 {
     55   #ifndef  ONLINE_JUDGE 
     56     freopen("in.txt","r",stdin); 
     57   #endif
     58     cin>>n>>ss;
     59     int x,y;
     60     char order;
     61     ms(b,0);
     62     ms(s,0);
     63     ms(id,0);
     64     ms(id2,0);
     65     int cntA=0,cntB= 0 ;
     66     for ( int i  = 0  ; i < n ; i++){
     67     cin>>order>>x>>y;
     68     if (order=='B'){
     69         if (id[x]==0)
     70         {
     71         cntA++;
     72         id[x] = cntA;
     73         }
     74         b[id[x]].num = b[id[x]].num + y;
     75         b[id[x]].val = x;
     76         b[id[x]].id = id[x];
     77     }
     78     else
     79     {
     80         if (id2[x]==0)
     81         {
     82         cntB++;
     83         id2[x] = cntB;
     84         }
     85         s[id2[x]].num = s[id2[x]].num+y;
     86         s[id2[x]].val = x;
     87         s[id2[x]].id = id2[x];
     88     }
     89     }
     90     sort(s+1,s+cntB+1,cmp);
     91     sort(b+1,b+cntA+1,cmp2);
     92     cntA = min(cntA,ss);
     93     cntB = min(cntB,ss);
     94     ms(ans,0);
     95     for ( int i = 1 ; i <= cntB ; i++){
     96     ans [i].val = s[i].val;
     97     ans[i].num = s[i].num;
     98     }
     99     sort(ans+1,ans+cntB+1,cmp2);
    100     for ( int i = 1; i <= cntB ; i++){
    101     cout<<"S "<<ans[i].val<<" "<<ans[i].num<<endl;
    102     }
    103     ms(ans,0);
    104     for ( int i =  1 ; i <= cntA ; i++){
    105     ans[i].val = b[i].val;
    106     ans[i].num = b[i].num;
    107     }
    108     sort(ans+1,ans+cntA+1,cmp2);
    109     for ( int i = 1 ; i <= cntA ; i++){
    110     cout<<"B "<<ans[i].val<<" "<<ans[i].num<<endl;
    111     }
    112   
    113   
    114  #ifndef ONLINE_JUDGE  
    115   fclose(stdin);
    116   #endif
    117     return 0;
    118 }
    View Code
  • 相关阅读:
    Java的学习笔记(二)
    Java的学习笔记
    简单界面生成
    登录界面
    播放器
    java计划
    求和
    修改后的抽奖系统
    第十周作业
    JAVA第五次作业
  • 原文地址:https://www.cnblogs.com/111qqz/p/4752675.html
Copyright © 2020-2023  润新知