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.

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm> using namespace std; int n,m;
int buy[100100];
int sell[100100]; int main()
{
while(scanf("%d%d",&n,&m)!=EOF){
memset(buy,0,sizeof(buy));
memset(sell,0,sizeof(sell));
char str[11];
int x,y;
for(int i=0;i<n;i++){
scanf("%s%d%d",str,&x,&y);
if(strcmp(str,"B") == 0){
buy[x] += y;
}else{
sell[x] += y;
}
}
int t = 0;
int p = 0;
for(int i=0;i<=100000;i++)
{
if(t<m && sell[i]){
t++;
p = i;
}else if(t>=m){
break;
}
}
for(int i=p;i>=0;i--){
if(sell[i]){
printf("S %d %d\n",i,sell[i]);
t++;
}
}
t = 0;
for(int i=100000;i>=0;i--){
if(t<m && buy[i]){
printf("B %d %d\n",i,buy[i]);
t++;
}else if(t>=m){
break;
}
}
}
return 0;
}

B. Order Book(Codeforces Round #317 )的更多相关文章

  1. 「日常训练」Watering Flowers(Codeforces Round #340 Div.2 C)

    题意与分析 (CodeForces 617C) 题意是这样的:一个花圃中有若干花和两个喷泉,你可以调节水的压力使得两个喷泉各自分别以\(r_1\)和\(r_2\)为最远距离向外喷水.你需要调整\(r_ ...

  2. 「日常训练」Alternative Thinking(Codeforces Round #334 Div.2 C)

    题意与分析 (CodeForces - 603A) 这题真的做的我头疼的不得了,各种构造样例去分析性质... 题意是这样的:给出01字符串.可以在这个字符串中选择一个起点和一个终点使得这个连续区间内所 ...

  3. 「日常训练」More Cowbell(Codeforces Round #334 Div.2 B)

    题意与分析(CodeForces 604B) 题意是这样的:\(n\)个数字,\(k\)个盒子,把\(n\)个数放入\(k\)个盒子中,每个盒子最多只能放两个数字,问盒子容量的最小值是多少(水题) 不 ...

  4. 「日常训练」Duff in the Army (Codeforces Round #326 Div.2 E)

    题意(CodeForces 588E) 给定一棵\(n\)个点的树,给定\(m\)个人(\(m\le n\))在哪个点上的信息,每个点可以有任意个人:然后给\(q\)个询问,每次问\(u\)到\(v\ ...

  5. 「日常训练」Kefa and Dishes(Codeforces Round #321 Div. 2 D)

    题意与分析(CodeForces 580D) 一个人有\(n\)道菜,然后要点\(m\)道菜,每道菜有一个美味程度:然后给你了很多个关系,表示如果\(x\)刚好在\(y\)前面做的话,他的美味程度就会 ...

  6. 「日常训练」Kefa and Park(Codeforces Round #321 Div. 2 C)

    题意与分析(CodeForces 580C) 给你一棵树,然后每个叶子节点会有一家餐馆:你讨厌猫(waht?怎么会有人讨厌猫),就不会走有连续超过m个节点有猫的路.然后问你最多去几家饭店. 这题我写的 ...

  7. 「日常训练」Kefa and Company(Codeforces Round #321 Div. 2 B)

    题意与分析(CodeForces 580B) \(n\)个人,告诉你\(n\)个人的工资,每个人还有一个权值.现在从这n个人中选出m个人,使得他们的权值之和最大,但是对于选中的人而言,其他被选中的人的 ...

  8. 「日常训练」Case of Matryoshkas(Codeforces Round #310 Div. 2 C)

    题意与分析(CodeForces 556C) 为了将所有\(n\)个娃娃编号递增地串在一起(原先是若干个串,每个串是递增的), 我们有两种操作: 拆出当前串中最大编号的娃娃(且一定是最右边的娃娃). ...

  9. 「日常训练」ZgukistringZ(Codeforces Round #307 Div. 2 B)

    题意与分析(CodeForces 551B) 这他妈哪里是日常训练,这是日常弟中弟. 题意是这样的,给出一个字符串A,再给出两个字符串B,C,求A中任意量字符交换后(不限制次数)能够得到的使B,C作为 ...

随机推荐

  1. Problem H: STL——括号匹配

    Description 给出一堆括号,看其是否匹配,例如 ().()().(()) 这样的括号就匹配,       )(.)()) 而这样的括号就不匹配 Input 每一行代表一组测试样例,每组测试样 ...

  2. Ubuntu开启ApacheRewrite功能

    参考原文   :  http://www.knowsky.com/888354.html 1.安装好apache2之后,手动命令启用 执行加载Rewrite模块: a2enmod rewrite 执行 ...

  3. Easy UI下拉列表默认选中(多行)与为文本框赋值

    1.为单行文本框赋值 var data2 = $('#LoadArea').combobox("getData"); if (data2) { $('#id).combobox(' ...

  4. 11) 十分钟学会android--Intent消息处理与传递详解

    一个Android app通常都会有多个activities. 每个activity的界面都扮演者用户接口的角色,允许用户执行一些特定任务(例如查看地图或者是开始拍照等).为了让用户能够从一个acti ...

  5. 创建内部的yum源

    http://www.phy.duke.edu/~rgb/General/yum_HOWTO/yum_HOWTO/yum_HOWTO.html#toc6.2 主要的步骤包括: 1.创建Reposito ...

  6. VueJS使用笔记

    html: <script src='vue.js'></script> <div id='app'> <span>{{msg}}</span&g ...

  7. Python 面向对象(三) 魔术方法

    __getitem__ 在对实例或对象使用索引访问时调用,self[key]__dir__ 收集当前模块的信息,包括继承自其它基类(包括object类)的属性和方法 __new 定义如何创建实例__i ...

  8. 利用vertical-align实现行内元素对齐

    实际项目中,常常会遇到一排行内元素对齐排列的需求,但是往往它们是这样的 我们想要的其实是这样的 曾经我一度不得不使用定位来实现我想要的位置效果,将父元素设置 position:relative ,行内 ...

  9. DP 网易内推:合唱团

    链接:https://www.nowcoder.com/questionTerminal/661c49118ca241909add3a11c96408c8来源:牛客网 [编程题]合唱团 热度指数:18 ...

  10. ExpandableListView使用

    相关博客 ExpandableListView使用 博客内容记录 场景 有时候,使用ListView并不能满足应用程序所需要的功能.有些应用程序需要多组ListView,这时候我们就要使用一种新的控件 ...