集合竞价

#include<cstdio>
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<vector> 
#include<string>
#include<queue>
#include<map>
using namespace std;
struct Node
{
    int type;     //0表示buy
    double p;
    int s; 
}node[5005],buy[5005],sell[5005];
bool BUY(Node x,Node y)    //价格越高优先级越高,价格一样,股数越高越高 
{
    if(x.p != y.p)
        return x.p>y.p;
    else
        return x.s>y.s; 
}
bool SELL(Node x,Node y)    //价格越低优先级越高,价格一样,股数越高越高 
{
    if(x.p != y.p)
        return x.p<y.p;
    else
        return x.s>y.s; 
}
int min(int x,int y)
{
    if(x<y)    return x;
    else    return y;
}
int main()
{
    string str;
    double p;
    int s,pos;
    int h=0;
    while(cin>>str)
    {
        if(str == "buy")    
        {
            cin>>p>>s;
            node[h].type = 0;
            node[h].p = p;
            node[h].s = s;
            h++;
        }
        else if(str == "sell")    
        {
            cin>>p>>s;
            node[h].type = 1;
            node[h].p = p;
            node[h].s = s;
            h++;
        }
        else
        {
            cin>>pos;
            for(int i=pos;i<h;i++)
                node[i-1] = node[i];
            h--; 
        }
    }
    int h1=0,h2=0;
    for(int i=0;i<h;i++)
    {
        if(node[i].type == 0)
        {
            buy[h1]= node[i];
            h1++;
        }
        else
        {
            sell[h2] = node[i];
            h2++;
        }
    }
    sort(buy,buy+h1,BUY);
    sort(sell,sell+h2,SELL);
    double price=-100;
    int i=0,j=0,k=0,maxs=-100;
    //如果有多个符合条件的开盘价,你的程序应当输出最高的那一个。
    for(k=0;k<h1;k++)
    {
        double p0 = buy[k].p;
        int buy_sum=0,sell_sum=0;
        for(i=0;i<h1;i++)
        {
            if(buy[i].p>=p0)
                buy_sum+=buy[i].s;
            else
                break;
        }            
        for(i=0;i<h2;i++)
        {
            if(sell[i].p<=p0)
                sell_sum+=sell[i].s;
            else
                break;
        }
        int num = min(buy_sum,sell_sum);
        if(num>maxs)
        {
            maxs = num;
            price = p0;    //满足成交量最大情况下,最大的sell 
        }
        else if(num == maxs && p0>price)
            price = p0;
    }
    for(k=0;k<h2;k++)
    {
        double p0 = sell[k].p;
        int buy_sum=0,sell_sum=0;
        for(i=0;i<h1;i++)
        {
            if(buy[i].p>=p0)
                buy_sum+=buy[i].s;
            else
                break;
        }            
        for(i=0;i<h2;i++)
        {
            if(sell[i].p<=p0)
                sell_sum+=sell[i].s;
            else
                break;
        }
        int num = min(buy_sum,sell_sum);
        if(num>maxs)
        {
            maxs = num;
            price = p0;    //满足成交量最大情况下,最大的sell 
        }
        else if(num == maxs && p0>price)
            price = p0;
    }
    cout<<setiosflags(ios::fixed)<<setprecision(2);
    cout<<price;
    printf(" %d",maxs);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/fzuhyj/p/10515504.html