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作为 ...
随机推荐
- Python 面向对象基础知识
面向对象基础知识 1.什么是面向对象编程? - 以前使用函数 - 类 + 对象 2.什么是类什么是对象,又有什么关系? class 类: def 函数1(): pass def 函数2(): pass ...
- 北京Python筛选过程中应注意什么
计算机初级爱好者普遍喜欢Python,因为Python干净利索,简单直接.它编写代码的速度非常的快,而且非常注重代码的可读性,非常适合多人参与的项目.很多人选择了培训,那么北京Python培训筛选过程 ...
- word,excel,ppt转Pdf,Pdf转Swf,通过flexpaper+swftools实现在线预览
其实这是我好几年前的项目,现在再用这种方式我也不建议了,毕竟未来flash慢慢会淘汰,此方式也是因为目测大部分人都装了flash,才这么做的,但是页面展示效果也不好.其实还是考虑收费的控件,毕竟收费的 ...
- Qemu 简述
Qemu 架构 Qemu 是纯软件实现的虚拟化模拟器,几乎可以模拟任何硬件设备,我们最熟悉的就是能够模拟一台能够独立运行操作系统的虚拟机,虚拟机认为自己和硬件打交道,但其实是和 Qemu 模拟出来的硬 ...
- SSM框架通过mybatis-generator自动生成代码
一.首先eclipse配置好maven环境,并且创建好一个SSM框架的工程 二.在pom.xml中添加plugin <build> <finalName>ssm_web< ...
- Cache类缓存
此处主要总结System.Web.Caching.Cache类 该类是用于存储常用信息的类,HttpRuntime.Cache以及HttpContext.Current.Cache都是该类的实例. 该 ...
- 《Metasploit魔鬼训练营》第四章(下)
p163 XSSF 默认kali 2.0中没有xssf,先下载:https://code.google.com/archive/p/xssf/downloads 将下载下来的zip文件解压,将其中的d ...
- matlab之“audioread”函数帮助文档翻译
课 程 设 计 (1) 原文 audioread Read audio file Syntax [y,Fs] = audioread(filename) [y,Fs] = audioread(fil ...
- re部分
0x01.Writeup-RE-CM_2 题目: 解题思路: 1.这个是经过xor的,王老师提示说用xortool,于是放进kali,装好之后执行 xortool CM_2.exe -b, 0.out ...
- Foundation框架的小总结
一.Foundation框架—结构体 一.基本知识 Foundation框架中包含了很多开发中常用的数据类型,如结构体,枚举,类等,是其他ios框架的基础. 如果要想使用foundation框架中的数 ...