# Mana Points solution codechef

Mana Points solution codechef – Chef is playing a mobile game. In the game, Chef’s character Chefario can perform special attacks. However, one special attack costs  mana points to Chefario.

## Mana Points solution codechef

If Chefario currently has  mana points, determine the maximum number of special attacks he can perform.

### Input Format

• The first line contains a single integer  — the number of test cases. Then the test cases follow.
• The first and only line of each test case contains two space-separated integers  and  — the cost of one special attack and the number of mana points Chefario has initially.

### Output Format

For each test case, output the maximum number of special attacks Chefario can perform.

## Mana Points solution codechef

• 1≤�≤105
• 1≤�≤100
• 1≤�≤1000

### Sample 1:

Input

Output

3
10 30
6 41
50 2

3
6
0


## Mana Points solution codechef Explanation:

Test case 1: Chefario can perform a maximum of 3 special attacks which will cost him 30 mana points.

Test case 2: Chefario can perform a maximum of 6 special attacks which will cost him 36 mana points. Note that Chefario can not perform 7 special attacks as these will cost him 42 mana points while he has only 41 mana points.

Test case 3: Chefario will not be able to perform any special attacks in this case.

# Wonderful Jump solution codeforces

Wonderful Jump solution codeforces – You are given an array of positive integers 𝑎1,𝑎2,,𝑎𝑛a1,a2,…,an of length 𝑛n.

## Wonderful Jump solution codeforces

In one operation you can jump from index 𝑖i to index 𝑗j (1𝑖𝑗𝑛1≤i≤j≤n) by paying min(𝑎𝑖,𝑎𝑖+1,,𝑎𝑗)(𝑗𝑖)2min(ai,ai+1,…,aj)⋅(j−i)2 eris.

For all 𝑘k from 11 to 𝑛n, find the minimum number of eris needed to get from index 11 to index 𝑘k.

Input

The first line contains a single integer 𝑛n (2𝑛41052≤n≤4⋅105).

The second line contains 𝑛n integers 𝑎1,𝑎2,𝑎𝑛a1,a2,…an (1𝑎𝑖𝑛1≤ai≤n).

Output

Output 𝑛n integers — the 𝑘k-th integer is the minimum number of eris needed to reach index 𝑘k if you start from index 11.

## Wonderful Jump solution codeforces

input

Copy
3
2 1 3

output

Copy
0 1 2


## Wonderful Jump solution codeforces

Copy
6
1 4 1 6 3 2

output

Copy
0 1 2 3 6 8

input

Copy
2
1 2

output

Copy
0 1

input

Copy
4
1 4 4 4

output

Copy
0 1 4 8


## Wonderful Jump solution codeforces

In the first example:

• From 11 to 11: the cost is 00,
• From 11 to 22121→2 — the cost is min(2,1)(21)2=1min(2,1)⋅(2−1)2=1,
• From 11 to 331231→2→3 — the cost is min(2,1)(21)2+min(1,3)(32)2=1+1=2min(2,1)⋅(2−1)2+min(1,3)⋅(3−2)2=1+1=2.

In the fourth example from 11 to 441341→3→4 — the cost is min(1,4,4)(31)2+min(4,4)(43)2=4+4=8min(1,4,4)⋅(3−1)2+min(4,4)⋅(4−3)2=4+4=8.

Also read: Partial Sorting solution codeforces

# Partial Sorting solution codeforces

Partial Sorting solution codeforces – Consider a permutation 𝑝p of length 3𝑛3n. Each time you can do one of the following operations:

• Sort the first 2𝑛2n elements in increasing order.
• Sort the last 2𝑛2n elements in increasing order.

## Partial Sorting solution codeforces

We can show that every permutation can be made sorted in increasing order using only these operations. Let’s call 𝑓(𝑝)f(p) the minimum number of these operations needed to make the permutation 𝑝p sorted in increasing order.

