1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<sstream>
using namespace std;
struct Bid{
int bidID;
int client;
int price;
int share;
};
struct max_h{
bool operator()(const Bid& a,const Bid& b) const{ //construct max heap with price as key
return a.price<b.price;
}
};
struct min_h{
bool operator()(const Bid& a,const Bid& b) const{ //construct min heap with price as key
return a.price>b.price;
}
};
void print_heap(vector<Bid>& heap){ //checking heap content
for(int i=0; i<heap.size(); ++i){
cout<< heap.at(i).client << " ";
}
return;
}
void print_trans(vector<Bid>& buyer, vector<Bid>& seller, int& transactionID){
if(buyer.empty() || seller.empty())
return;
if(buyer.at(0).price < seller.at(0).price)
return;
if(buyer.at(0).share > seller.at(0).share){
cout << transactionID << "\t" << buyer.at(0).client << "\t" << seller.at(0).client << "\t" << seller.at(0).price << "\t" << seller.at(0).share << endl;
buyer.at(0).share -= seller.at(0).share;
transactionID++;
pop_heap(seller.begin(), seller.end(), min_h());
seller.pop_back();
print_trans(buyer, seller, transactionID);
}
else if(buyer.at(0).share < seller.at(0).share){
cout << transactionID << "\t" << buyer.at(0).client << "\t" << seller.at(0).client << "\t" << seller.at(0).price << "\t" << buyer.at(0).share << endl;
seller.at(0).share -= buyer.at(0).share;
transactionID++;
pop_heap(buyer.begin(), buyer.end(), max_h());
buyer.pop_back();
print_trans(buyer, seller, transactionID);
}
else{
cout << transactionID << "\t" << buyer.at(0).client << "\t" << seller.at(0).client << "\t" << seller.at(0).price << "\t" << seller.at(0).share << endl;
transactionID++;
pop_heap(seller.begin(), seller.end(), min_h());
seller.pop_back();
pop_heap(buyer.begin(), buyer.end(), max_h());
buyer.pop_back();
print_trans(buyer, seller, transactionID);
}
return;
}
main(){
vector<Bid> buyer;
vector<Bid> seller;
string line;
int index, client, action, price, share_count;
int transactionID=0;
while(getline(cin, line)){
stringstream get(line);
get >> index >> client >> action >> price >> share_count;
Bid A;
A.bidID = index;
A.client = client;
A.price = price;
A.share = share_count;
if(action == 0){
buyer.push_back(A);
make_heap(buyer.begin(), buyer.end(), max_h());
if(!seller.empty()){
if(buyer.at(0).price >= seller.at(0).price){ //check for available transaction. If there is, print.
print_trans(buyer, seller, transactionID);
}
}
}
else if(action == 1){
seller.push_back(A);
make_heap(seller.begin(), seller.end(), min_h());
if(!buyer.empty()){
if(buyer.at(0).price >= seller.at(0).price){ //check for available transaction. If there is, print.
print_trans(buyer, seller, transactionID);
}
}
}
else if(action == 2){
for(int i=0; i<buyer.size(); ++i){
if(buyer.at(i).bidID == index){
buyer.erase(buyer.begin()+i);
make_heap(buyer.begin(), buyer.end(), max_h());
}
}
for(int i=0; i<seller.size(); ++i){
if(seller.at(i).bidID == index){
seller.erase(seller.begin()+i);
make_heap(seller.begin(), seller.end(), min_h());
}
}
}
}
}
|