代码如下:
1 //by NKU lkjslkjdlk
2 #include<cstdio>
3 #include<cstring>
4 #include<set>
5 using namespace std;
6 inline int min(int x,int y){return x<y?x:y;}
7 int cas,or_num,sto_pri,i,bid[1005],ask[1005],trade_num,trade_pri,min_de;
8 char order[100],tmp[10],bs[5],ch;
9 set<int> bids,asks;set<int>::reverse_iterator biter;set<int>::iterator aiter;
10 void print(){
11 if(asks.empty())printf("- ");else printf("%d ",*asks.begin());
12 if(bids.empty()) printf("- ");else printf("%d ",*bids.rbegin());
13 if(sto_pri==-1) printf("-\n");else printf("%d\n",sto_pri);
14 }
15 void sell(){
16 while(1){
17 biter=bids.rbegin();aiter=asks.begin();
18 if(*biter>=*aiter){
19 min_de=min(bid[*biter],ask[*aiter]);
20 bid[*biter]-=min_de;ask[*aiter]-=min_de;
21 sto_pri=*aiter;
22 if(bid[*biter]==0) bids.erase(*biter);
23 if(ask[*aiter]==0) asks.erase(*aiter);
24 }
25 if(asks.empty()||bids.empty()) break;
26 if(*asks.begin()>*bids.rbegin()) break;
27 }
28 }
29 int main()
30 {
31 scanf("%d",&cas);
32 while(cas--){
33 bids.clear();asks.clear();
34 memset(bid,0,sizeof(bid));memset(ask,0,sizeof(ask));
35 scanf("%d%c",&or_num,&ch);
36 for(i=0,sto_pri=-1;i<or_num;i++){
37 gets(order);sscanf(order,"%s %d %s %s %d",bs,&trade_num,tmp,tmp,&trade_pri);
38 if(!strcmp(bs,"buy")){
39 bids.insert(trade_pri);bid[trade_pri]+=trade_num;
40 if(!asks.empty()){sell();print();}else print();
41 }
42 else{
43 asks.insert(trade_pri);ask[trade_pri]+=trade_num;
44 if(!bids.empty()){sell();print();}else print();
45 }
46 }
47 }
48 return 0;
49 }