UVa 12266 stock Prices (priority queue)

Source: Internet
Author: User
Tags bool printf time limit stock prices


12266-stock Prices



Time limit:3.000 seconds



http://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=3418



In this problem we deal with the calculation of the stock prices. You are need to know the following things about-stock prices:



The ask price was the lowest price at which someone are willing to sell a share of a.



The bid price are the highest price at which someone are willing to buy a share of a.



The stock price is the price at which's last deal was established.



Whenever the bid price are greater than or equal to "ask", a deal is established. A buy-order offering the bid and matched with a sell order demanding the ask price, and shares are exchanged at the R Ate of the ask price until either the sell and the buy order (or both) are fulfilled (i.e., buyer wants no more St Ocks, or the seller wants to sell no more stocks). You'll be given a list of orders (either buy or sell) and your have to calculate, after the , bid price and the stock price.



Input



On the "the" a positive integer:the number of test cases, at most 100. After that, the test case:



One line with an integer n (1≤n≤1): The number of orders.



n lines of the form "Order_type x shares at Y", Whereorder_type are either "buy" or "sell", and X (1≤x≤1) is the number of shares of a-someone wishes to-buy or to sell, and Y (1≤y≤1) are the desired price.



Output



Per test case:



n lines, each of the form "Ai bi si", where ai, bi and Si are, bid and stock prices, respectively, after t He i-th the order has been processed and all possible deals the have place. Whenever a is not defined, output "-" instead to the price.



Sample In-and Output


2
6
shares at
sell 1 shares at
sell shares
at the shares at a Hares at a
1 shares at
6
sell shares at
1 shares
at the shares at Sell shares at shares at
sell 1 shares at 80


Learn English:



After the i-th order has been processed and all possible deals to have place.



In the first stock order is processed, and all possible transactions occur after.



Train of thought: Implement with priority queue, buy big in front, sell small in front.



Complete code:


/*0.025s*/
  
#include <bits / stdc ++. h>
using namespace std;
  
char buff [10];
struct Buy
{
    int price, num;
    bool operator <(const Buy & a) const
    {
        return price <a.price;
    }
} b;
struct Sell
{
    int price, num;
    bool operator <(const Sell & a) const
    {
        return price> a.price;
    }
} s;
priority_queue <Buy> buyseq; /// big first
priority_queue <Sell> sellseq; // small first
int lastprice, delta;
  
void update ()
{
    while (! buyseq.empty () &&! sellseq.empty () && buyseq.top (). price> = sellseq.top (). price) /// can continue trading
    {
        b = buyseq.top (), s = sellseq.top ();
        buyseq.pop (), sellseq.pop ();
        lastprice = s.price;
        delta = min (b.num, s.num);
        b.num-= delta, s.num-= delta;
        if (b.num> 0) buyseq.push (b);
        if (s.num> 0) sellseq.push (s);
    }
}
  
void print ()
{
    if (sellseq.empty ()) printf ("-");
    else printf ("% d", sellseq.top (). price);
    if (buyseq.empty ()) printf ("-");
    else printf ("% d", buyseq.top (). price);
    if (lastprice == -1) printf ("-\ n");
    else printf ("% d \ n", lastprice);
}
  
int main ()
{
    int T, n, i, num, price;
    scanf ("% d", & T);
    while (T--)
    {
        while (! buyseq.empty ()) buyseq.pop ();
        while (! sellseq.empty ()) sellseq.pop ();
        lastprice = -1;
        scanf ("% d", & n);
        for (i = 1; i <= n; ++ i)
        {
            scanf ("% s% d shares at% d", buff, & num, & price);
            if (buff [0] == 'b') buyseq.push ((Buy) {price, num});
            else sellseq.push ((Sell) {price, num});
            update ();
            print ();
        }
    }
    return 0;
} 


Author: csdn Blog Synapse7



See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.