CF2190G.Maximize Determinant

NOI/NOI+/CTSC

通过率:0%

时间限制:4.00s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

An n×nn \times n matrix BB is called an interval matrix if for each row ii, there exists a contiguous range of columns [li,ri][l_i, r_i] (1≤li≤ri≤n1 \le l_i \le r_i \le n) filled with ones, while all other elements in the row are zero. Formally, Bi,j=1B_{i, j} = 1 if li≤j≤ril_i \le j \le r_i, and Bi,j=0B_{i, j} = 0 otherwise.

You are given an integer nn and an initial interval matrix AA, described by nn triples (li,ri,ai)(l_i, r_i, a_i). The ii-th row of AA corresponds to the interval [li,ri][l_i, r_i], and aia_i is the cost associated with modifying this row.

Let S\mathcal{S} be the set of all possible n×nn \times n interval matrices. We define XX as the maximum possible determinant among them: $$ X = \max\limits_{B \in \mathcal{S}} \operatorname{det}(B). $$ (Note that ∣S∣=(n(n+1)2)n|\mathcal{S}| = \left(\frac{n(n + 1)}{2}\right)^n, as each row can be any valid interval)

Your goal is to transform AA into a matrix A′A' such that det⁡(A′)=X\operatorname{det}(A') = X. To achieve this, you can perform the following operation any number of times:

  • Choose a row index ii (1≤i≤n1 \le i \le n) and a new valid interval [L,R][L, R] (1≤L≤R≤n1 \le L \le R \le n).
  • Replace the current interval of the ii-th row with [L,R][L, R].
  • The cost of this operation is aia_i.

Find the minimum total cost required to make the determinant of the matrix equal to XX.

一个 n×nn \times n 矩阵 BB 被称为区间矩阵,如果对每一行 ii,均存在一个连续的列区间 [li,ri][l_i, r_i](其中 1≤li≤ri≤n1 \le l_i \le r_i \le n),使得该区间内的所有元素均为 11,而该行其余位置的元素均为 00。形式化地,Bi,j=1B_{i, j} = 1 当且仅当 li≤j≤ril_i \le j \le r_i;否则 Bi,j=0B_{i, j} = 0。

给定一个整数 nn 和一个初始区间矩阵 AA,它由 nn 个三元组 (li,ri,ai)(l_i, r_i, a_i) 描述:第 ii 行对应区间 [li,ri][l_i, r_i],而 aia_i 是修改该行所需付出的代价。

令 S\mathcal{S} 表示所有可能的 n×nn \times n 区间矩阵构成的集合。我们定义 XX 为该集合中矩阵行列式的最大可能值:

X=max⁡B∈Sdet⁡(B).X = \max\limits_{B \in \mathcal{S}} \operatorname{det}(B).

(注意:∣S∣=(n(n+1)2)n|\mathcal{S}| = \left(\frac{n(n + 1)}{2}\right)^n,因为每行可独立选取任意一个合法区间)

你的目标是将 AA 变换为某个矩阵 A′A',使得 det⁡(A′)=X\operatorname{det}(A') = X。为此,你可以执行以下操作任意多次:

  • 选择一个行索引 ii(1≤i≤n1 \le i \le n)和一个新的合法区间 [L,R][L, R](1≤L≤R≤n1 \le L \le R \le n);
  • 将第 ii 行当前的区间替换为 [L,R][L, R];
  • 此操作的代价为 aia_i。

求使矩阵行列式等于 XX 所需的最小总代价。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤5⋅1041 \le t \le 5 \cdot 10^4). The description of the test cases follows.

The first line of each test case contains a single integer nn (1≤n≤1061 \le n \le 10^6) — the size of the matrix.

The next nn lines describe the rows of the matrix AA. The ii-th of these lines contains three integers lil_i, rir_i, and aia_i (1≤li≤ri≤n1 \le l_i \le r_i \le n, 1≤ai≤1091 \le a_i \le 10^9) — the interval boundaries and the cost of modifying the ii-th row.

