CF1792D.Fixed Prefix Permutations

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given nn permutations a1,a2,…,ana_1, a_2, \dots, a_n, each of length mm. Recall that a permutation of length mm is a sequence of mm distinct integers from 11 to mm.

Let the beauty of a permutation p1,p2,…,pmp_1, p_2, \dots, p_m be the largest kk such that p1=1,p2=2,…,pk=kp_1 = 1, p_2 = 2, \dots, p_k = k. If p1≠1p_1 \neq 1, then the beauty is 00.

The product of two permutations p⋅qp \cdot q is a permutation rr such that rj=qpjr_j = q_{p_j}.

For each ii from 11 to nn, print the largest beauty of a permutation ai⋅aja_i \cdot a_j over all jj from 11 to nn (possibly, i=ji = j).

给你 nn 个长度为 mm 的排列 a1,a2,…,ana_1, a_2, \dots, a_n。回忆一下,一个长度为 mm 的排列是由 11 到 mm 的 mm 个互不相同的整数组成的序列。

定义一个排列 p1,p2,…,pmp_1, p_2, \dots, p_m 的**优美值(beauty)**为最大的 kk,使得 p1=1,p2=2,…,pk=kp_1 = 1, p_2 = 2, \dots, p_k = k;若 p1≠1p_1 \neq 1,则其优美值为 00。

两个排列 pp 与 qq 的乘积 p⋅qp \cdot q 是一个排列 rr,满足 rj=qpjr_j = q_{p_j}。

对每个从 11 到 nn 的 ii,输出在所有 jj(1≤j≤n1 \le j \le n,允许 i=ji = j)中,排列 ai⋅aja_i \cdot a_j 的最大优美值。

输入格式

The first line contains a single integer tt (1≤t≤1041 \le t \le 10^4) — the number of testcases.

The first line of each testcase contains two integers nn and mm (1≤n≤5⋅1041 \le n \le 5 \cdot 10^4; 1≤m≤101 \le m \le 10) — the number of permutations and the length of each permutation.

The ii-th of the next nn lines contains a permutation aia_i — mm distinct integers from 11 to mm.

The sum of nn doesn't exceed 5⋅1045 \cdot 10^4 over all testcases.

第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)—— 测试用例的数量。

每个测试用例的第一行包含两个整数 nn 和 mm(1≤n≤5⋅1041 \le n \le 5 \cdot 10^4;1≤m≤101 \le m \le 10)—— 排列的个数以及每个排列的长度。

接下来 nn 行中的第 ii 行包含一个排列 aia_i —— 由 11 到 mm 中互不相同的 mm 个整数组成。

所有测试用例中 nn 的总和不超过 5⋅1045 \cdot 10^4。

输出格式

For each testcase, print nn integers. The ii-th value should be equal to the largest beauty of a permutation ai⋅aja_i \cdot a_j over all jj (1≤j≤n1 \le j \le n).

对于每个测试用例,输出 nn 个整数。其中第 ii 个值应等于所有 jj(1≤j≤n1 \le j \le n)对应的排列 ai⋅aja_i \cdot a_j 的最大美观度。

输入输出样例

  • 输入#1

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

    输出#1

    1 4 4 
    2 2 
    10 8 1 6 8 10 1 7

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

首页