Given 𝑛n, find the sum of 𝑓(𝑝)f(p) over all (3𝑛)!(3n)! permutations 𝑝p of size 3𝑛3n.

Since the answer could be very large, output it modulo a prime 𝑀M.

A permutation of length 𝑛n is an array consisting of 𝑛n distinct integers from 11 to 𝑛n 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 (𝑛=3n=3 but there is 44 in the array).

## Partial Sorting solution codeforces

The only line of input contains two numbers 𝑛n and 𝑀M (1𝑛1061≤n≤106108𝑀109108≤M≤109). It is guaranteed that 𝑀M is a prime number.

Also read: Lucky Permutation solution codeforces

Output

Examples
input

Copy
1 100009067


## Partial Sorting solution codeforces

Copy
9

input

Copy
2 100000357

output

Copy
1689


## Partial Sorting solution codeforces

Copy
69 999900997

output

Copy
193862705


## Partial Sorting solution codeforces

In the first test case, all the permutations are:

• [1,2,3][1,2,3], which requires 00 operations;
• [1,3,2][1,3,2], which requires 11 operation;
• [2,1,3][2,1,3], which requires 11 operation;
• [2,3,1][2,3,1], which requires 22 operations;
• [3,1,2][3,1,2], which requires 22 operations;
• [3,2,1][3,2,1], which requires 33 operations.

# Lucky Permutation solution codeforces

Lucky Permutation solution codeforces – You are given a permutation 𝑝p of length 𝑛n. In one operation, you can choose two indices 1𝑖<𝑗𝑛1≤i<j≤n and swap 𝑝𝑖pi with 𝑝𝑗pj.

## Lucky Permutation solution codeforces

Find the minimum number of operations needed to have exactly one inversion in the permutation.

A permutation is an array consisting of 𝑛n distinct integers from 11 to 𝑛n 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 (𝑛=3n=3 but there is 44 in the array).

The number of inversions of a permutation 𝑝p is the number of pairs of indices (𝑖,𝑗)(i,j) such that 1𝑖<𝑗𝑛1≤i<j≤n and 𝑝𝑖>𝑝𝑗pi>pj.

## Lucky Permutation solution codeforces

