【CCF CSP-20141203】集合竞价
C++代码
#include<bits/stdc++.h>
using namespace std;
struct Record{
int type;
long long s;
double p;
bool cancel;
Record(int t,long long ss,double pp=0.0,bool c=false):type(t),s(ss),p(pp),cancel(c){}
};
vector<Record>records={Record(-1,0)};
bool cmp(int a,int b){
return records[a].p>records[b].p;
}
int main(){
vector<int>buy,sell;
string str;
double p;
int s;
while(cin>>str){
if(str=="cancel"){
scanf("%d",&s);
records.push_back(Record(2,0));
records[s].cancel=true;
}else{
scanf("%lf%lld",&p,&s);
records.push_back(Record(str=="buy"?0:1,s,p));
}
}
long long buyS=0,sellS=0,ansS=0;
double ansP=0.0;
for(int i=1;i<records.size();++i)
if(records[i].type==0&&!records[i].cancel)
buy.push_back(i);
else if(records[i].type==1&&!records[i].cancel){
sell.push_back(i);
sellS+=records[i].s;
}
sort(buy.begin(),buy.end(),cmp);
sort(sell.begin(),sell.end(),cmp);
for(int i=0,j=0;i<buy.size();++i){
buyS+=records[buy[i]].s;
for(;j<sell.size()&&records[sell[j]].p>records[buy[i]].p;++j)
sellS-=records[sell[j]].s;
if(min(buyS,sellS)>ansS){
ansS=min(buyS,sellS);
ansP=records[buy[i]].p;
}
}
printf("%.2f %lld",ansP,ansS);
return 0;
}