先把 $b_i$ 取负,问题转化为了归并 $p_1,\dots,p_n$ 和 $q_1,\ldots ,q_n$,使得 $p$, $q$ 间的顺序对的个数恰好为 $k$。由于当 $p$ 全在 $q$ 前面时答案最大,反之则答案最小,而交换相邻两个数至多让答案改变 $1$。因此一定有解。只需在保证 $k$ 始终非负的情况下先放 $p$ 即可。用树状数组维护 $p_i$ 与后面的顺序对数。时间复杂度 $O(n\log n)$。
QOJ.ac
QOJ
As we are currently experiencing an overwhelming number of web requests for fetching user submissions, we have temporarily disabled the full submissions list. You must now be logged in to view submissions.
Discussion #348 for Problem #12213. Cool pairs
Type: Editorial
Status: Open
Posted by: jiangly
Posted at: 2025-12-14 07:14:38
Last updated: 2025-12-14 07:14:42
题解
Comments
No comments yet.