CCF-201412-3-集合竞价

题意:
求出成交量最大的开盘价,如果有多个,取开盘价最大的
题解:
注意输入方式(用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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值