B. Order Book(Codeforces Round #317 )

时间:2022-10-16 17:18:18
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 &num;340 Div&period;2 C)

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

  2. 「日常训练」Alternative Thinking(Codeforces Round &num;334 Div&period;2 C)

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

  3. 「日常训练」More Cowbell(Codeforces Round &num;334 Div&period;2 B)

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

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

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

  5. 「日常训练」Kefa and Dishes(Codeforces Round &num;321 Div&period; 2 D)

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

  6. 「日常训练」Kefa and Park(Codeforces Round &num;321 Div&period; 2 C)

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

  7. 「日常训练」Kefa and Company(Codeforces Round &num;321 Div&period; 2 B)

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

  8. 「日常训练」Case of Matryoshkas(Codeforces Round &num;310 Div&period; 2 C)

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

  9. 「日常训练」ZgukistringZ(Codeforces Round &num;307 Div&period; 2 B)

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

随机推荐

  1. 如何部署Icinga服务端

    Icinga是Nagios的一个变种,配置,使用方式几乎一样,而且完全兼容Nagios的插件.所以下面的部署方案对Nagios同样使用. 它还推出了两个中文版本,icinga-cn原版和icinga- ...

  2. CocoaPods报错:The dependency &grave;Alamofire &grave; is not used in any concrete target

    看到这个错误提示,首先看看自己的版本是不是 OS X EI Capitan,也就是10.10以后的版本,因为这个版本是比较新的版本,网络上找的那些安装cocoapod命令其实有些过时了,特别是创建po ...

  3. 【leetcode】Happy Number(easy)

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  4. LoadRunner中取Request、Response

    LoadRunner中取Request.Response LoadRunner两个“内置变量”: 1.REQUEST,用于提取完整的请求头信息. 2.RESPONSE,用于提取完整的响应头信息. 响应 ...

  5. NodeJS常用工具

    一.NodeJS版本管理器n或者nvm npm install -g n npm install n -g --registry=https://registry.npm.taobao.org --v ...

  6. 读取properties文件

    假设项目名称为myproject public class UtilConfig { private static final Properties prop; static { prop = new ...

  7. ☀【CSS3】形状

    CSS3shapeshttp://www.css3shapes.com/ <!DOCTYPE html> <html lang="zh-CN"> <h ...

  8. stream&comma;file&comma;filestream&comma;memorystream简单的整理

    一.Stream 什么是stream?(https://www.cnblogs.com/JimmyZheng/archive/2012/03/17/2402814.html#no8) 定义:提供字节序 ...

  9. MySQL基本操作命令

    数据库的基本操作命令 1.登录MySQL -- 进入数据库的方法一 mysql -uroot -pmysql # mysql 数据库密码(显示) -- 进入数据库的方法二 mysql -uroot - ...

  10. HDU - 4780费用流

    题意:M台机器要生产n个糖果,糖果i的生产区间在(si, ti),花费是k(pi-si),pi是实际开始生产的时间机器,j从初始化到生产糖果i所需的时间Cij,花费是Dij,任意机器从生产糖果i到生产 ...