#P9306. 「DTOI-5」进行一个排的重 (Minimum Version)

    ID: 9621 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 5 上传者: 标签>2023洛谷原创O2优化排列组合构造

「DTOI-5」进行一个排的重 (Minimum Version)

Background

The difference between this problem and the Maximum Version is the extremum being asked for and the constraints.

Little L is keen on reordering sequences to make them look neat.

Problem Description

Little L has a sequence aa of length nn, where each item aia_i is a pair (pi,qi)(p_i, q_i).

To make aa look neater, he specifies that pp and qq are both permutations of length nn.

To measure how neat aa is, he defines a weight function $f(a) = \displaystyle\sum_{i = 1}^n ([p_i > \max_{j = 1}^{i - 1} p_j] + [q_i > \max_{j = 1}^{i - 1} q_j])$. Note that when i=1i = 1, both bracketed terms can take a value, because we consider $\displaystyle\max_{j = 1}^0 p_j = \displaystyle\max_{j = 1}^0 q_j = -\infty$.

To make aa look even neater, he decides to reorder aa in some way to obtain aa' such that f(a)f(a') is minimized. Note that during reordering, you must treat ai=(pi,qi)a'_i = (p'_i, q'_i) as an indivisible whole.

He wants you to find the value of f(a)minf(a')_{\min}, and how many different aa' can achieve f(a)minf(a')_{\min}.

Since the number of ways may be very large, you only need to output the result modulo 998244353998244353.

Input Format

The first line contains an integer nn.

The second line contains nn integers p1,p2,,pnp_1, p_2, \cdots, p_n.

The third line contains nn integers q1,q2,,qnq_1, q_2, \cdots, q_n.

Output Format

Output one line with two integers, representing the required values.

5
1 5 2 4 3
1 4 2 5 3
3 48

Hint

Constraints

$$\def\or{\operatorname{or}} %\def\arrayscretch{1.5} \def\arraystretch{1.5} \begin{array}{|c|c|c|}\hline \textbf{Subtask}&n\le &\textbf{Points}\cr\hline \sf1&10&10 \operatorname{pts}\cr\hline \sf2&500&20 \operatorname{pts}\cr\hline \sf3&5\times10^3&20 \operatorname{pts}\cr\hline \sf4&10^5&20 \operatorname{pts}\cr\hline \sf5&5\times10^5&30 \operatorname{pts}\cr\hline \end{array}$$

For 100%100\% of the testdata, 1n5×1051 \leq n \leq 5 \times 10^5, 1pi,qin1 \leq p_i, q_i \leq n, and it is guaranteed that pp and qq are both permutations.

Translated by ChatGPT 5