Smile

Smile

常用链接

统计

最新评论

hunnu 10982 优先队列的使用

这个现场没有写的原因是优先队列不太熟练,并且情况比较多,这里wa了一次是因为初始化错了,逻辑思路还是比较清晰的。

#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;

int const max_n = 1010;
struct node{//buy 从大到小
        int price;
        int num;
        friend bool operator < ( node n1, node n2 ){
               if( n1.price < n2.price )
                   return true;
               if( n1.price == n2.price )
                   if( n1.num > n2.num )
                       return true;
               return false;
        }
        node(int a,int b ):price(a),num(b){}
};
struct node1{//sell 从小到大
        int price;
        int num;
        friend bool operator < ( node1 n1, node1 n2 ){
               if( n1.price > n2.price )
                   return true;
               if( n1.price == n2.price )
                   if( n1.num > n2.num )
                       return true;
               return false;
        }
        node1(int a,int b ):price(a),num(b){}
};
int main(){//ask:high need bid:sell low ldprice:目前成交价格
    int t;
    scanf("%d",&t);
    while( t-- ){
           int n;
           scanf("%d",&n);
           char a[1000],b[1000],c[1000];
           int dprice,dnum;
           int ldprice = 0,ask=0,bid=0;
           priority_queue< node,vector<node> > buy;
           priority_queue< node1,vector<node1> > sell;
           while( n-- ){
                  //ldprice = 0;
                  ask = 0;
                  bid = 0;
                  scanf("%s %d %s %s %d",a,&dnum,&b,&c,&dprice);
                  if( strcmp(a,"buy") == 0 ){
                      if( !sell.empty() )
                          if( dprice < (sell.top()).price ){
                              buy.push(node(dprice,dnum));
                              ask = (buy.top()).price;
                              bid = (sell.top()).price;
                          }
                          else{
                               node1 s1(0,0);
                               while( !sell.empty() && dnum > 0 && (sell.top()).price <=dprice ){
                                      s1 = sell.top();
                                      sell.pop();
                                      if( s1.num<=dnum ){
                                          dnum -= s1.num;
                                          s1.num = 0;
                                      }
                                      else{
                                            s1.num -= dnum;
                                            dnum = 0;
                                      }
                                      ldprice = s1.price;
                               }
                               if( dnum > 0 )
                                   buy.push(node(dprice,dnum));
                               if( s1.num >0 )
                                   sell.push(s1);
                               if( !buy.empty() )
                                   ask = (buy.top()).price;
                               else
                                   ask = 0;
                               if( !sell.empty() )
                                   bid = (sell.top()).price;
                               else
                                   bid = 0;
                               //ldprice = dprice;                                          
                      }
                      else{
                           buy.push(node(dprice,dnum));
                           ask = (buy.top()).price;
                           bid = 0;
                      }
                  }
                  else{
                        if( buy.empty() ){
                            sell.push(node1(dprice,dnum));
                            ask = 0;
                            bid = (sell.top()).price;
                        }
                        else{
                              if( dprice > (buy.top()).price ){
                                  sell.push(node1(dprice,dnum));
                                  ask = (buy.top()).price;
                                  bid = (sell.top()).price;
                              }
                              else{
                                    node b1(0,0);
                                    while( !buy.empty() && dnum > 0 && dprice <= (buy.top()).price){
                                           b1 = buy.top();
                                           buy.pop();
                                           if( b1.num<=dnum ){
                                               dnum -= b1.num;
                                               b1.num = 0;
                                           }
                                           else{
                                                 b1.num -= dnum;
                                                 dnum = 0;
                                           }
                                           //ldprice = b1.price;
                                    }
                               if( dnum > 0 )
                                   sell.push(node1(dprice,dnum));
                               if( b1.num >0 )
                                   buy.push(b1);
                               if( !buy.empty() )
                                   ask = (buy.top()).price;
                               else
                                   ask = 0;
                               if( !sell.empty() )
                                   bid = (sell.top()).price;
                               else
                                   bid = 0;
                               ldprice = dprice;
                               }
                        }
                  }                                                             
           if( bid == 0 )
               printf("- ");
           else
               printf("%d ",bid);
           if( ask == 0 )
               printf("- ",ask);
           else
               printf("%d ",ask);
           if( ldprice == 0 )
               printf("-\n");
           else
               printf("%d\n",ldprice);
           }
    }
    return 0;
}

posted on 2011-08-27 00:48 Smile3 阅读(92) 评论(0)  编辑 收藏 引用 所属分类: 其他题目模板