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作为 ...
随机推荐
- 算法:javascript截取字符串
题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...
- .net ef core 领域设计代码转换(上篇)
一.前言 .net core 2.0正式版已经发布几个月了,经过研究,决定把项目转移过来,新手的话可以先看一些官方介绍 传送门:https://docs.microsoft.com/zh-cn/dot ...
- queue的入门
#include "iostream"#include "queue" using namespace std; void main12(){ queue &l ...
- Easy UI下拉列表默认选中(多行)与为文本框赋值
1.为单行文本框赋值 var data2 = $('#LoadArea').combobox("getData"); if (data2) { $('#id).combobox(' ...
- whonix官网部分翻译
Whonix:一个高安全的方式来Surfing the Internet Whonix是一个桌面操作系统,被设计用于高级安全和隐私.It realistically addresses attacks ...
- C#中的静态成员和非静态成员
C#的类中可以包含两种方法:C#静态方法与非静态方法.那么他们的定义有什么不同呢?他们在使用上会有什么不同呢? 让我们来看看最直观的差别:使用了static 修饰符的方法为静态方法,反之则是非静态方法 ...
- 《天书夜读:从汇编语言到windows内核编程》八 文件操作与注册表操作
1)Windows运用程序的文件与注册表操作进入R0层之后,都有对应的内核函数实现.在windows内核中,无论打开的是文件.注册表或者设备,都需要使用InitializeObjectAttribut ...
- [转载] Thrift原理简析(JAVA)
转载自http://shift-alt-ctrl.iteye.com/blog/1987416 Apache Thrift是一个跨语言的服务框架,本质上为RPC,同时具有序列化.发序列化机制:当我们开 ...
- 启用 Brotli 压缩算法,对比 Gzip 压缩 CDN 流量再减少 20%
Google 认为互联网用户的时间是宝贵的,他们的时间不应该消耗在漫长的网页加载中,因此在 2015 年 9 月 Google 推出了无损压缩算法 Brotli.Brotli 通过变种的 LZ77 算 ...
- linux使用yum安装mariadb
一,安装 yum install mariadb mariadb-server 二,如何设置密码 用root 进入mysql后 mysql>set password =password('你的 ...