The first line contains a single integer 𝑡t (1𝑡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 𝑛n (2𝑛21052≤n≤2⋅105).

The second line of each test case contains 𝑛n integers 𝑝1,𝑝2,,𝑝𝑛p1,p2,…,pn (1𝑝𝑖𝑛1≤pi≤n). It is guaranteed that 𝑝p is a permutation.

Also read: Elemental Decompress solution codeforces

It is guaranteed that the sum of 𝑛n over all test cases does not exceed 21052⋅105.

Output

For each test case output a single integer — the minimum number of operations needed to have exactly one inversion in the permutation. It can be proven that an answer always exists.

## Lucky Permutation solution codeforces

input

Copy
4
2
2 1
2
1 2
4
3 4 1 2
4
2 4 3 1

output

Copy
0
1
3
1


## Lucky Permutation solution codeforces

In the first test case, the permutation already satisfies the condition.

In the second test case, you can perform the operation with (𝑖,𝑗)=(1,2)(i,j)=(1,2), after that the permutation will be [2,1][2,1] which has exactly one inversion.

In the third test case, it is not possible to satisfy the condition with less than 33 operations. However, if we perform 33 operations with (𝑖,𝑗)(i,j) being (1,3)(1,3),(2,4)(2,4), and (3,4)(3,4) in that order, the final permutation will be [1,2,4,3][1,2,4,3] which has exactly one inversion.

In the fourth test case, you can perform the operation with (𝑖,𝑗)=(2,4)(i,j)=(2,4), after that the permutation will be [2,1,3,4][2,1,3,4] which has exactly one inversion.

# Elemental Decompress solution codeforces

Elemental Decompress solution codeforces – You are given an array 𝑎a of 𝑛n integers.

Find two permutations 𝑝p and 𝑞q of length 𝑛n such that max(𝑝𝑖,𝑞𝑖)=𝑎𝑖max(pi,qi)=ai for all 1𝑖𝑛1≤i≤n or report that such 𝑝p and 𝑞q do not exist.

## Elemental Decompress solution codeforces

A permutation of length 𝑛n is an array consisting of 𝑛n distinct integers from 11 to 𝑛n 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 (𝑛=3n=3 but there is 44 in the array).

Input

The first line contains a single integer 𝑡t (1𝑡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 𝑛n (1𝑛21051≤n≤2⋅105).

The second line of each test case contains 𝑛n integers 𝑎1,𝑎2,,𝑎𝑛a1,a2,…,an (1𝑎𝑖𝑛1≤ai≤n) — the array 𝑎a.

It is guaranteed that the total sum of 𝑛n over all test cases does not exceed 21052⋅105.

## Elemental Decompress solution codeforces

For each test case, if there do not exist 𝑝p and 𝑞q that satisfy the conditions, output “NO” (without quotes).

Otherwise, output “YES” (without quotes) and then output 22 lines. The first line should contain 𝑛n integers 𝑝1,𝑝2,,𝑝𝑛p1,p2,…,pn and the second line should contain 𝑛n integers 𝑞1,𝑞2,,𝑞𝑛q1,q2,…,qn.

Also read: Quick Sort solution codeforces

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).

## Elemental Decompress solution codeforces

input

Copy
3
1
1
5
5 3 4 2 5
2
1 1

output

Copy
YES
1
1
YES
1 3 4 2 5
5 2 3 1 4
NO


## Elemental Decompress solution codeforces

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

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

• 𝑎1=max(𝑝1,𝑞1)=max(1,5)=5a1=max(p1,q1)=max(1,5)=5,
• 𝑎2=max(𝑝2,𝑞2)=max(3,2)=3a2=max(p2,q2)=max(3,2)=3,
• 𝑎3=max(𝑝3,𝑞3)=max(4,3)=4a3=max(p3,q3)=max(4,3)=4,
• 𝑎4=max(𝑝4,𝑞4)=max(2,1)=2a4=max(p4,q4)=max(2,1)=2,
• 𝑎5=max(𝑝5,𝑞5)=max(5,4)=5a5=max(p5,q5)=max(5,4)=5.

In the third test case, one can show that no such 𝑝p and 𝑞q exist.

# Quick Sort solution codeforces

Quick Sort solution codeforces – You are given a permutation 𝑝p of length 𝑛n and a positive integer 𝑘𝑛k≤n.

## Quick Sort solution codeforces

In one operation, you:

• Choose 𝑘k distinct elements 𝑝𝑖1,𝑝𝑖2,,𝑝𝑖𝑘pi1,pi2,…,pik.
• Remove them and then add them sorted in increasing order to the end of the permutation.

For example, if 𝑝=[2,5,1,3,4]p=[2,5,1,3,4] and 𝑘=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 𝑛n is an array consisting of 𝑛n distinct integers from 11 to 𝑛n 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 (𝑛=3n=3 but there is 44 in the array).

## Quick Sort solution codeforces

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

The first line of each test case contains two integers 𝑛n and 𝑘k (2𝑛1052≤n≤1051𝑘𝑛1≤k≤n).

The second line of each test case contains 𝑛n integers 𝑝1,𝑝2,,𝑝𝑛p1,p2,…,pn (1𝑝𝑖𝑛1≤pi≤n). It is guaranteed that 𝑝p is a permutation.

Wonderful Jump solution codeforces

It is guaranteed that the sum of 𝑛n 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.

## Quick Sort solution codeforces

input

Copy
4
3 2
1 2 3
3 1
3 1 2
4 2
1 3 2 4
4 2
2 3 1 4

output

Copy
0
1
1
2


## Quick Sort solution codeforces

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].