#include #include #include #include using namespace std; struct deal_t { int lowest, summation, highest, n; deal_t() { lowest = INT_MAX; summation = highest = n = 0; } }; struct dealer_t { int paid, received; dealer_t() { paid = received = 0; } }; struct order_t { string dealer; bool sell; string commodity; int price; }; int main() { int n; while (cin >> n && n > 0) { order_t orders[1024]; map deals; map dealers; deals.clear(); for (int i = 0; i < n; i++) { string type; cin >> orders[i].dealer >> type >> orders[i].commodity >> orders[i].price; if (type == "SELL") { orders[i].sell = true; } else { orders[i].sell = false; } if (dealers.find(orders[i].dealer) == dealers.end()) { dealers[orders[i].dealer] = dealer_t(); } int target = -1, targetprice; for (int j = 0; j < i; j++) { if (orders[i].dealer != orders[j].dealer && orders[i].sell != orders[j].sell && orders[i].commodity == orders[j].commodity) { int selling = (orders[i].sell ? i : j), buying = (orders[i].sell ? j : i); if (orders[selling].price <= orders[buying].price) { /* it can be candidate */ if (orders[i].sell) { if (target < 0 || targetprice < orders[j].price) { target = j; targetprice = orders[j].price; } } else { if (target < 0 || targetprice > orders[j].price) { target = j; targetprice = orders[j].price; } } } } } if (target >= 0) { int final_price = (orders[i].price + orders[target].price) / 2; if (deals[orders[i].commodity].lowest > final_price) { deals[orders[i].commodity].lowest = final_price; } if (deals[orders[i].commodity].highest < final_price) { deals[orders[i].commodity].highest = final_price; } deals[orders[i].commodity].n++; deals[orders[i].commodity].summation += final_price; if (orders[i].sell) { dealers[orders[i].dealer].received += final_price; } else { dealers[orders[i].dealer].paid += final_price; } if (orders[target].sell) { dealers[orders[target].dealer].received += final_price; } else { dealers[orders[target].dealer].paid += final_price; } orders[target].commodity = "Done"; orders[i].commodity = "Done"; } } for (map::iterator p = deals.begin(); p != deals.end(); p++) { cout << p->first << " " << p->second.lowest << " " << (p->second.summation / p->second.n) << " " << p->second.highest << endl; } cout << "--" << endl; for (map::iterator p = dealers.begin(); p != dealers.end(); p++) { cout << p->first << " " << p->second.paid << " " << p->second.received << endl; } cout << "----------" << endl; } return 0; }