Problem:
Problem B:
e-Market
The city of Hakodate recently established a commodity exchange market. To participate in the market, each dealer transmits through the Internet an order consisting of his or her name, the type of the order (buy or sell), the name of the commodity, and the quoted price.
In this market a deal can be made only if the price of a sell order is lower than or equal to the price of a buy order. The price of the deal is the mean of the prices of the buy and sell orders, where the mean price is rounded downward to the nearest integer. To exclude dishonest deals, no deal is made between a pair of sell and buy orders from the same dealer. The system of the market maintains the list of orders for which a deal has not been made and processes a new order in the following manner.
For a new sell order, a deal is made with the buy order with the highest price in the list satisfying the conditions. If there is more than one buy order with the same price, the deal is made with the earliest of them.
For a new buy order, a deal is made with the sell order with the lowest price in the list satisfying the conditions. If there is more than one sell order with the same price, the deal is made with the earliest of them.
The market opens at 7:00 and closes at 22:00 everyday. When the market closes, all the remaining orders are cancelled. To keep complete record of the market, the system of the market saves all the orders it received everyday.
The manager of the market asked the system administrator to make a program which reports the activity of the market. The report must contain two kinds of information. For each commodity the report must contain informationon the lowest, the average and the highest prices of successful deals. For each dealer, the report must contain information on the amounts the dealer paid and received for commodities.
Input
The input contains several data sets. Each data set represents the record of the market on one day. The first line of each data set contains an integer
n
(
n
< 1000)  which is the number of orders in the record. Each line of the record describes an order, consisting of the name of the dealer, the type of the order, the name of the commodity, and the quoted price. They are separated by a single space character.
The name of a dealer consists of capital alphabetical letters and is less than 10 characters in length. The type of an order is indicated by a string, "
BUY
" or "
SELL
". The name of a commodity is a single capital letter. The quoted price is a positive integer less than 1000.
The orders in a record are arranged according to time when they were received and the first line of the record corresponds to the oldest order.
The end of the input is indicated by a line containing a zero.
Output
The output for each data set consists of two parts separated by a line containing two hyphen (`
-
') characters.
The first part is output for commodities. For each commodity, your program should output the name of the comm
...(truncated)

Submitted solutions:

code_snippet: #define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
#include <cctype>
#include <utility>
#include <assert.h>
  
using namespace std;
  
typedef long long ll;
typedef pair <int,int> P;
typedef pair <int,P> PP;
  
static const double EPS = 1e-8;

class Deal {
public:
  string _dealer_name;
  string _type;
  string _commodity_name;
  int _price;
  int _id;
  Deal(string dealer_name,string type,string commodity_name,int price,int id) 
    : _dealer_name(dealer_name), _type(type), _commodity_name(commodity_name), _price(price), _id(id) {}
};

class DealerResult {
public:
  int _paid;
  int _received;
  DealerResult() : _paid(0),_received(0) {}
};

class CommodityResult {
public:
  int _lowest;
  int _mean;
  int _highest;
  vector<int> _purchase_history;
  void init(){
    if(_purchase_history.size() > 0){
      sort(_purchase_history.begin(),_purchase_history.end());
      _highest = *(_purchase_history.end()-1);
      _lowest = *(_purchase_history.begin());
      
      int sum = 0;
      for(int i=0;i<_purchase_history.size();i++){
        sum += _purchase_history[i];
      }
      _mean = sum / _purchase_history.size();
    }
  }
  CommodityResult() : _lowest(0), _mean(0), _highest(0) {}
};

int main(){
  int total_dealers;
  while(~scanf("%d",&total_dealers)){
    if(total_dealers == 0) break;

    vector<Deal> deals;
    for(int dealer_i = 0; dealer_i < total_dealers; dealer_i++){
      string dealer_name,type,commodity_name;
      int price;
      cin >> dealer_name >> type >> commodity_name >> price;
      deals.push_back(Deal(dealer_name,type,commodity_name,price,dealer_i));
    }

    bool used[1001];
    memset(used,false,sizeof(used));

    map<string,CommodityResult> commodity_result;
    map<string,DealerResult> dealer_result;

    for(int dealer_i = 0; dealer_i < total_dealers; dealer_i++){
      dealer_result[deals[dealer_i]._dealer_name] = DealerResult();
    }

    for(int dealer_i = 0; dealer_i < total_dealers; dealer_i++){
      if(used[dealer_i]) continue;
      int current_sell_price = INF; // lowest
      int current_buy_price = -INF; // highest
      int target_id = -1;

      for(int dealer_j = 0; dealer_j < dealer_i; dealer_j++){
        if(used[dealer_j]) continue;
        if(deals[dealer_i]._type == deals[dealer_j]._type) continue;
        if(deals[dealer_i]._dealer_name == deals[dealer_j]._dealer_name) continue;
        if(deals[dealer_i]._commodity_name != deals[dealer_j]._commodity_name) continue;

        if(deals[dealer_i]._type == "BUY"){
          if(deals[dealer_j]._price <= deals[dealer_i]._price){
            if(current_sell_price > deals[dealer_j]._price){
              current_sell_price = deals[dealer_j]._price;
              target_id = dealer_j;
            }
          }
        }
        else if(deals[dealer_i]._type == "SELL"){
          if(deals[dealer_j]._price >= deals[dealer_i]._price){
            if(current_buy_price < deals[dealer_j]._price){
              current_buy_price = deals[dealer_j]._price;
              target_id = dealer_j;
            }
          }
        }
      }

      if(target_id != -1){
        if(deals[dealer_i]._type == "BUY"){
          dealer_result[deals[dealer_i]._dealer_name]._paid += (current_sell_price + deals[dealer_i]._price) / 2;
          dealer_result[deals[target_id]._dealer_name]._received += (current_sell_price + deals[dealer_i]._price) / 2;
          commodity_result[deals[dealer_i]._commodity_name]._purchase_history
            .push_back((current_sell_price + deals[dealer_i]._price) / 2);
        }

        if(deals[dealer_i]._type == "SELL"){
          dealer_result[deals[dealer_i]._dealer_name]._received += (current_buy_price + deals[dealer_i]._price) / 2;
          dealer_result[deals[target_id]._dealer_name]._paid += (current_buy_price + deals[dealer_i]._price) / 2;
          commodity_result[deals[dealer_i]._commodity_name]._purchase_history
            .push_back((current_buy_price + deals[dealer_i]._price) / 2);
        }
        
        used[dealer_i] = true;
        used[target_id] = true;
      }
    }

    for(map<string,CommodityResult>::iterator it = commodity_result.begin();
        it != commodity_result.end();
        it++){
      it->second.init();
      printf("%s %d %d %d\n",it->first.c_str(),it->second._lowest,it->second._mean,it->second._highest);
    }

    printf("--\n");
    for(map<string,DealerResult>::iterator it = dealer_result.begin();
        it != dealer_result.end();
        it++){

      printf("%s %d %d\n",it->first.c_str(),it->second._paid,it->second._received);
    }
    printf("----------\n");
  }
}
accuracy: 1
time_ms: 10
memory_kb: 1496
final_rank: 6

code_snippet: //#include <bits/stdc++.h>
//#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <vector>
#include <algorithm>
#include <cctype>
#include <fstream>
#include <map>
#include <list>
#include <set>
#include <string>
#include <stack>
#include <bitset>

#define sz 50005
#define pb(a) push_back(a)
#define pp pop_back()
#define ll long long
#define fread freopen("input.txt","r",stdin)
#define fwrite freopen("output.txt","w",stdout)
#define inf (1e9)
#define chng(a,b) a^=b^=a^=b;
#define clr(abc,z) memset(abc,z,sizeof(abc))
#define PI acos(-1)
#define fr(i,a,b) for(i=a;i<=b;i++)
#define print1(a)    cout<<a<<endl
#define print2(a,b) cout<<a<<" "<<b<<endl
#define print3(a,b,c) cout<<a<<" "<<b<<" "<<c<<endl
#define mod 1000000007
using namespace std;

struct person
{
    char name[15];
    int buy, sell;
    person(char line[])
    {
        strcpy(name, line);
        buy=sell=0;
    }
};

struct commodity
{
    char name[5];
    int n, sum, mn, mx;
    commodity(char line[])
    {
        sum = n = 0;
        mn = inf;
        mx = -inf;
        strcpy(name,line );
    }
};

struct product
{
    char name[15];
    char prod[5];
    int price;
    int cnt;

    product(char line1[], char line2[], int p, int c)
    {
        strcpy(name, line1);
        strcpy(prod, line2);
        price = p;
        cnt = c;
    }
};


vector<person>per;
vector<commodity>thing;
vector<product>buys, sells;

bool compbuy(const product &a, const product &b)
{
    if(a.price==b.price) return a.cnt<b.cnt;
    return a.price>b.price;
}
bool compsell(const product &a, const product &b)
{
    if(a.price==b.price) return a.cnt<b.cnt;
    return a.price<b.price;
}

bool printCompThing(const commodity &a, const commodity &b)
{
    return strcmp(a.name,b.name)<0;
}

bool printCompPerson(const person &a, const person &b)
{
    return strcmp(a.name,b.name)<0;
}

void printall()
{
    sort(thing.begin(), thing.end(), printCompThing);
    for (int i = 0; i<thing.size(); i++)
        printf("%s %d %d %d\n", thing[i].name, thing[i].mn, thing[i].sum/thing[i].n, thing[i].mx);
    puts("--");

    sort(per.begin(), per.end(), printCompPerson);
    for (int i = 0; i<per.size(); i++)
        printf("%s %d %d\n", per[i].name, per[i].buy, per[i].sell);
    puts("----------");

    return;
}

void printBuyList()
{
    cout<<"Buy ---------------"<<endl;
    for (int i = 0; i<buys.size(); i++)
        cout<<"N: "<<buys[i].name<<" Prod: "<<buys[i].prod<<" Price: "<<buys[i].price<<" cnt: "<<buys[i].cnt<<endl;
    cout<<"-------------------"<<endl;
    return;
}

void printSellList()
{
    cout<<"Sell---------------"<<endl;
    for (int i = 0; i<sells.size(); i++)
        cout<<"N: "<<sells[i].name<<" Prod: "<<sells[i].prod<<" Price: "<<sells[i].price<<" cnt: "<<sells[i].cnt<<endl;
    cout<<"-------------------"<<endl;
    return;
}




int main()
{
#ifdef ENAM
    fread;
    fwrite;
#endif // ENAM
    int t, n, m, cas=1, price, x,y, dealprice;
    char name[20], type[10], com[5];


    while(~scanf("%d", &n)&&n)
    {
        per.clear();
        thing.clear();
        buys.clear();
        sells.clear();

        for (int i = 0; i<n; i++)
        {
//            printBuyList();
//            printSellList();
            scanf(" %s %s %s %d", name, type, com, &price);
            int j;
            for (j = 0; j<per.size(); j++)
                if(!strcmp(per[j].name, name)) break;

            if(j==per.size()) per.pb(person(name));

//            printf("::>> %s %s %s %d\n", name, type, com, price);

            if(!strcmp(type, "SELL"))
            {
                if(!buys.empty())
                {
                    sort(buys.begin(), buys.end(), compbuy);
                    for (j = 0; j<buys.size(); j++)
                    {
                        if(strcmp(buys[j].name, name) && !strcmp(com,buys[j].prod) && price<= buys[j].price)
                        {
                            dealprice = (price+buys[j].price)/2;

                            int ii;
                            //selling person update
                            for (ii = 0; ii<per.size(); ii++ )
                                if(!strcmp(per[ii].name, name)) break;
                            per[ii].sell+=dealprice;

                            //buying person update
                            for (ii = 0; ii<per.size(); ii++ )
                                if(!strcmp(per[ii].name, buys[j].name)) break;
                            per[ii].buy+=dealprice;

                            //commodity update
                            for (ii = 0; ii<thing.size(); ii++ )
                                if(!strcmp(thing[ii].name, com)) break;

                            if(ii==thing.size()) thing.pb(commodity(com));

                            thing[ii].n++;
                            thing[ii].sum+=dealprice;
                            thing[ii].mn = min(thing[ii].mn, dealprice);
                            thing[ii].mx = max(thing[ii].mx, dealprice);

                            break;
                        }
                    }

                    if(j==buys.size()) sells.pb(product(name,com, price,i));
                    else buys.erase(buys.begin()+j);
                }
                else sells.pb(product(name,com, price,i));
            }
            else
            {
//                printf(":->%s %s %s %d\n", name, type, com, price);
                if(!sells.empty())
                {
                    sort(sells.begin(), sells.end(), compsell);
                    int j;
                    for (j = 0; j<sells.size(); j++)
                    {
                        if(strcmp(sells[j].name, name) && !strcmp(com,sells[j].prod) && sells[j].price <= price)
                        {
                            dealprice = (price+sells[j].price)/2;

                            int ii;
                            //buying person update
                            for (ii = 0; ii<per.size(); ii++ )
                                if(!strcmp(per[ii].name, name)) break;
                            per[ii].buy+=dealprice;

                            //selling person update
                            for (ii = 0; ii<per.size(); ii++ )
                                if(!strcmp(per[ii].name, sells[j].name)) break;
                            per[ii].sell+=dealprice;

                            //commodity update
                            for (ii = 0; ii<thing.size(); ii++ )
                                if(!strcmp(thing[ii].name, com)) break;

                            if(ii==thing.size()) thing.pb(commodity(com));

                            thing[ii].n++;
                            thing[ii].sum+=dealprice;
                            thing[ii].mn = min(thing[ii].mn, dealprice);
                            thing[ii].mx = max(thing[ii].mx, dealprice);

                            break;
                        }
                    }

                    if(j==sells.size()) buys.pb(product(name,com, price,i));
                    else sells.erase(sells.begin()+j);
                }
                else buys.pb(product(name,com, price,i));
            }
        }
        printall();
    }
    return 0;
}

/*
3
PERLIS SELL A 300
MILKES BUY A 200
HAMMING SELL A 100
4
BACKUS SELL A 10
FLOYD BUY A 20
IVERSON SELL B 30
BACKUS BUY B 40
7
MILKINSON SELL A 500
MCCARTHY BUY C 300
MILKINSON SELL C 200
DIJKSTRA SELL B 100
BACHMAN BUY A 400
DIJKSTRA BUY A 600
MILKINSON SELL A 300
2
ABCD SELL X 10
ABC BUY X 15
2
A SELL M 100
A BUY M 100
0
*/
accuracy: 1
time_ms: 10
memory_kb: 1256
final_rank: 3

code_snippet: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <tuple>
#include <map>

using namespace std;

int main(){
	int n;
	while (cin >> n, n){
		vector<tuple<string, char, string, int>> v(n);
		vector<int> used(n);

		map<string, pair<int, int>> comm_minmax;
		map<string, pair<int, int>> comm_sumcnt;
		map<string, pair<int, int>> dealer;

		for (int i = 0; i < n; ++i){
			string a, b, c;
			int x;
			cin >> a >> b >> c >> x;
			v[i] = make_tuple(a, b[0], c, x);
			dealer[a];
			int idx = -1, opt = x;
			bool buy = b[0] == 'B';
			for (int j = 0; j < i; ++j){
				if (used[j]) continue;
				if (a != get<0>(v[j]) && b[0] != get<1>(v[j]) && c == get<2>(v[j])){
					int y = get<3>(v[j]);
					if (buy && y <= opt){
						opt = y - 1, idx = j;
					}
					if (!buy && y >= opt){
						opt = y + 1, idx = j;
					}
				}
			}
			if (idx < 0) continue;
			used[i] = used[idx] = 1;
			string buyer = a, seller = get<0>(v[idx]);
			if (!buy) swap(buyer, seller);

			if (buyer == seller) continue;
			auto &c1 = comm_minmax[c], &c2 = comm_sumcnt[c], &db = dealer[buyer], &ds = dealer[seller];
			int val = (x + get<3>(v[idx])) / 2;
			c1.first = c1.first ? min(c1.first, val) : val, c1.second = c1.second ? max(c1.second, val) : val;
			c2.first += val, ++c2.second;
			db.first += val, ds.second += val;
		}

		for (auto &c : comm_minmax){
			int mean = comm_sumcnt[c.first].first / comm_sumcnt[c.first].second;
			cout << c.first << ' ' << c.second.first << ' ' << mean << ' ' << c.second.second << '\n';
		}
		cout << "--\n";
		for (auto &d : dealer){
			cout << d.first << ' ' << d.second.first << ' ' << d.second.second << '\n';
		}
		cout << "----------\n";
	}

	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1356
final_rank: 4

code_snippet: #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;

#define INF (1<<29)

typedef pair<int, int> P;
typedef pair<P, P> PP;

struct Order{
  string pname, dname;
  string order;
  int price;
  Order(string pname, string dname, string order, int price) :
    pname(pname), dname(dname), order(order), price(price){}
};
  

int main(){
  int n;
  while(cin >> n, n){
    bool used[1010];
    memset(used, false, sizeof(used));
    vector<Order> market;
    map<string, P> person;
    map<string, PP> item;
    
    string pname, dname, order;
    int price;
    for(int i = 0 ; i < n ; i++){
      cin >> pname >> order >> dname >> price;
      market.push_back(Order(pname, dname, order, price));
      if(person.find(pname) == person.end()) person[pname] = P(0, 0);
      if(order == "SELL"){
	int number = -1;
	int p = -1;
	for(int j = 0 ; j < (int)market.size() ; j++){
	  if(market[j].order == "BUY" && market[j].pname != pname &&
	     market[j].dname == dname && market[j].price >= price &&
	     !used[j]){
	    if(p < market[j].price){
	      number = j;
	      p = market[j].price;
	    }
	  }
	}
	if(number == -1) continue;
	int deal = (price + p) / 2;
	person[pname].second += deal;
	person[market[number].pname].first += deal;
	item[dname].first.first += deal;
	item[dname].first.second++;
	item[dname].second.first = max(item[dname].second.first, deal);
	if(item[dname].first.second == 1) item[dname].second.second = deal;
	else item[dname].second.second = min(item[dname].second.second, deal);
	used[i] = used[number] = true;
      }

      if(order == "BUY"){
	int number = -1;
	int p = INF;
	for(int j = 0 ; j < (int)market.size() ; j++){
	  if(market[j].order == "SELL" && market[j].pname != pname &&
	     market[j].dname == dname && market[j].price <= price &&
	     !used[j]){
	    if(p > market[j].price){
	      number = j;
	      p = market[j].price;
	    }
	  }
	}
	if(number == -1) continue;
	int deal = (price + p) / 2;
	person[pname].first += deal;
	person[market[number].pname].second += deal;	       
	used[i] = used[number] = true;
	item[dname].first.first += deal;
	item[dname].first.second++;
	item[dname].second.first = max(item[dname].second.first, deal);
	if(item[dname].first.second == 1) item[dname].second.second = deal;
	else item[dname].second.second = min(item[dname].second.second, deal);      
      }
    }
    
    for(map<string, PP>::iterator it = item.begin() ; it != item.end() ; it++){
      cout << it->first << ' '
	   << it->second.second.second << ' '
	   << it->second.first.first / it->second.first.second << ' '
	   << it->second.second.first << endl;	  

    }
    cout << "--" << endl;
    
    for(map<string, P>::iterator it = person.begin() ; it != person.end() ; it++){
      cout << it->first << ' '
	   << it->second.first << ' '
	   << it->second.second << endl;
    }
    cout << "----------" << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1416
final_rank: 5

code_snippet: #include<iostream>
#include<algorithm>
#include<map>
#include<cmath>
#include<vector>
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define inf (1<<29)
#define all(n) (n).begin(),(n).end()
using namespace std;
typedef pair<int,int> ii;//first -> paid, received
struct P
{
  int low,avg,hig,total,cnt;
  P(int low=inf,int avg=0,int hig=-inf):low(low),avg(avg),hig(hig){total = cnt = 0;}
};
struct Merchandise
{
  int index,price;
  string name,dealer;
  bool completed;
  Merchandise(int index=-inf,string dealer="x",string name="x",int price=-inf):index(index),price(price),name(name),dealer(dealer){completed = false;}
  bool operator < (const Merchandise &a)const
  {
    if(price == a.price)return index < a.index;
    return price < a.price;
  }
};
int n;

bool cmp(const Merchandise &a,const Merchandise &b)//use this method to sort the vector Buy
{
  if(a.price == b.price)return a.index < b.index;
  return a.price > b.price;
}

int main()
{
  while(cin >> n,n)
    {
      map<string,ii> dealer;
      map<string,P>  commodity;
      vector<Merchandise> Sell,Buy;
      rep(i,n)
	{
	  string sdealer,name,order;
	  int price;
	  cin >> sdealer >> order >> name >> price;
	  dealer[sdealer] = dealer[sdealer];
	  if(order[0] == 'S')
	    {
	      Sell.push_back(Merchandise(i,sdealer,name,price));
	      int index = Sell.size()-1;
	      int bsize = Buy.size();
	      rep(j,bsize)
		{
		  if(Buy[j].completed)continue;
		  if(Buy[j].dealer == sdealer)continue;
		  if(Buy[j].name == name && Buy[j].price >= price)
		    {
		      //cout << "Sell " << sdealer << " the commodity " << name << " to " << Buy[j].dealer << endl;
		      Buy[j].completed = true;
		      Sell[index].completed = true;
		      int value = (int)floor((Buy[j].price+price)/2.0);
		      dealer[Buy[j].dealer].first += value;
		      dealer[sdealer].second += value;
		      commodity[name].low = min(commodity[name].low,value);
		      commodity[name].hig = max(commodity[name].hig,value);
		      commodity[name].total += value;
		      commodity[name].cnt++;
		      break;
		    }
		}
	      sort(all(Sell));
	    }
	  else
	    {
	      Buy.push_back(Merchandise(i,sdealer,name,price));
	      int index = Buy.size()-1;
	      int ssize = Sell.size();
	      rep(j,ssize)
		{
		  if(Sell[j].completed)continue;
		  if(Sell[j].dealer == sdealer)continue;
		  if(Sell[j].name == name && Sell[j].price <= price)
		    {
		      //cout << "Buy " << sdealer << " the commodity " << name << " from " << Sell[j].dealer << endl;
		      Sell[j].completed = true;
		      Buy[index].completed = true;
		      int value = (int)floor((Sell[j].price+price)/2.0);
		      dealer[Sell[j].dealer].second += value;
		      dealer[sdealer].first += value;
		      commodity[name].low = min(commodity[name].low,value);
		      commodity[name].hig = max(commodity[name].hig,value);
		      commodity[name].total += value;
		      commodity[name].cnt++;
		      break;
		    }
		}
	      sort(all(Buy),cmp);
	    }
	}

      for(map<string,P>::iterator it = commodity.begin(); it != commodity.end(); it++)
	{
	  cout << (it->first) << " " << (it->second.low) << " " << (int)floor((it->second.total)/(it->second.cnt)) << " " << (it->second.hig) << endl;
	}
      cout << "--" << endl;

      for(map<string,ii>::iterator it = dealer.begin();it != dealer.end();it++)
	{
	  cout << (it->first) << " " << (it->second.first) << " " << (it->second.second) << endl; 
	}
      cout << "----------" << endl;

    }
  return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 1376
final_rank: 7

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
using namespace std;

#define MAX (1<<24)
#define MIN (-1)

class State
{
public:
	string n,t;
	char c;
	int p,r;
	State(string n, string t, char c, int p, int r)
		:n(n),t(t),c(c),p(p),r(r)
	{}

	bool operator<(const State& s) const
	{
		if(t!=s.t) return t<s.t;
		if(t=="SELL" && p!=s.p) return p<s.p;
		if(t=="BUY" && p!=s.p) return p>s.p;

		return r<s.r;
	}
};

class Dealer
{
public:
	string n;
	int s,b;
	Dealer(string n, int s, int b)
		:n(n),s(s),b(b)
	{}

	bool operator<(const Dealer& d) const
	{
		return n<d.n;
	}
};

int main()
{
	int N;
	while(cin >> N, N)
	{
		int mi[30], ma[30], tt[30], nm[30];
		for(int i=0; i<30; i++)
		{
			mi[i]=MAX;
			ma[i]=MIN;
			tt[i]=0;
			nm[i]=0;
		}

		int n=0;
		map<string, int> d;
		vector<State> st;
		vector<Dealer> dr;
		for(int i=0; i<N; i++)
		{
			bool dealed=0;
			string a,b;
			char c;
			int p;
			
			cin >> a >> b >> c >> p;
			if(!d.count(a))
			{
				d[a]=dr.size();
				dr.push_back(Dealer(a,0,0));
			}

			for(int j=0; j<st.size(); j++)
			{
				if(st[j].t==b) continue;
				if(st[j].c!=c) continue;

				if(st[j].n==a) continue;

				if(b=="SELL")
				{
					if(p<=st[j].p)
					{
						int np=(p+st[j].p)/2;
						int z=st[j].c-'A';
						mi[z]=min(mi[z], np);
						ma[z]=max(ma[z], np);
						tt[z]+=np;
						nm[z]++;

						dr[d[a]].s+=np;
						dr[d[st[j].n]].b+=np;

						dealed=true;
						st.erase(st.begin()+j);
					}

					
				}
				else
				{
					if(p>=st[j].p)
					{
						int np=(p+st[j].p)/2;
						int z=st[j].c-'A';
						mi[z]=min(mi[z], np);
						ma[z]=max(ma[z], np);
						tt[z]+=np;
						nm[z]++;

						dr[d[a]].b+=np;
						dr[d[st[j].n]].s+=np;

						dealed=true;
						st.erase(st.begin()+j);
					}
				}

				break;
			}

			if(!dealed) st.push_back(State(a,b,c,p,i));
			sort(st.begin(), st.end());
		}

		for(int i=0; i<26; i++)
		{
			if(nm[i]==0) continue;
			cout << (char)(i+'A') << " " << mi[i] << " "  << (int)(tt[i]/nm[i]) << " "<< ma[i] << endl;
		}
		cout << "--" << endl;

		sort(dr.begin(), dr.end());

		for(int i=0; i<dr.size(); i++)
		{
			cout << dr[i].n << " " << dr[i].b << " " << dr[i].s << endl;
		}
		cout << "----------" << endl;
	}

}
accuracy: 1
time_ms: 140
memory_kb: 992
final_rank: 8

code_snippet: #include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;

class P{
public:
	int price,id;
	string name,com;
	bool flg; //true:wü, false:p

	P(string _name,bool _flg,string _com,int _price,int _id){
		name = _name;
		flg = _flg;
		com = _com;
		price = _price;
		id = _id;
	}

	bool operator<(const P &p)const{
		if(price == p.price) return id < p.id;
		if(flg) return price > p.price;
		return price < p.price;
	}
};

typedef struct{int min,sum,max,n;}Com;
typedef struct{int pay,recv;}Dealer;

int main(void){
	int n;

	while(cin>>n,n){
		vector<P> buy,sell;
		map<string,Com> mc;
		map<string,Dealer> md;

		for(int i=0;i<n;i++){
			string name,flg,com;
			int price;
			cin>>name>>flg>>com>>price;

			md[name].pay += 0; //L[o^Ì½ß

			if(flg == "BUY"){
				bool buyFlg = false;
				for(int j=0;j<sell.size();j++){
					if(com == sell[j].com && name != sell[j].name && price >= sell[j].price){
						int deal = (price + sell[j].price) / 2;

						if(mc[com].min == 0){
							Com tmp = {deal,deal,deal,1};
							mc[com] = tmp;
						}
						else{
							if(mc[com].max < deal) mc[com].max = deal;
							if(mc[com].min > deal) mc[com].min = deal;
							mc[com].sum += deal;
							mc[com].n++;
						}

						md[name].pay += deal;
						md[sell[j].name].recv += deal;
						sell.erase(sell.begin()+j);
						buyFlg = true;
						break;
					}
				}

				if(!buyFlg){
					buy.push_back(P(name,true,com,price,i));
					sort(buy.begin(),buy.end());
				}
			}
			else{
				bool sellFlg = false;
				for(int j=0;j<buy.size();j++){
					if(com == buy[j].com && name != buy[j].name && buy[j].price >= price){
						int deal = (price + buy[j].price) / 2;

						if(mc[com].min == 0){
							Com tmp = {deal,deal,deal,1};
							mc[com] = tmp;
						}
						else{
							if(mc[com].max < deal) mc[com].max = deal;
							if(mc[com].min > deal) mc[com].min = deal;
							mc[com].sum += deal;
							mc[com].n++;
						}

						md[buy[j].name].pay += deal;
						md[name].recv += deal;
						buy.erase(buy.begin()+j);
						sellFlg = true;
						break;
					}
				}

				if(!sellFlg){
					sell.push_back(P(name,false,com,price,i));
					sort(sell.begin(),sell.end());
				}
			}

		}

		for(map<string,Com>::iterator i=mc.begin();i!=mc.end();i++){
			cout<<(i->first)<<" "<<(i->second).min<<" "<<(i->second).sum / (i->second).n<<" "<<(i->second).max<<endl;
		}
		cout<<"--\n";

		for(map<string,Dealer>::iterator i=md.begin();i!=md.end();i++){
			cout<<(i->first)<<" "<<(i->second).pay<<" "<<(i->second).recv<<endl;
		}
		cout<<"----------\n";
	}

	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 980
final_rank: 2

code_snippet: #include<iostream>
#include<string>
#include<map>
#include<vector>

using namespace std;
const int infty = 100000;

struct order{
  string who;
  string does;
  string item;
  int val;
};

struct item{
  vector<int> v;
};

struct dealer{
  int sell;
  int buy;
};

#define SELL 0
#define BUY 1

int minimumValue(const vector<int> &v){
  int ret = infty;
  for(int i = 0; i < (int)v.size(); ++i) ret = min(ret, v[i] );
  return ret;
}
int maximumValue(const vector<int> &v){
  int ret = 0;
  for(int i = 0; i < (int)v.size(); ++i) ret = max(ret, v[i] );
  return ret;
}
int meanValue(const vector<int> &v){
  int sum = 0;
  for(int i = 0; i < (int)v.size(); ++i)sum+=v[i];
  return sum / v.size();
}

int main()
{
  while(true){
    int records;
    cin >> records;
    if(records==0)break;
    vector<order> os;
    map<string,item> items;
    map<string,dealer> dealers;
    bool dealed[records];
    for(int i = 0; i < records; ++i){
      order o;
      cin >> o.who >> o.does >> o.item >> o.val;
      items[o.item] = item();
      dealers[o.who] = dealer();
      os.push_back(o);
      dealed[i] = false;
    }
    for(int i = 0; i < (int)os.size(); ++i){
      int max_deal_val = 0;
      int min_deal_val = infty;
      int anti_dealer = infty;
      for(int j = i-1; j >= 0; --j){
	if( os[j].who != os[i].who && os[j].does != os[i].does && os[j].item == os[i].item &&
	    !dealed[i] && !dealed[j] ){
	  if( os[i].does == "SELL" ){
	    if( os[i].val <= os[j].val ){
	      if( os[j].val > max_deal_val ){
		max_deal_val = os[j].val;
		anti_dealer = j;
	      }else if( os[j].val == max_deal_val ){
		anti_dealer = min( anti_dealer, j );
	      }
	    }
	  }else{
	    if( os[j].val <= os[i].val ){
	      if( os[j].val < min_deal_val ){
		min_deal_val = os[j].val;
		anti_dealer = j;
	      }else if( os[j].val == min_deal_val ){
		anti_dealer = min( anti_dealer, j );
	      }
	    }
	  }
	}
      }
      if( anti_dealer < infty ){
	// deal occured
	dealed[i] = true;
	dealed[anti_dealer] = true;
	int deal_val = (os[anti_dealer].val + os[i].val) /2;
	items[ os[i].item ].v.push_back( deal_val );
	if( os[i].does == "SELL" ){
	  dealers[ os[i].who ].sell += deal_val;
	  dealers[ os[anti_dealer].who ].buy += deal_val;
	}else{
	  dealers[ os[i].who ].buy += deal_val;
	  dealers[ os[anti_dealer].who ].sell += deal_val;
	}
      }
    }

    for(map<string,item>::iterator it = items.begin(); it != items.end(); ++it){
      if( it->second.v.size() > 0 ){
	int minv = minimumValue( it->second.v );
	int maxv = maximumValue( it->second.v );
	int avgv = meanValue( it->second.v );
	cout << it->first << ' ' << minv << ' ' << avgv << ' ' << maxv << endl;
      }
    }
    cout << "--" << endl;
    for(map<string,dealer>::iterator it = dealers.begin(); it != dealers.end(); ++it){
      cout << it->first << ' ' << it->second.buy << ' ' << it->second.sell << endl;
    }
    cout << "----------" << endl;
    
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 0
final_rank: 1