B. Order Book(Codeforces Round #317 )
2 seconds
256 megabytes
standard input
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.
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.
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.
6 2
B 10 3
S 50 2
S 40 1
S 50 6
B 20 4
B 25 10
S 50 8
S 40 1
B 25 10
B 20 4
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 )的更多相关文章
- 「日常训练」Watering Flowers(Codeforces Round #340 Div.2 C)
题意与分析 (CodeForces 617C) 题意是这样的:一个花圃中有若干花和两个喷泉,你可以调节水的压力使得两个喷泉各自分别以\(r_1\)和\(r_2\)为最远距离向外喷水.你需要调整\(r_ ...
- 「日常训练」Alternative Thinking(Codeforces Round #334 Div.2 C)
题意与分析 (CodeForces - 603A) 这题真的做的我头疼的不得了,各种构造样例去分析性质... 题意是这样的:给出01字符串.可以在这个字符串中选择一个起点和一个终点使得这个连续区间内所 ...
- 「日常训练」More Cowbell(Codeforces Round #334 Div.2 B)
题意与分析(CodeForces 604B) 题意是这样的:\(n\)个数字,\(k\)个盒子,把\(n\)个数放入\(k\)个盒子中,每个盒子最多只能放两个数字,问盒子容量的最小值是多少(水题) 不 ...
- 「日常训练」Duff in the Army (Codeforces Round #326 Div.2 E)
题意(CodeForces 588E) 给定一棵\(n\)个点的树,给定\(m\)个人(\(m\le n\))在哪个点上的信息,每个点可以有任意个人:然后给\(q\)个询问,每次问\(u\)到\(v\ ...
- 「日常训练」Kefa and Dishes(Codeforces Round #321 Div. 2 D)
题意与分析(CodeForces 580D) 一个人有\(n\)道菜,然后要点\(m\)道菜,每道菜有一个美味程度:然后给你了很多个关系,表示如果\(x\)刚好在\(y\)前面做的话,他的美味程度就会 ...
- 「日常训练」Kefa and Park(Codeforces Round #321 Div. 2 C)
题意与分析(CodeForces 580C) 给你一棵树,然后每个叶子节点会有一家餐馆:你讨厌猫(waht?怎么会有人讨厌猫),就不会走有连续超过m个节点有猫的路.然后问你最多去几家饭店. 这题我写的 ...
- 「日常训练」Kefa and Company(Codeforces Round #321 Div. 2 B)
题意与分析(CodeForces 580B) \(n\)个人,告诉你\(n\)个人的工资,每个人还有一个权值.现在从这n个人中选出m个人,使得他们的权值之和最大,但是对于选中的人而言,其他被选中的人的 ...
- 「日常训练」Case of Matryoshkas(Codeforces Round #310 Div. 2 C)
题意与分析(CodeForces 556C) 为了将所有\(n\)个娃娃编号递增地串在一起(原先是若干个串,每个串是递增的), 我们有两种操作: 拆出当前串中最大编号的娃娃(且一定是最右边的娃娃). ...
- 「日常训练」ZgukistringZ(Codeforces Round #307 Div. 2 B)
题意与分析(CodeForces 551B) 这他妈哪里是日常训练,这是日常弟中弟. 题意是这样的,给出一个字符串A,再给出两个字符串B,C,求A中任意量字符交换后(不限制次数)能够得到的使B,C作为 ...
随机推荐
- [认证授权] 5.OIDC(OpenId Connect)身份认证授权(扩展部分)
在上一篇[认证授权] 4.OIDC(OpenId Connect)身份认证授权(核心部分)中解释了OIDC的核心部分的功能,即OIDC如何提供id token来用于认证.由于OIDC是一个协议族,如果 ...
- 在foreach的判断条件里执行方法会有效率问题吗?
楼猪平时一有空就有看别人代码的习惯,从许多优秀规范的代码中学习到了很多简约高效的写法和画龙点睛的思想精华.但是有的时候也会觉得某些写法很值得玩味.比如刚看到一段代码,在foreach的条件判断里加了一 ...
- Git命令补全配置
Git命令补全功能 1.下载下面的文件 https://github.com/sguo421/code/blob/master/git-completion.bash 2.放倒HOME目录下,设置为隐 ...
- 使用flex
Flex如何处理二义性模式: 1.词法分析器匹配输入时匹配尽可能多的字符串 2.如果两个模式都可以匹配的话,匹配在程序中更早出现的模式 上下文相关的记号 flex提供起始状态(start state) ...
- radis学习总结
Redis与Memcached的比较. 1.Memcached是多线程,而Redis使用单线程. 2.Memcached使用预分配的内存池的方式,Redis使用现场申请内存的方式来存储数据,并且可以配 ...
- 【小技巧解决大问题】使用 frp 突破阿里云主机无弹性公网 IP 不能用作 Web 服务器的限制
背景 今年 8 月份左右,打折价买了一个阿里云主机,比平常便宜了 2000 多块.买了之后,本想作为一个博客网站的,毕竟国内的服务器访问肯定快一些.满心欢喜的下单之后,却发现 http 服务,外网怎么 ...
- SHA1 安全哈希算法(Secure Hash Algorithm)
安全哈希算法(Secure Hash Algorithm)主要适用于数字签名标准 (Digital Signature Standard DSS)里面定义的数字签名算法(Digital Signatu ...
- P3092 [USACO13NOV]没有找零No Change
题目描述 Farmer John is at the market to purchase supplies for his farm. He has in his pocket K coins (1 ...
- 2)C语言的基本知识(C自考学习)
字符集 在C语言程序中允许出现的所有基本字符的组合称为C语言的字符集.C语言的字符集就是ASCII字符集.主要包含一下几类: 1)大小写英文字母A~Z,a~z(52个) 2)数字0-9(10个) 3) ...
- js 图片转换为base64 (2)
<input type="file" id="testUpload"> <img src="" id="img& ...