UVa 12266 Stock Prices (优先队列)
来源:互联网 发布:php支付宝服务端demo 编辑:程序博客网 时间:2024/06/18 04:42
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.
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
InputOutput26buy 10 shares at 100sell 1 shares at 120sell 20 shares at 110buy 30 shares at 110sell 10 shares at 99buy 1 shares at 1206sell 10 shares at 100buy 1 shares at 80buy 20 shares at 90sell 30 shares at 90buy 10 shares at 101sell 1 shares at 80
- 100 -120 100 -110 100 -120 110 110120 100 99- 100 120100 - -100 80 -100 90 -90 80 90100 80 90100 - 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;}
- UVa 12266 Stock Prices (优先队列)
- UVA 12266 Stock prices --优先队列
- NWERC2010 NKOJ2178 Stock Prices
- HDU 4163 Stock Prices
- UVA - 11997(优先队列)
- UVa 10050 Hartals(优先队列)
- UVA 1203 Argus(优先队列)
- uva 1203 - Argus(优先队列)
- UVA FILL(BFS + 优先队列)
- UVA 1203Argus【优先队列】
- UVa 10603 BFS+优先队列
- UVA 1203 Argus(优先队列)
- UVA-10954 贪心+优先队列
- UVA 12100 Printer Queue(队列,优先队列)
- UVA 540 and UVA 136 队列及优先队列
- UVa 10047 - The Monocycle, 优先队列+BFS
- UVa 10954 Add All ( 优先队列 )
- UVa 10954 - Add All 贪心+优先队列
- 应用Yii1.1和PHP5进行敏捷Web开发13
- [OpenWrt] openwrt的一些琐事
- 应用Yii1.1和PHP5进行敏捷Web开发14
- live555源代码研究——RTSP服务器端建立及监听
- Cleanup总结---Thinking in java 英文版部分小结
- UVa 12266 Stock Prices (优先队列)
- Oracle中存储过程和函数的比较
- 【eclipse设置】Eclipse使用入门教程
- dos下实现延迟功能
- Activity跳转与传值
- setsocketopt设置socket应用
- 排序算法
- vs2008 无法设置断点&设置内存断点的方法
- 数据对齐问题