CF1209E2.Rotate Columns (hard version)

省选/NOI-

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

This is a harder version of the problem. The difference is only in constraints.

You are given a rectangular n×mn \times m matrix aa. In one move you can choose any column and cyclically shift elements in this column. You can perform this operation as many times as you want (possibly zero). You can perform this operation to a column multiple times.

After you are done with cyclical shifts, you compute for every row the maximal value in it. Suppose that for ii-th row it is equal rir_i. What is the maximal possible value of r1+r2+…+rnr_1+r_2+\ldots+r_n?

这是一个更难版本的问题,区别仅在于约束条件。

给你一个 n×mn \times m 的矩形矩阵 aa。在一次操作中,你可以任选一列,并对该列元素进行循环移位(cyclic shift)。该操作可执行任意多次(包括零次),且可对同一列执行多次。

完成所有循环移位操作后,你将对每一行计算其最大值。设第 ii 行的最大值为 rir_i。那么 r1+r2+…+rnr_1 + r_2 + \ldots + r_n 的最大可能值是多少?

输入格式

The first line contains an integer tt (1≤t≤401 \le t \le 40), the number of test cases in the input.

The first line of each test case contains integers nn and mm (1≤n≤121 \le n \le 12, 1≤m≤20001 \le m \le 2000) — the number of rows and the number of columns in the given matrix aa.

Each of the following nn lines contains mm integers, the elements of aa (1≤ai,j≤1051 \le a_{i, j} \le 10^5).

第一行包含一个整数 tt(1≤t≤401 \le t \le 40),表示输入中测试用例的数量。

每个测试用例的第一行包含两个整数 nn 和 mm(1≤n≤121 \le n \le 12,1≤m≤20001 \le m \le 2000)—— 分别表示给定矩阵 aa 的行数和列数。

接下来的 nn 行,每行包含 mm 个整数,表示矩阵 aa 的元素(1≤ai,j≤1051 \le a_{i, j} \le 10^5)。

输出格式

Print tt integers: answers for all test cases in the order they are given in the input.

输出 tt 个整数:按输入中给出的顺序,输出所有测试用例的答案。

输入输出样例

  • 输入#1

    3
    2 3
    2 5 7
    4 2 4
    3 6
    4 1 5 2 10 4
    8 6 6 4 9 10
    5 4 9 5 8 7
    3 3
    9 9 9
    1 1 1
    1 1 1

    输出#1

    12
    29
    27

说明/提示

In the first test case you can shift the third column down by one, this way there will be r1=5r_1 = 5 and r2=7r_2 = 7.

In the second case you can don't rotate anything at all, this way there will be r1=r2=10r_1 = r_2 = 10 and r3=9r_3 = 9.

在第一个测试用例中,你可以将第三列向下移动一格,这样将得到 r1=5r_1 = 5 和 r2=7r_2 = 7。

在第二个测试用例中,你完全可以不进行任何旋转,这样将得到 r1=r2=10r_1 = r_2 = 10 且 r3=9r_3 = 9。

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

首页