UVa 12266 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 stock prices. You need to know the following things about stock prices:

The ask price is the lowest price at which someone is willing to sell a share of a stock.

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

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

Whenever the bid price is greater than or equal to the ask price, a deal is established. A buy order offering the bid price is matched with a sell order demanding the ask price, and shares are exchanged at the rate of the ask price until either the sell order or the buy order (or both) is fulfilled (i.e., the buyer wants no more stocks, or the seller wants to sell no more stocks). You will be given a list of orders (either buy or sell) and you have to calculate, after each order, the current ask price, bid price and stock price.

Input

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

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

n lines of the form "order_type x shares at y", whereorder_type is either "buy" or "sell", x (1 ≤ x ≤ 1 000) is the number of shares of a stock someone wishes to buy or to sell, and y (1 ≤ y ≤ 1 000) is the desired price.

Output

Per test case:

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

Sample in- and output

2
6
buy 10 shares at 100
sell 1 shares at 120
sell 20 shares at 110
buy 30 shares at 110
sell 10 shares at 99
buy 1 shares at 120
6
sell 10 shares at 100
buy 1 shares at 80
buy 20 shares at 90
sell 30 shares at 90
buy 10 shares at 101
sell 1 shares at 80

学英语:

after the i-th order has been processed and all possible deals have taken place.

在第i个股票订单被处理,并且所有可能的交易都发生后。

思路:用优先队列实现,buy的大的在前,sell的小的在前。

完整代码:

/*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;///大的在前
priority_queue<Sell> sellseq;///小的在前
int lastprice, delta;

void update()
{
    while (!buyseq.empty() && !sellseq.empty() && buyseq.top().price >= sellseq.top().price)///还可以继续交易
    {
        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;
}

作者:csdn博客 synapse7

查看本栏目更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索stock
, ask
, order
, The
, priority_queue
, priority_queue详解
, priority_queue实例
AT
stock prices、uva打印队列、en12266 1、www.712266.com、en12266,以便于您获取更多的相关知识。

时间: 2024-07-29 01:45:16

UVa 12266 Stock Prices (优先队列)的相关文章

Script Caching with PHP

Intended Audience Introduction The Caching Imperative The Script Caching Solution The Caching Script Implementation: Avoiding Common Pitfalls Summary The Script About the Author Intended AudienceThis article is intended for the PHP programmer interes

Ajax Hacks-Hack 5.取得普通字符串

ajax|字符串 Ajax Hacks-Hack 5.取得普通字符串 管理天气信息,股票高价,以及其他不使用XML的时候,就是用到了普通字符串. request 对象有时候不使用XML: request.responseText也可以很好的应用于web应用.本hack讲述用户通过选择一个股票代号,然后从服务器以字符串的格式取得该股票的价格.在下一个hack中取得股票的价格是以数字的格式. 首先看一下页面的HTML代码,其中引入了源文件hack9.js "http://www.w3.org/TR/

Flash 缓存问题的解决

缓存|解决|问题 使用以下的方法,使SWF文件强制不从浏览器读本地的缓存.或强制其SWF文件每次都去读取最新的媒体文件. 确保每次都读取最新的SWF文件. 1:使用"Expires"标头 这是在HTML文件中告诉浏览器不读取本地缓存 在<head> </head> 中间加以下代码<!-- BEGIN INSERT --> <META HTTP-EQUIV="Expires" CONTENT="Mon, 04 Dec

用ASP.NET动态生成图像(转1)

asp.net|动态 Dynamic Image Generation with ASP.NET Scott GuthrieJanuary 14, 2001 Level: Beginner/Intermediate One of the neat features that you can now leverage with .NET is the ability to easily generate dynamic images from code, which you can then ei

jbpm5.1介绍(12)

GWT是什么 如今,编写网络应用程序是一个单调乏味且易于出错的过程.开发人员可能要花费 90% 的时间来处理浏览器行话.此外,构建.重复使用以及维护大量 JavaScript 代码库和 AJAX 组件可能困难且不可靠.Google Web Toolkit (GWT) 允许开发人员使用 Java 编程语言快速构建和维护复杂而又高性能的 JavaScript 前端应用程序,从而降低了开发难度,尤其是与 Eclipse Google 插件结合使用时,优势更明显. google的官方说的很详细 http

R和Python中的文本挖掘:8个入门小贴士

你希望学习文本挖掘,却发现大多数教程难度跨度很大?或者说你找不到心仪的数据集? 本文将会通过 8 个小贴士帮助你走进文本挖掘之门. 对文本保持好奇 在数据科学世界中,凡事的第一步都是"感到好奇",文本挖掘也不例外. 就像 StackOverflow 的数据科学家 David Robinson 在他的博客中说的那样,"当我看到一个假设 [-] 我就迫不及待地想要用数据验证它".你也应该像他那样对文本保持好奇心. David Robinson 看到的假设是: 即使你并不

R 和 Python 中的文本挖掘:8 个入门小贴士

你希望学习文本挖掘,却发现大多数教程难度跨度很大?或者说你找不到心仪的数据集? 本文将会通过 8 个小贴士帮助你走进文本挖掘之门. 对文本保持好奇 在数据科学世界中,凡事的第一步都是"感到好奇",文本挖掘也不例外. 就像 StackOverflow 的数据科学家 David Robinson 在他的博客中说的那样,"当我看到一个假设 [-] 我就迫不及待地想要用数据验证它".你也应该像他那样对文本保持好奇心. David Robinson 看到的假设是: 即使你并不

[usaco]4.3.1 最长递减子序列 和超级整型数

Buy Low, Buy Lower The advice to "buy low" is half the formula to success in the stock market. But to be considered a great investor you must also follow this problems' advice: "Buy low, buy lower" That is, each time you buy a stock, y

AI(OtterTune)引波澜 - AI会洗牌数据库行业吗? DBA如何转变思想

标签 PostgreSQL , 机器学习 , AI , 自动优化 , DBA , 科学计算 背景 最近AI的新闻特别多,席卷了围棋圈之后,成为了技术圈和媒体热捧的话题. 今天又一个产品借AI上头条了 - OtterTune ,一个数据库参数调优的产品,借助机器学习的技术,生成最优的数据库参数. 下面是这个产品的论文 <Automatic Database Management System Tuning Through Large-scale Machine Learning> http://