Problem 1225 : e-market
Problem B: e-Market
http://rose.u-aizu.ac.jp/onlinejudge/ProblemSet/description.jsp?id=1225
- 問題概要
売買したい商品の名前、その人の名前、買うか売るか、何円で売買したいか、という情報が与えられる。
すでに、買いたい値段 <= 売りたい値段 の情報があるならそこと売買を結びつける。
おなじ値段の売買情報があれば、先に受け付けた方を優先する。
ない場合はストックする。
売買が成立するとき、それは買いたい値段と売りたい値段の平均。
商品それぞれの平均値段、最高値段、最低値段、
ディーラーそれぞれの買った金額売った金額を出力せよ。
- 解法
書くだけ。
- ソース
#include<iostream> #include<string> #include<algorithm> #include<vector> #include<map> #include<set> #include<cstdlib> #include<cstdio> #include<cmath> #include<sstream> #include<cassert> #include<queue> #include<stack> #define REP(i,b,n) for(int i=b;i<n;i++) #define rep(i,n) REP(i,0,n) #define ALL(C) (C).begin(),(C).end() #define pb push_back #define mp make_pair #define vint vector<int> #define FOR(it,o) for(__typeof((o).begin()) it=(o).begin(); it!=(o).end(); ++it) #define lli long long int #define ld long double using namespace std; class Order{ public: int ID, price; string man, name, buy; bool used; Order(){ used = false; } bool operator < (const Order &o)const{ if(buy == "BUY"){ if(price != o.price){ return price > o.price; } else return ID < o.ID; } else { if(price != o.price){ return price < o.price; } else return ID < o.ID; } } }; main(){ int n; while(cin >> n){ if(n == 0)break; vector<Order> BUY, SELL; map<string, vint> item; map<string, int> sum; map<string, pair<int, int> > man; rep(i, n){ Order o; cin >> o.man >> o.buy >> o.name >> o.price; man[o.man].first = max(0, man[o.man].first); o.ID = i; bool found = false; if(o.buy == "BUY"){ if(SELL.size() != 0){ sort(ALL(SELL)); FOR(it, SELL){ if((*it).used)continue; if((*it).name != o.name)continue; if((*it).man == o.man)continue; if((*it).price > o.price)break; found = true; int tmp = (o.price + (*it).price)/2; item[o.name].pb(tmp); sum[o.name] += tmp; man[o.man].first += tmp; man[(*it).man].second += tmp; (*it).used = true; break; } } if(found)continue; BUY.pb(o); } else { if(BUY.size() != 0){ sort(ALL(BUY)); FOR(it, BUY){ if((*it).used)continue; if((*it).name != o.name)continue; if((*it).man == o.man)continue; if((*it).price < o.price)break; found = true; int tmp = (o.price + (*it).price)/2; item[o.name].pb(tmp); man[o.man].second += tmp; sum[o.name] += tmp; man[(*it).man].first += tmp; (*it).used = true; break; } } if(found)continue; SELL.pb(o); } } if(item.size() != 0){ FOR(it, item){ vint v = (*it).second; cout << (*it).first <<' '<< *(min_element(ALL(v))) << ' ' << sum[(*it).first] / v.size() << ' '<< *(max_element(ALL(v))) << endl; } } cout <<"--"<<endl; if(man.size() != 0){ FOR(it, man){ cout << (*it).first << ' ' << (*it).second.first << ' ' << (*it).second.second << endl; } } cout << "----------"<< endl; } }
else return ID < o.ID のところを else ID < o.IDと書いていて、2,3時間無駄にした。
vectorのerase使うと、いつもランタイムエラーになるのはなぜだろう。