题意:
求出成交量最大的开盘价,如果有多个,取开盘价最大的
题解:
注意输入方式(用string),理解题意
代码如下:
#include<bits/stdc++.h>
using namespace std;
const int maxn=5e3+5;
typedef long long ll;
struct node{
double price;
int quantity,flag;
bool operator<(const node &rs)const{
return price<rs.price;
}
}A[maxn],sell[maxn],buy[maxn];
set<double>as;
set<double>::iterator it;
int main(){
int i,j,id,cnt,cnt1,cnt2;
double tmpPrice,ansPri;
ll tmp,sellSum,buySum,ansQua;
string s;
node now;
as.clear();ansPri=0;ansQua=-1;cnt=cnt1=cnt2=0;
while(cin>>s){
if(s=="buy"){
cin>>now.price>>now.quantity;now.flag=2;
}
else if(s=="sell"){
cin>>now.price>>now.quantity;now.flag=1;
}
else if(s=="cancel"){
cin>>id;now.flag=0;A[id].flag=0;
}
///else break;
A[++cnt]=now;
}
for(i=1;i<=cnt;i++){
if(!A[i].flag)continue;
if(A[i].flag==1)sell[++cnt1]=A[i];
else buy[++cnt2]=A[i];
as.insert(A[i].price);
}
sort(sell+1,sell+1+cnt1);
sort(buy+1,buy+1+cnt2);
sellSum=buySum=0;
for(i=1;i<=cnt2;i++)buySum+=buy[i].quantity;
it=as.begin();i=j=1;
while(it!=as.end()){
tmpPrice=(*it);
while(i<=cnt1&&(sell[i].price<=tmpPrice))sellSum+=sell[i].quantity,i++;
while(j<=cnt2&&(buy[j].price<tmpPrice))buySum-=buy[j].quantity,j++;
tmp=min(sellSum,buySum);
if(ansQua<=tmp)ansQua=tmp,ansPri=tmpPrice;
it++;
}
printf("%.2f %lld\n",ansPri,ansQua);
return 0;
}