集合竞价

试题名称: 集合竞价
时间限制: 1.0s
内存限制: 256.0MB
问题描述:
问题描述
  某股票交易所请你编写一个程序,根据开盘前客户提交的订单来确定某特定股票的开盘价和开盘成交量。
  该程序的输入由很多行构成,每一行为一条记录,记录可能有以下几种:
  1. buy p s 表示一个购买股票的买单,每手出价为p,购买股数为s。
  2. sell p s 表示一个出售股票的卖单,每手出价为p,出售股数为s。
  3. cancel i表示撤销第i行的记录。
  如果开盘价为p0,则系统可以将所有出价至少为p0的买单和所有出价至多为p0的卖单进行匹配。因此,此时的开盘成交量为出价至少为p0的买单的总股数和所有出价至多为p0的卖单的总股数之间的较小值。
  你的程序需要确定一个开盘价,使得开盘成交量尽可能地大。如果有多个符合条件的开盘价,你的程序应当输出最高的那一个。
输入格式
  输入数据有任意多行,每一行是一条记录。保证输入合法。股数为不超过108的正整数,出价为精确到恰好小数点后两位的正实数,且不超过10000.00。
输出格式
  你需要输出一行,包含两个数,以一个空格分隔。第一个数是开盘价,第二个是此开盘价下的成交量。开盘价需要精确到小数点后恰好两位。
样例输入
buy 9.25 100
buy 8.88 175
sell 9.00 1000
buy 9.00 400
sell 8.92 400
cancel 1
buy 100.00 50
样例输出
9.00 450
评测用例规模与约定

  对于100%的数据,输入的行数不超过5000。


自己编了一个程序,结果一直在等待评测。

上网找别人的代码提交也一样是卡在等待评测上了。

但是提交其它的题目没有问题。

按Ctrl+Z退出输入。

该代码可能存在问题,慎用。

第二天看了一下,通过了,运行了那么久...


long long不能用在VC6.0