It is guaranteed that the sum of nn over all test cases does not exceed 10610^6.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤5⋅1041 \le t \le 5 \cdot 10^4)。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤1061 \le n \le 10^6)—— 矩阵的大小。

接下来的 nn 行描述矩阵 AA 的各行。其中第 ii 行包含三个整数 lil_i、rir_i 和 aia_i(1≤li≤ri≤n1 \le l_i \le r_i \le n,1≤ai≤1091 \le a_i \le 10^9)—— 分别表示第 ii 行的区间边界及修改该行的代价。

保证所有测试用例中 nn 的总和不超过 10610^6。

输出格式

For each test case, print a single integer — the minimum total cost required to make det⁡(A)=X\operatorname{det}(A) = X.

对于每个测试用例,输出一个整数——使 det⁡(A)=X\operatorname{det}(A) = X 所需的最小总成本。

输入输出样例

  • 输入#1

    6
    2
    1 1 1
    2 2 1
    2
    2 2 1
    1 1 1
    4
    1 4 4
    2 2 3
    4 4 6
    2 4 5
    6
    1 6 1
    5 5 1000
    2 4 1000
    6 6 1000
    3 3 1000
    4 4 1000
    5
    1 1 1000000000
    1 1 1000000000
    1 1 1000000000
    1 1 1000000000
    1 1 1000000000
    5
    1 4 15
    4 4 14
    2 3 16
    2 2 14
    3 5 15

    输出#1

    0
    2
    3
    1000
    4000000000
    14

说明/提示

In the first example, n=2n = 2 and it can be shown that X=1X = 1. The matrix is A=[1001]A = \begin{bmatrix} 1 & 0\\0 & 1\end{bmatrix}. Since det⁡(A)=1\operatorname{det}(A) = 1, no operations are needed.

In the second example, XX is still 11, but the matrix is A=[0110]A = \begin{bmatrix} 0 & 1\\1 & 0\end{bmatrix}. The determinant is −1-1, so we must transform the matrix.

It can be proven that it is impossible to reach a determinant of 11 with a single operation. Therefore, we must modify both rows. One possible sequence of operations is:

\\begin{bmatrix} 0 & 1\\\\1 & 0\\end{bmatrix} \\xrightarrow{i = 2, \\, L = 2, \\, R = 2} \\begin{bmatrix} 0 & 1\\\\0 & 1\\end{bmatrix} \\xrightarrow{i = 1, \\, L = 1, \\, R = 2} \\begin{bmatrix} 1 & 1\\\\0 & 1\\end{bmatrix}

The final matrix has a determinant of 11. The total cost is a2+a1=1+1=2a_2 + a_1 = 1 + 1 = 2, which is the answer.

在第一个例子中,n=2n = 2,且可以证明 X=1X = 1。矩阵为 A=[1001]A = \begin{bmatrix} 1 & 0\\0 & 1\end{bmatrix}。由于 det⁡(A)=1\operatorname{det}(A) = 1,因此无需任何操作。

在第二个例子中,XX 仍为 11,但矩阵为 A=[0110]A = \begin{bmatrix} 0 & 1\\1 & 0\end{bmatrix}。其行列式为 −1-1,因此必须对该矩阵进行变换。

可以证明,仅通过一次操作无法使行列式变为 11。因此,我们必须修改两行。一种可能的操作序列为:

[0110]→i=2, L=2, R=2[0101]→i=1, L=1, R=2[1101]\begin{bmatrix} 0 & 1\\1 & 0\end{bmatrix} \xrightarrow{i = 2, \, L = 2, \, R = 2} \begin{bmatrix} 0 & 1\\0 & 1\end{bmatrix} \xrightarrow{i = 1, \, L = 1, \, R = 2} \begin{bmatrix} 1 & 1\\0 & 1\end{bmatrix}

最终矩阵的行列式为 11。总代价为 a2+a1=1+1=2a_2 + a_1 = 1 + 1 = 2,即为答案。

输入解题思路,AI测评打分。不知道怎么写?

首页