CodeForces

来源:互联网 发布:什么软件可以下载软件 编辑:程序博客网 时间:2024/04/30 20:59

In this task you need to process a set of stock exchange orders and use them to createorder book.

An order is an instruction of some participant to buy or sell stocks on stock exchange. The order numberi has price pi, directiondi — buy or sell, and integerqi. This means that the participant is ready to buy or sellqi stocks at pricepi for one stock. A valueqi is also known as avolume of an order.

All orders with the same price p and directiond are merged into one aggregated order with price p and directiond. 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 thans 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 depths for these orders.

Input

The input starts with two positive integers n ands (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 integerpi (0 ≤ pi ≤ 105) and an integerqi (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 depths. The output format for orders should be the same as in input.

Example
Input
6 2B 10 3S 50 2S 40 1S 50 6B 20 4B 25 10
Output
S 50 8S 40 1B 25 10B 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.

题意:

读题是最大的难点;给你n个账的记录,如果相同类且价格一样,则数量合并,取se价格从小到大前s个,再取b价格从大到小s个,最后先将s价格从大到小输出,再将b价格从大到小输出,读了好久没读懂,,,,

读懂题,这就是TMD水题;

#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>using namespace std;struct node{int a;int va;} s[10010];struct node1{int b;int vb;}s1[10010];struct node2{int c;int vc;}s2[10010];struct node3{int d;int vd;}s3[10010];bool cmp(node x,node y){if(x.a!=y.a)return x.a>y.a;return x.va>y.va;}bool cmp1(node1 x,node1 y){if(x.b!=y.b)return x.b<y.b;return x.b>y.b;}bool cmp2(node2 x,node2 y){if(x.c!=y.c)return x.c>y.c;return x.vc>y.vc;}bool cmp3(node3 x,node3 y){if(x.d!=y.d)return x.d>y.d;return x.vd>y.vd;}int main(){int n,m,p1,p2,i=0,j=0,p,t;char ch;memset(s,0,sizeof(s));memset(s1,0,sizeof(s1));scanf("%d %d",&n,&m);t=n;while(n--){getchar();scanf("%c",&ch);if(ch=='B'){scanf("%d %d",&p1,&p2);s[i].a=p1;bool flag=true;for(p=0;p<i;p++){if(s[p].a==s[i].a){flag=false;break;}}if(flag){s[i].va=p2;i++;}else{s[p].va+=p2;s[i].va=0;}}else{scanf("%d %d",&p1,&p2);s1[j].b=p1;bool flag=true;for(p=0;p<j;p++){if(s1[p].b==s1[j].b){flag=false;break;}}if(flag){s1[j].vb=p2;j++;}else{s1[p].vb+=p2;s1[j].vb=0;}}}sort(s,s+i,cmp);sort(s1,s1+j,cmp1);t=min(t,m);p=i;for(i=0;i<min(t,j);i++){s2[i].c=s1[i].b;s2[i].vc=s1[i].vb;}sort(s2,s2+i,cmp2);for(j=0;j<i;j++)printf("S %d %d\n",s2[j].c,s2[j].vc);for(i=0;i<min(t,p);i++){s3[i].d=s[i].a;s3[i].vd=s[i].va;}sort(s3,s3+i,cmp3);for(j=0;j<i;j++)printf("B %d %d\n",s3[j].d,s3[j].vd);return 0;}

1 0