[cpp]  view plain copy
  1. #include<iostream>  
  2. #include<string>  
  3. #include<iomanip>  
  4. using namespace std;  
  5.   
  6. class Node{  
  7. public:  
  8.     double price;  
  9.     long long amount;  
  10.     int index;  
  11.     Node* next;  
  12.     Node(){  
  13.         index = 0;  
  14.         price = 0;  
  15.         amount = 0;  
  16.         next = NULL;  
  17.     }  
  18. };  
  19.   
  20. class LinkedList{  
  21. private:  
  22.     Node* head;  
  23.     Node* tail;  
  24.     int size;  
  25. public:  
  26.     LinkedList(){  
  27.         head = NULL;  
  28.         tail = NULL;  
  29.         size = 0;  
  30.     }  
  31.     void insert(double price,long long amount,int index){  
  32.         if(head == NULL){  
  33.             head = new Node();  
  34.             head->price = price;  
  35.             head->amount = amount;  
  36.             head->index = index;  
  37.             tail = head;  
  38.             size++;  
  39.         }  
  40.         else{  
  41.             Node *s = new Node(),*p = head;  
  42.             s->price = price;  
  43.             s->amount = amount;  
  44.             s->index = index;  
  45.             if( price > p->price){  
  46.                 s->next = p;  
  47.                 head = s;  
  48.                 size++;  
  49.                 return;  
  50.             }  
  51.             while(p->next && p->next->price > price){  
  52.                 p = p -> next;  
  53.             }  
  54.             while(p->next && price == p->next->price && p->next->amount > amount){  
  55.                 p = p -> next;  
  56.             }  
  57.             if(p->next){  
  58.                 s -> next = p->next;  
  59.                 p -> next = s;  
  60.                 size++;  
  61.             }  
  62.             else{  
  63.                 p -> next = s;  
  64.                 size++;  
  65.             }  
  66.         }  
  67.     }  
  68.   
  69.     void cancel(int index){  
  70.         Node *p,*s=head;  
  71.         while(s->index!=index && s->next){  
  72.             p = s;  
  73.             s = s->next;  
  74.         }  
  75.         if(s==head){  
  76.             head = head -> next;  
  77.             size--;  
  78.             delete s;  
  79.             return;  
  80.         }  
  81.         if(s->next){  
  82.             p->next = s->next;  
  83.             size--;  
  84.             delete s;  
  85.         }  
  86.         else{  
  87.             p->next = NULL;  
  88.             size--;  
  89.             delete s;  
  90.         }  
  91.     }  
  92.   
  93.     void deleteFirst(){  
  94.         if(head){  
  95.             Node* p = head;  
  96.             head = head->next;  
  97.             size--;  
  98.             delete p;  
  99.         }  
  100.     }  
  101.   
  102.     Node* getFirst(){  
  103.         return head;  
  104.     }  
  105.   
  106.     void display(){  
  107.         Node*p=head;  
  108.         while(p){  
  109.             cout<<p->price<<" "<<p->amount<<endl;  
  110.             p = p -> next;  
  111.         }  
  112.     }  
  113. };  
  114.   
  115. long long a = 0;  
  116. double p = 0.00;  
  117. int cancel[5000] = {0};//0表示无,1表示买,2表示卖,3表示取消  
  118. int length = 0;  
  119.   
  120. void Z(LinkedList& buy,LinkedList& sell){  
  121.     int i = 1;  
  122.     while(buy.getFirst() && sell.getFirst()){  
  123.         if(buy.getFirst()->price >= sell.getFirst()->price){  
  124.             if(buy.getFirst()->amount < sell.getFirst()->amount){  
  125.                 sell.getFirst()->amount -= buy.getFirst()->amount;  
  126.                 a += buy.getFirst()->amount;  
  127.                 p = buy.getFirst()->price;  
  128.                 buy.deleteFirst();  
  129.             }  
  130.             else if(buy.getFirst()->amount > sell.getFirst()->amount){  
  131.                 buy.getFirst()->amount -= sell.getFirst()->amount;  
  132.                 a += sell.getFirst()->amount;  
  133.                 p = buy.getFirst()->price;  
  134.                 sell.deleteFirst();  
  135.             }  
  136.             else{  
  137.                 a += sell.getFirst()->amount;  
  138.                 p = buy.getFirst()->price;  
  139.                 sell.deleteFirst();  
  140.                 buy.deleteFirst();  
  141.             }  
  142.         }//bprice>=s.price  
  143.         else{  
  144.             sell.deleteFirst();  
  145.         }  
  146.     }  
  147.     cout<<setprecision(2)<<fixed<<p<<" "<<a<<endl;  
  148. }  
  149.   
  150. int main(){  
  151.     LinkedList buy;  
  152.     LinkedList sell;  
  153.     string s;  
  154.     int c = -1;  
  155.     long long a;  
  156.     double p;  
  157.     while(cin>>s){  
  158.         if(s=="buy"){  
  159.             c = 0;  
  160.         }  
  161.         else if(s=="sell"){  
  162.             c = 1;  
  163.         }  
  164.         else if(s=="cancel"){  
  165.             c = 2;  
  166.         }  
  167.         else if(s=="EOF"){  
  168.             break;  
  169.         }  
  170.         switch(c){  
  171.         case 0:  
  172.             cin>>p>>a;  
  173.             buy.insert(p,a,length);  
  174.             cancel[length++] = 1;  
  175.             break;  
  176.         case 1:  
  177.             cin>>p>>a;  
  178.             sell.insert(p,a,length);  
  179.             cancel[length++] = 2;  
  180.             break;  
  181.         case 2:  
  182.             cin>>c;  
  183.             cancel[length++] = 3;  
  184.             switch(cancel[c-1]){  
  185.             case 1:  
  186.                 buy.cancel(c-1);  
  187.                 break;  
  188.             case 2:  
  189.                 sell.cancel(c-1);  
  190.                 break;  
  191.             };  
  192.             break;  
  193.         };  
  194.     }  
  195. //  buy.display();  
  196. //  cout<<endl;  
  197. //  sell.display();  
  198.     Z(buy,sell);  
  199. //  system("pause");  
  200.     return 0;  
  201. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值