CF2145D.Inversion Value of a Permutation

普及+/提高

通过率:0%

时间限制:3.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

A permutation of length nn is an array of nn integers, where each number from 11 to nn appears exactly once. An inversion in a permutation pp is a pair of indices (i,j)(i, j) such that i<ji \lt j and pi>pjp_i \gt p_j.

For a permutation pp, we define its inversion value as the number of its subsegments that contain at least one inversion. Formally, this is the number of pairs of integers (l,r)(l, r) (1≤l<r≤n1 \le l \lt r \le n) for which there exists a pair of indices (i,j)(i, j) satisfying the following conditions: l≤i<j≤rl \le i \lt j \le r and pi>pjp_i \gt p_j.

For example, for the permutation [3,1,4,2][3, 1, 4, 2], the inversion value is 55.

You are given two integers nn and kk. Your task is to construct a permutation of length nn with an inversion value equal to exactly kk.

长度为 nn 的排列是指由 nn 个整数组成的数组,其中每个从 11 到 nn 的整数恰好出现一次。排列 pp 中的一个逆序对是指满足 i<ji \lt j 且 pi>pjp_i \gt p_j 的下标对 (i,j)(i, j)。

对于一个排列 pp,我们定义其逆序值为:包含至少一个逆序对的子区间的个数。形式化地说,即满足如下条件的整数对 (l,r)(l, r)(其中 1≤l<r≤n1 \le l \lt r \le n)的个数:存在一对下标 (i,j)(i, j),使得 l≤i<j≤rl \le i \lt j \le r 且 pi>pjp_i \gt p_j。

例如,对于排列 [3,1,4,2][3, 1, 4, 2],其逆序值为 55。

给定两个整数 nn 和 kk,你的任务是构造一个长度为 nn 的排列,使其逆序值恰好等于 kk。

输入格式

The first line contains one integer tt (1≤t≤5001 \le t \le 500) — the number of test cases.

Each test case consists of a single line containing two integers nn and kk (2≤n≤302 \le n \le 30; 0≤k≤n(n−1)20 \le k \le \dfrac{n(n-1)}{2}).

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

每个测试用例由一行组成,包含两个整数 nn 和 kk(2≤n≤302 \le n \le 30;0≤k≤n(n−1)20 \le k \le \dfrac{n(n-1)}{2})。

输出格式

For each test case, output the answer as follows:

  • if the desired permutation does not exist, output a single integer 00;
  • otherwise, output nn distinct integers from 11 to nn — the desired permutation. If there are multiple such permutations, you may output any of them.

对于每个测试用例,按如下方式输出答案:

  • 如果所要求的排列不存在,则输出单个整数 00;
  • 否则,输出 nn 个互不相同的、取值范围在 11 到 nn 之间的整数——即所要求的排列。如果存在多个满足条件的排列,输出其中任意一个即可。

输入输出样例

  • 输入#1

    5
    4 5
    5 10
    5 0
    6 8
    3 1

    输出#1

    3 1 4 2
    5 4 3 2 1
    1 2 3 4 5
    2 3 5 6 1 4
    0

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

首页