Codeforces Round #842 (Div. 2)题解

/*写在前面*/

/*这次算是第一次补题了,原因是在做的过程中做出了c题,可是后来重判的时候超时了,然后修修补补,终于弄好了,记录一下*/

A. Greatest Convex

time limit per test1 second

memory limit per test256 megabytes

inputstandard input

outputstandard output

You are given an integer kk. Find the largest integer xx, where 1≤x<k1≤x<k, such that x!+(x−1)!†x!+(x−1)!† is a multiple of ‡‡ kk, or determine that no such xx exists.

†† y!y! denotes the factorial of yy, which is defined recursively as y!=y⋅(y−1)!y!=y⋅(y−1)! for y≥1y≥1 with the base case of 0!=10!=1. For example, 5!=5⋅4⋅3⋅2⋅1⋅0!=1205!=5⋅4⋅3⋅2⋅1⋅0!=120.

‡‡ If aa and bb are integers, then aa is a multiple of bb if there exists an integer cc such that a=b⋅ca=b⋅c. For example, 1010 is a multiple of 55 but 99 is not a multiple of 66.

Input

The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases. The description of test cases follows.

The only line of each test case contains a single integer kk (2≤k≤1092≤k≤109).

Output

For each test case output a single integer — the largest possible integer xx that satisfies the conditions above.

If no such xx exists, output −1−1.

Example

input

Copy

436810

output

Copy

2

5

7

9

Note

In the first test case, 2!+1!=2+1=32!+1!=2+1=3, which is a multiple of 33.

In the third test case, 7!+6!=5040+720=57607!+6!=5040+720=5760, which is a multiple of 88.

B. Quick Sort

time limit per test1 second

memory limit per test256 megabytes

inputstandard input

outputstandard output

You are given a permutation†† pp of length nn and a positive integer k≤nk≤n.

In one operation, you:

  • Choose kk distinct elements pi1,pi2,…,pikpi1,pi2,…,pik.

  • Remove them and then add them sorted in increasing order to the end of the permutation.

For example, if p=[2,5,1,3,4]p=[2,5,1,3,4] and k=2k=2 and you choose 55 and 33 as the elements for the operation, then [2,5,1,3,4]→[2,1,4,3,5][2,5,1,3,4]→[2,1,4,3,5].

Find the minimum number of operations needed to sort the permutation in increasing order. It can be proven that it is always possible to do so.

†† A permutation of length nn is an array consisting of nn distinct integers from 11 to nn in arbitrary order. For example, [2,3,1,5,4][2,3,1,5,4] is a permutation, but [1,2,2][1,2,2] is not a permutation (22 appears twice in the array), and [1,3,4][1,3,4] is also not a permutation (n=3n=3 but there is 44 in the array).

Input

The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases. The description of test cases follows.

The first line of each test case contains two integers nn and kk (2≤n≤1052≤n≤105, 1≤k≤n1≤k≤n).

The second line of each test case contains nn integers p1,p2,…,pnp1,p2,…,pn (1≤pi≤n1≤pi≤n). It is guaranteed that pp is a permutation.

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

Output

For each test case output a single integer — the minimum number of operations needed to sort the permutation. It can be proven that it is always possible to do so.

Example

input

Copy

43 21 2 33 13 1 24 21 3 2 44 22 3 1 4

output

Copy

0

1

1

2

Note

In the first test case, the permutation is already sorted.

In the second test case, you can choose element 33, and the permutation will become sorted as follows: [3,1,2]→[1,2,3][3,1,2]→[1,2,3].

In the third test case, you can choose elements 33 and 44, and the permutation will become sorted as follows: [1,3,2,4]→[1,2,3,4][1,3,2,4]→[1,2,3,4].

In the fourth test case, it can be shown that it is impossible to sort the permutation in 11 operation. However, if you choose elements 22 and 11 in the first operation, and choose elements 33 and 44 in the second operation, the permutation will become sorted as follows: [2,3,1,4]→[3,4,1,2]→[1,2,3,4][2,3,1,4]→[3,4,1,2]→[1,2,3,4].

C. Elemental Decompress

time limit per test2 seconds

memory limit per test256 megabytes

inputstandard input

outputstandard output

You are given an array aa of nn integers.

Find two permutations†† pp and qq of length nn such that max(pi,qi)=aimax(pi,qi)=ai for all 1≤i≤n1≤i≤n or report that such pp and qq do not exist.

†† A permutation of length nn is an array consisting of nn distinct integers from 11 to nn in arbitrary order. For example, [2,3,1,5,4][2,3,1,5,4] is a permutation, but [1,2,2][1,2,2] is not a permutation (22 appears twice in the array), and [1,3,4][1,3,4] is also not a permutation (n=3n=3 but there is 44 in the array).

Input

The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases. The description of test cases follows.

The first line of each test case contains a single integer nn (1≤n≤2⋅1051≤n≤2⋅105).

The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤n1≤ai≤n) — the array aa.

It is guaranteed that the total sum of nn over all test cases does not exceed 2⋅1052⋅105.

Output

For each test case, if there do not exist pp and qq that satisfy the conditions, output "NO" (without quotes).

Otherwise, output "YES" (without quotes) and then output 22 lines. The first line should contain nn integers p1,p2,…,pnp1,p2,…,pn and the second line should contain nn integers q1,q2,…,qnq1,q2,…,qn.

If there are multiple solutions, you may output any of them.

You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).

Example

input

Copy

31155 3 4 2 521 1

output

Copy

YES

1

1

YES

1 3 4 2 5

5 2 3 1 4

NO

Note

In the first test case, p=q=[1]p=q=[1]. It is correct since a1=max(p1,q1)=1a1=max(p1,q1)=1.

In the second test case, p=[1,3,4,2,5]p=[1,3,4,2,5] and q=[5,2,3,1,4]q=[5,2,3,1,4]. It is correct since:

  • a1=max(p1,q1)=max(1,5)=5a1=max(p1,q1)=max(1,5)=5,

  • a2=max(p2,q2)=max(3,2)=3a2=max(p2,q2)=max(3,2)=3,

  • a3=max(p3,q3)=max(4,3)=4a3=max(p3,q3)=max(4,3)=4,

  • a4=max(p4,q4)=max(2,1)=2a4=max(p4,q4)=max(2,1)=2,

  • a5=max(p5,q5)=max(5,4)=5a5=max(p5,q5)=max(5,4)=5.

In the third test case, one can show that no such pp and qq exist.

/***************************解释************************/

这里我用了很多个数组(还好元素值不是很大),用来储存各种各样的数据,因为使用下标访问会很快

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嗯嗯你说的对

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值