省赛选拔,三人组队赛,队友之间的配合很重要,对于英文题目,精读题,否则对题意理解的偏差可能会导致出乎意料的损失。
那么,就来回忆一下这道题。
Title:
H. Stock Prices
Time Limit:
2000Ms
Content:
In this problem we deal with the calculation of stock prices. You need to know the following things about stock prices:
1.The ask price is the lowest price at which someone is willing to sell a share of a stock.
2.The bid price is the highest price at which someone is willing to buy a share of a stock.
3.The stock price is the price at which the last deal was established.
Whenever the bid price is greater than or equal to theask price, a deal is established. A buy order offering thebid price is matched with a sell order demanding theask 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 currentask price,bid price andstock price.
Input:
The first line gives the number of test cases, at most 100. For each test case:
1.One line with an integer n(1<=n<=1000):the number of orders.
2.n lines of the form "order_type x shares at y" whereorder_type is either "buy" or "sell",x(1<=x<=1000) is the number of shares of a stock someone wishes to buy or to sell, andy(1<=y<=1000) is the desired price.
Output:
For each test cases, output n lines, each of the form "ai bi si", where ai, bi,si are the currentask,bid andstock prices, respectively, after thei-th order has been processed and all possible deals have taken place. Wherever a price is not defined, output "-" instead of the price.
Sample Input:
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
Sample Output:
- 100 -
120 100 -
110 100 -
120 110 110
120 100 99
- 100 120
100 - -
100 80 -
100 90 -
90 80 90
100 80 90
100 - 80
分析:这就是一道典型的模拟题,分析清楚股票的交易过程,然后一步步实现就可以了。那就需要我们注意一下题目中的几个问题
1.对于ask price和bid price, 题目中明确说明了是最低/高的价格,这就说明交易不是按顺序进行的,而是需要排序挑选,因此,需要用优先队列,这个细节又会引出第六个问题;
2.对于stock price, 题目中last可理解为是上次成交的价格,这就说明,如果本次交易不成功,那stock price应该取上一个成功交易的stock price,而非“-”。除非至今还未出现成功交易,否则不会输出“-”。对此,可以考虑用一个单独的变量存储price stock,时时维护与更新,并初始化为-1以标记未出现成功交易的情况;
3.对于交易成功的结果,经分析可以理解为普通的买卖交易情况,就是少的一方买入或卖出全部股票,并且以卖方的价格出售;
4.对于交易结束的条件,确实耐人寻味,最终将重点研究对象放在了当前喊价格的这个人,即当这个人无法再进行交易时本次交易结束,否则,他可以连续和多个人进行交易。
5.基于问题四,我们可以得出每次交易结束后,所剩要价中不存在能进行交易的组合(否则在执行该要价的过程中,该组合就会被匹配)。因此,每次只有关注该次新的要价即可,不需对内部原有要价再次研究。
6.与问题五形成对比,每次研究的都是新增要价,然而,新增要价并非一定是最低/高的,即可能该次新增要价根本满足不了(意思就是说可能进入优先队列后,再出来就并不是这个这个对象了),因此处理的价格和最终输出的价格可能并不一样。所以,就需要对这个要价必须先处理,后队列,再取出,最后输出,步骤不能少,也不能转换顺序,否则就会出现不一致的情况。
/*
Author:Owen_Q
*/
#include <bits/stdc++.h>
using namespace std;
typedef struct STOCK
{
int shares;
int price;
bool operator > (const struct STOCK &s) const
{
return price > s.price;
}
bool operator < (const struct STOCK &s) const
{
return price < s.price;
}
}Stock;
priority_queue <Stock,vector<Stock> > buy;
priority_queue <Stock,vector<Stock>, greater<Stock> > sell;
int main()
{
//freopen("H.in","r",stdin);
//freopen("re.out","w",stdout);
int m,n;
scanf("%d",&m);
while(m--)
{
while(!buy.empty())
{
buy.pop();
}
while(!sell.empty())
{
sell.pop();
}
scanf("%d",&n);
int lastprice = -1;
for(int i=0;i<n;i++)
{
Stock tempstock;
char choice[10],useless[10];
scanf("%s%d%s%s%d",choice,&tempstock.shares,useless,useless,&tempstock.price);
if(strcmp(choice,"buy")==0)
{
if(sell.empty())
{
buy.push(tempstock);
printf("- %d ",buy.top().price);
if(lastprice == -1)
{
printf("-\n");
}
else
{
printf("%d\n",lastprice);
}
}
else
{
Stock laststock;
laststock.price = sell.top().price;
laststock.shares = sell.top().shares;
bool in = false;
while(laststock.price<=tempstock.price)
{
lastprice = laststock.price;
sell.pop();
if(laststock.shares>tempstock.shares)
{
laststock.shares -= tempstock.shares;
sell.push(laststock);
in = true;
break;
}
else if(laststock.shares<tempstock.shares)
{
tempstock.shares -= laststock.shares;
if(sell.empty())
{
break;
}
else
{
laststock.price = sell.top().price;
laststock.shares = sell.top().shares;
}
}
else
{
in = true;
break;
}
}
if(!in)
{
buy.push(tempstock);
}
if(sell.empty())
{
printf("- ");
}
else
{
printf("%d ",sell.top().price);
}
if(buy.empty())
{
printf("- ");
}
else
{
printf("%d ",buy.top().price);
}
if(lastprice == -1)
{
printf("-\n");
}
else
{
printf("%d\n",lastprice);
}
}
}
else
{
if(buy.empty())
{
sell.push(tempstock);
printf("%d - ",sell.top().price);
if(lastprice == -1)
{
printf("-\n");
}
else
{
printf("%d\n",lastprice);
}
}
else
{
Stock laststock;
laststock.price = buy.top().price;
laststock.shares = buy.top().shares;
bool in = false;
while(laststock.price>=tempstock.price)
{
lastprice = tempstock.price;
buy.pop();
if(laststock.shares>tempstock.shares)
{
laststock.shares -= tempstock.shares;
buy.push(laststock);
in = true;
break;
}
else if(laststock.shares<tempstock.shares)
{
tempstock.shares -= laststock.shares;
if(buy.empty())
{
break;
}
else
{
laststock.price = buy.top().price;
laststock.shares = buy.top().shares;
}
}
else
{
in = true;
break;
}
}
if(!in)
{
sell.push(tempstock);
}
if(sell.empty())
{
printf("- ");
}
else
{
printf("%d ",sell.top().price);
}
if(buy.empty())
{
printf("- ");
}
else
{
printf("%d ",buy.top().price);
}
if(lastprice == -1)
{
printf("-\n");
}
else
{
printf("%d\n",lastprice);
}
}
}
}
}
return 0;
}
可见,读题的过程必须耐心,仔细。如果题意读错,就可能花费大量时间做无用功,以此题为例,细节决定成败。
科普:命令行的fc命令可以比较两个文件的不同之处,确实也是一个不小的收获

本文解析了一道关于股票交易过程的模拟题,详细讨论了如何使用优先队列处理买卖订单,确保正确计算ask price、bid price及stock price,并提供了一个完整的C++实现示例。
2178

被折叠的 条评论
为什么被折叠?



