题解:P6981 [NEERC 2015] Iceberg Orders
ZMQ_Ink65536 · · 题解
题解:P6981 [NEERC 2015] Iceberg Orders
怎么没有题解,我写一篇吧。
:::warning[AI 使用说明]{open}
为更清晰讲述题面和思路,使用 AI 生成了
题意简述
题面看着太难受了,整理份容易阅读的。
你需要模拟一个市场的交易系统。以下是每个订单的信息:
id:订单唯一编号;p:价格;v:总剩余交易量;tv:单次最大显示量;cv:当前可交易量;pr:时间戳优先级。
订单有购入订单和售出订单两类,你需要为每个新到达系统的订单匹配想对应的卖家或买家。当一个新订单
目标搜寻阶段
- 若订单
a 为买单,检索订单簿中所有满足价格P_b \le P_a 的卖单b ;若订单a 为卖单,检索订单簿中所有满足价格P_b \ge P_a 的买单b 。 - 若检索结果非空,优先选择价格最优者:对买单而言最优价格为最低售价,对卖单而言最优价格为最高买价。若存在多个同处于最优价格的订单,则在其中选择时间戳优先级
PR 最小的订单。最终锁定的订单记为对方单b 。 - 若检索结果为空,则宣告匹配落空,跳过后续撮合,直接进入挂单阶段。
撮合交易阶段
- 确定成交价格:成交价格始终等于盘口已存在的对方单价格
P_b 。 - 确定成交数量:本次成交量
V_t 取订单a 的剩余量V_a 与对方单b 的当前可交易量CV_b 二者的较小值。 - 执行扣减:令
V_a 减少V_t ,令对方单的总剩余量V_b 减少V_t ,令对方单的当前可交易量CV_b 减少V_t 。
盘口维护阶段
结算对方单存续:
- 若
V_b 扣减至零,将对方单b 彻底移出订单簿。 - 若
V_b 依然大于零但CV_b 扣减至零,触发冰山浮出判定:重置其当前可交易量CV_b 为V_b 与单次最大显示量TV_b 的较小值;随后重置其优先级PR_b 为全局计数器GP 的当前数值,并将GP 的数值+1 。该订单仍留在订单簿中,但在同价格序列中的优先级降为最末。 - 若
V_b 大于零且CV_b 大于零,对方单b 保持原样留在订单簿中,其优先级PR_b 保持不变。
挂单入簿阶段
若订单
结算与合并阶段
当新订单
:::info[AI 提示]{open}
AI 生成错误内容修改,图 1111;在 ① 和 ③ 之间则啃过了 239 和 1234。
:::
解题思路
依据题意模拟。
我们可以维护两个 set 分别作为买单簿和卖单簿:
sell(卖单簿):按P 升序排列;若P 相同,按时间戳优先级PR 升序排列;若仍相同,按ID 升序排列。buy(买单簿):按P 降序排列;若P 相同,按时间戳优先级PR 升序排列;若仍相同,按ID 升序排列。
每当一个新订单
- 观察对应订单簿的队头,获取最优对手单
b 。 - 判定价格是否交叉(若买则
P_b \le P_a ,若卖则P_b \ge P_a )。若不满足则退出循环,将a 的剩余量作为挂单插入对应簿中。 - 若满足,计算单笔切片成交量
cj = \min(V_a, CV_b) ,累加到以(BUY ID,SELL ID,P) 为键的map中进行流水合并,方便后续输出。 - 将
b 从set中暂时移除,三方扣除交易量:V_a \leftarrow V_a - cj ,V_b \leftarrow V_b - cj ,CV_b \leftarrow CV_b - cj 。 - 维护对方单存续状态:若
V_b = 0 :该订单彻底成交完毕,不再插回。否则若V_b > 0 且CV_b > 0 :说明V_a 已耗尽,该单原样插回set,优先级PR 不变。否则若V_b > 0 且CV_b = 0 :说明露出的水面切片已耗尽,触发冰山补充。更新CV_b = \min(V_b, TV_b) ,赋予最新优先级PR_b = GP ,GP \leftarrow GP + 1 ,重新插入set。
写完交一发:TLE on test 31.。
算法优化
考虑极端情况:盘口挂有一个订单
- 每次循环只能扣减
cj = 1 的交易量。 2。 整个匹配过程需要执行10^9 次erase()和set::insert(),直接超时。 - 同理,若同一最优价格下挂有多个小显示量订单,它们会一直在队头和队尾之间机械轮流转动,反复切片扣减,这也是导致超时的根源。
性质观察
注意到冰山订单在同一价格下的关键性质:
在同一最优价格
P 下,所有被完全吃光CV 并重新补充浮出的订单,其重置后的显示量均为满格的TV ,且由于全局计数器GP 单调递增,它们在队列中的先后顺序是严格固定且周期性循环的。
这意味着,只要进入这一价位且当前所有订单都已处于满额显示状态,后续的交易过程就会形成一个周而复始的环:订单序列
状态记忆与周期检测
我们可以在单次匹配中开一个临时的 vector 记录每个完成重置且满格浮出(
- 当新取出的盘口订单满足:它的
ID 恰好等于vector记录的第一个订单ID ,且其CV = TV 时,说明订单簿刚好完整转完了一圈,成功捕获到了一个封闭的循环节!
整轮快进
一旦检测到完整的循环节,就无需逐次单步模拟,可以直接通过整除计算出能够完整安全重复跑多少圈:
每完整转动一圈,来单消耗总量为
若
- 来单直接作差扣减:
V_a \leftarrow V_a - r \times \text{sumc} 。 - 遍历周期内的所有订单,在
set中查找到对应节点并取出:扣减剩余总量:V_{b_i} \leftarrow V_{b_i} - r \times TV_{b_i} 。 - 批量累加交易流水:该买卖对的成交量直接加上
r \times TV_{b_i} 。 - 若
V_{b_i} > 0 ,更新其显示量CV_{b_i} = \min(V_{b_i}, TV_{b_i}) 并重新插回set;若V_{b_i} = 0 ,则彻底出局不再插回。 - 清空周期记录容器,直接开启下一轮检索。
复杂度
每次触发
-
- 循环节中至少存在一个订单满足
V_{b_i} < TV_{b_i} :该订单在紧随其后的下一轮中必然被彻底清空出局。
全过程中最多发生
参考代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n , id , t , p , v , tv , gp , cj , sumc , r;
struct p_
{
int id , p , v , tv , cv , pr;
inline bool operator < (const p_ &other) const
{
if(p == other.p)
{
if(pr == other.pr)
{
if(id == other.id)
{
if(v == other.v)
{
if(tv == other.tv)
{
return cv < other.cv;
}
return tv < other.tv;
}
return v < other.v;
}
return id < other.id;
}
return pr < other.pr;
}
return p < other.p;
}
inline bool operator > (const p_ &other) const
{
if(p == other.p)
{
if(pr == other.pr)
{
if(id == other.id)
{
if(v == other.v)
{
if(tv == other.tv)
{
return cv > other.cv;
}
return tv > other.tv;
}
return v > other.v;
}
return id > other.id;
}
return pr > other.pr;
}
return p > other.p;
}
}s;
struct p__
{
int id , p , v , tv , cv , pr;
inline bool operator < (const p__ &other) const
{
if(p == other.p)
{
if(pr == other.pr)
{
if(id == other.id)
{
if(v == other.v)
{
if(tv == other.tv)
{
return cv < other.cv;
}
return tv < other.tv;
}
return v < other.v;
}
return id < other.id;
}
return pr < other.pr;
}
return p > other.p;
}
inline bool operator > (const p__ &other) const
{
if(p == other.p)
{
if(pr == other.pr)
{
if(id == other.id)
{
if(v == other.v)
{
if(tv == other.tv)
{
return cv > other.cv;
}
return tv > other.tv;
}
return v > other.v;
}
return id > other.id;
}
return pr > other.pr;
}
return p < other.p;
}
}b;
struct p___
{
int id1 , id2 , p;
inline bool operator < (const p___ &other) const
{
if(id1 == other.id1)
{
if(id2 == other.id2)
{
return p < other.p;
}
return id2 < other.id2;
}
return id1 < other.id1;
}
inline bool operator > (const p___ &other) const
{
if(id1 == other.id1)
{
if(id2 == other.id2)
{
return p > other.p;
}
return id2 > other.id2;
}
return id1 > other.id1;
}
};
struct p____
{
int id , t , p , v , tv , cv , pr;
inline bool operator < (const p____ &other) const
{
if(p == other.p)
{
if(pr == other.pr)
{
if(id == other.id)
{
if(t == other.t)
{
if(v == other.v)
{
if(tv == other.tv)
{
return cv < other.cv;
}
return tv < other.tv;
}
return v < other.v;
}
return t < other.t;
}
return id < other.id;
}
return pr < other.pr;
}
return p < other.p;
}
inline bool operator > (const p____ &other) const
{
if(p == other.p)
{
if(pr == other.pr)
{
if(id == other.id)
{
if(t == other.t)
{
if(v == other.v)
{
if(tv == other.tv)
{
return cv > other.cv;
}
return tv > other.tv;
}
return v > other.v;
}
return t > other.t;
}
return id > other.id;
}
return pr > other.pr;
}
return p > other.p;
}
};
set<p_> sell;
set<p__> buy;
set<p_>::iterator tmps;
set<p__>::iterator tmpb;
map<p___ , int> mp;
set<p____> st;
vector<p_> cycs;
vector<p__> cycb;
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
while(n--)
{
cin >> id >> t >> p >> v >> tv;
if(t == 1)
{
cycs.clear();
sumc = 0;
while(sell.size() && v)
{
tmps = sell.begin();
s = (*tmps);
if(s.p > p)
{
break;
}
if(cycs.size() > 0 && s.id == cycs[0].id && s.cv == s.tv)
{
r = v / sumc;
for(int i = 0 ; i < cycs.size() ; i++)
{
r = min(r , cycs[i].v / cycs[i].tv);
}
if(r > 0)
{
v -= r * sumc;
for(int i = 0 ; i < cycs.size() ; i++)
{
tmps = sell.find(cycs[i]);
if(tmps != sell.end())
{
p_ cur = (*tmps);
sell.erase(tmps);
cur.v -= r * cur.tv;
mp[{id , cur.id , cur.p}] += r * cur.tv;
if(cur.v > 0)
{
cur.cv = min(cur.v , cur.tv);
sell.insert(cur);
}
}
}
cycs.clear();
sumc = 0;
continue;
}
cycs.clear();
sumc = 0;
}
sell.erase(tmps);
cj = min(v , s.cv);
mp[{id , s.id , s.p}] += cj;
v -= cj;
s.v -= cj;
s.cv -= cj;
if(s.v > 0 && s.cv > 0)
{
sell.insert(s);
cycs.clear();
sumc = 0;
}
else if(s.v > 0)
{
s.cv = min(s.v , s.tv);
s.pr = gp;
gp++;
sell.insert(s);
if(s.cv == s.tv)
{
if(cycs.empty())
{
cycs.push_back(s);
sumc = s.tv;
}
else if(s.p == cycs[0].p)
{
cycs.push_back(s);
sumc += s.tv;
}
else
{
cycs.clear();
sumc = 0;
}
}
else
{
cycs.clear();
sumc = 0;
}
}
else
{
cycs.clear();
sumc = 0;
}
}
if(v > 0)
{
buy.insert({id , p , v , tv , min(tv , v) , gp});
gp++;
}
for(map<p___ , int>::iterator it = mp.begin() ; it != mp.end() ; it++)
{
cout << (it -> first).id1 << ' ' << (it -> first).id2 << ' ' << (it -> first).p << ' ' << (it -> second) << '\n';
}
mp.clear();
}
else
{
cycb.clear();
sumc = 0;
while(buy.size() && v)
{
tmpb = buy.begin();
b = (*tmpb);
if(b.p < p)
{
break;
}
if(cycb.size() > 0 && b.id == cycb[0].id && b.cv == b.tv)
{
r = v / sumc;
for(int i = 0 ; i < cycb.size() ; i++)
{
r = min(r , cycb[i].v / cycb[i].tv);
}
if(r > 0)
{
v -= r * sumc;
for(int i = 0 ; i < cycb.size() ; i++)
{
tmpb = buy.find(cycb[i]);
if(tmpb != buy.end())
{
p__ cur = (*tmpb);
buy.erase(tmpb);
cur.v -= r * cur.tv;
mp[{cur.id , id , cur.p}] += r * cur.tv;
if(cur.v > 0)
{
cur.cv = min(cur.v , cur.tv);
buy.insert(cur);
}
}
}
cycb.clear();
sumc = 0;
continue;
}
cycb.clear();
sumc = 0;
}
buy.erase(tmpb);
cj = min(v , b.cv);
mp[{b.id , id , b.p}] += cj;
v -= cj;
b.v -= cj;
b.cv -= cj;
if(b.v > 0 && b.cv > 0)
{
buy.insert(b);
cycb.clear();
sumc = 0;
}
else if(b.v > 0)
{
b.cv = min(b.v , b.tv);
b.pr = gp;
gp++;
buy.insert(b);
if(b.cv == b.tv)
{
if(cycb.empty())
{
cycb.push_back(b);
sumc = b.tv;
}
else if(b.p == cycb[0].p)
{
cycb.push_back(b);
sumc += b.tv;
}
else
{
cycb.clear();
sumc = 0;
}
}
else
{
cycb.clear();
sumc = 0;
}
}
else
{
cycb.clear();
sumc = 0;
}
}
if(v > 0)
{
sell.insert({id , p , v , tv , min(tv , v) , gp});
gp++;
}
for(map<p___ , int>::iterator it = mp.begin() ; it != mp.end() ; it++)
{
cout << (it -> first).id1 << ' ' << (it -> first).id2 << ' ' << (it -> first).p << ' ' << (it -> second) << '\n';
}
mp.clear();
}
}
for(tmps = sell.begin() ; tmps != sell.end() ; tmps++)
{
st.insert({(*tmps).id , 2 , (*tmps).p , (*tmps).v , (*tmps).tv , (*tmps).cv , (*tmps).pr});
}
for(tmpb = buy.begin() ; tmpb != buy.end() ; tmpb++)
{
st.insert({(*tmpb).id , 1 , (*tmpb).p , (*tmpb).v , (*tmpb).tv , (*tmpb).cv , (*tmpb).pr});
}
cout << '\n';
for(set<p____>::iterator it = st.begin() ; it != st.end() ; it++)
{
cout << (*it).id << ' ' << (*it).t << ' ' << (*it).p << ' ' << (*it).v << ' ' << (*it).tv << ' ' << (*it).cv << '\n';
}
return 0;
}