Problem A. Pretty Permutations: N N I I
Problem A. Pretty Permutations: N N I I
There are n cats in a line, labeled from 1 to n, with the i-th cat at position i. They are bored
of gyrating in the same spot all day, so they want to reorder themselves such that no cat is
in the same place as before. They are also lazy, so they want to minimize the total distance
they move. Help them decide what cat should be at each location after the reordering.
For example, if there are 3 cats, this is a valid reordering: [3, 1, 2]. No cat is in its original
position. The total distance the cats move is 1 + 1 + 2 = 4 as cat 1 moves one place to the
right, cat 2 moves one place to the right, and cat 3 moves two places to the left.
Input
The first line contains a single integer t (1 ≤ t ≤ 100) — the number of test cases. Then t
test cases follow.
The first and only line of each test case contains one integer n (2 ≤ n ≤ 100) — the number
of cats.
It can be proven that under the constraints of the problem, an answer always exist.
Output
Output t answers, one for each test case. Each answer consists of n integers — a
permutation with the minimum total distance. If there are multiple answers, print any.
Examples
Input Output
2 2 1
2 3 1 2
3
Note
Page 1 of 21
-
Contest 3.2 Jun 01, 2025
For the first test case, there is only one possible permutation that satisfies the conditions:
[2, 1].
The second test case was described in the statement. Another possible answer is [2, 3, 1].
Page 2 of 21
-
Contest 3.2 Jun 01, 2025
There is a chessboard of size n by n. The square in the i-th row from top and j -th column
from the left is labelled (i, j).
Currently, Gregor has some pawns in the n-th row. There are also enemy pawns in the 1-st
row. On one turn, Gregor moves one of his pawns. A pawn can move one square up (from
(i, j) to (i − 1, j)) if there is no pawn in the destination square. Additionally, a pawn can
move one square diagonally up (from (i, j) to either (i − 1, j − 1) or (i − 1, j + 1)) if and
only if there is an enemy pawn in that square. The enemy pawn is also removed.
Gregor wants to know what is the maximum number of his pawns that can reach row 1?
Note that only Gregor takes turns in this game, and the enemy pawns never move. Also,
when Gregor's pawn reaches row 1, it is stuck and cannot make any further moves.
Input
The first line of the input contains one integer t (1 ≤ t ≤ 2 ⋅ 104 ) — the number of test
cases. Then t test cases follow.
Each test case consists of three lines. The first line contains a single integer n (2 ≤n≤2⋅
105 ) — the size of the chessboard.
The second line consists of a string of binary digits of length n, where a 1 in the i-th
position corresponds to an enemy pawn in the i-th cell from the left, and 0 corresponds to
an empty cell.
The third line consists of a string of binary digits of length n, where a 1 in the i-th position
corresponds to a Gregor's pawn in the i-th cell from the left, and 0 corresponds to an empty
cell.
5
It is guaranteed that the sum of n across all test cases is less than 2 ⋅ 10 .
Output
Page 3 of 21
-
Contest 3.2 Jun 01, 2025
For each test case, print one integer: the maximum number of Gregor's pawns which can
reach the 1-st row.
Examples
Input Output
4 3
3 4
000 0
111 0
4
1111
1111
3
010
010
5
11001
00000
Note
In the first example, Gregor can simply advance all 3 of his pawns forward. Thus, the
answer is 3.
In the second example, Gregor can guarantee that all 4 of his pawns reach the enemy row,
by following the colored paths as demonstrated in the diagram below. Remember, only
Gregor takes turns in this "game"!
Page 4 of 21
-
Contest 3.2 Jun 01, 2025
In the third example, Gregor's only pawn is stuck behind the enemy pawn, and cannot reach
the end.
Page 5 of 21
-
Contest 3.2 Jun 01, 2025
Problem C. Era
Time limit 1000 ms
Mem limit 262144 kB
Shohag has an integer sequence a1 , a2 , … , an . He can perform the following operation any
For example, if a = [3, 3, 4] and he selects k = 2, then after the operation he can obtain one
of the sequences [2, 3, 3, 4], [3, 2, 3, 4], [3, 3, 2, 4], or [3, 3, 4, 2].
Shohag wants this sequence to satisfy the following condition: for each 1 ≤ i ≤ ∣a∣, ai ≤ i.
Help him to find the minimum number of operations that he has to perform to achieve this
goal. We can show that under the constraints of the problem it's always possible to achieve
this goal in a finite number of operations.
Input
The first line contains a single integer t (1 ≤ t ≤ 200) — the number of test cases.
The first line of each test case contains a single integer n (1 ≤ n ≤ 100) — the initial length
of the sequence.
Output
For each test case, print a single integer — the minimum number of operations needed to
perform to achieve the goal mentioned in the statement.
Page 6 of 21
-
Contest 3.2 Jun 01, 2025
Examples
Input Output
4 1
3 3
1 3 4 0
5 696966
1 2 5 7 4
1
1
3
69 6969 696969
Note
In the second test case, Shohag can perform the following operations:
In the third test case, the sequence already satisfies the condition.
Page 7 of 21
-
Contest 3.2 Jun 01, 2025
Problem Statement
You are given an N × N grid S and an M × M grid T . The cell at the i-th row from the top
and the j -th column from the left is denoted by (i, j).
The colors of the cells in S and T are represented by N 2 characters Si,j (1 ≤ i, j ≤ N ) and
2
M characters Ti,j (1 ≤ i, j ≤ M ), respectively. In grid S , cell (i, j) is white if Si,j is . , and
Constraints
1 ≤ M ≤ N ≤ 50
N and M are integers.
Each of Si,j and Ti,j is . or # .
Input
N M
S1,1 S1,2 … S1,N
⋮
SN ,1 SN ,2 … SN ,N
⋮
TM ,1 TM ,2 … TM ,M
Page 8 of 21
-
Contest 3.2 Jun 01, 2025
Output
Sample 1
Input Output
3 2 2 2
#.#
..#
##.
.#
#.
The 2 × 2 subgrid of S from the 2nd to the 3rd row and from the 2nd to the 3rd column
matches T .
Sample 2
Input Output
2 1 1 2
#.
##
.
Page 9 of 21
-
Contest 3.2 Jun 01, 2025
Problem E. 12435
Time limit 2000 ms
Mem limit 1048576 kB
Problem Statement
(1, 2, 3, 4, 5).
Determine whether A can be sorted in ascending order by performing exactly one operation
of swapping two adjacent elements in A.
Constraints
Input
A1 A2 A3 A4 A5
Output
If A can be sorted in ascending order by exactly one operation, print Yes ; otherwise, print
No .
Sample 1
Input Output
1 2 4 3 5 Yes
Page 10 of 21
-
Contest 3.2 Jun 01, 2025
Sample 2
Input Output
5 3 2 4 1 No
Sample 3
Input Output
1 2 3 4 5 No
Sample 4
Input Output
2 1 3 4 5 Yes
Page 11 of 21
-
Contest 3.2 Jun 01, 2025
Problem F. Watchmen
Time limit 3000 ms
Mem limit 262144 kB
Watchmen are in a danger and Doctor Manhattan together with his friend Daniel Dreiberg
should warn them as soon as possible. There are n watchmen on a plane, the i-th watchman
is located at point (xi, yi).
They need to arrange a plan, but there are some difficulties on their way. As you know,
Doctor Manhattan considers the distance between watchmen i and j to be |xi - xj| + |yi - yj|.
The success of the operation relies on the number of pairs (i, j) (1 ≤ i < j ≤ n), such that the
distance between watchman i and watchmen j calculated by Doctor Manhattan is equal to
the distance between them calculated by Daniel. You were asked to compute the number of
such pairs.
Input
The first line of the input contains the single integer n (1 ≤ n ≤ 200 000) — the number of
watchmen.
Each of the following n lines contains two integers xi and yi (|xi|, |yi| ≤ 109).
Output
Print the number of pairs of watchmen such that the distance between them calculated by
Doctor Manhattan is equal to the distance calculated by Daniel.
Examples
Page 12 of 21
-
Contest 3.2 Jun 01, 2025
Input Output
3 2
1 1
7 5
1 5
Input Output
6 11
0 0
0 1
0 2
-1 1
0 1
1 1
Note
In the first sample, the distance between watchman 1 and watchman 2 is equal to
|1 - 7| + |1 - 5| = 10 for Doctor Manhattan and for Daniel.
For pairs (1, 1), (1, 5) and (7, 5), (1, 5) Doctor Manhattan and Daniel will calculate the same
distances.
Page 13 of 21
-
Contest 3.2 Jun 01, 2025
We call a phone number a beautiful if it is a string of 10 digits, where the i-th digit from the
left is at least 10 − i. That is, the first digit must be at least 9, the second at least 8, …, with
the last digit being at least 0.
For example, 9988776655 is a beautiful phone number, while 9099999999 is not, since
the second digit, which is 0, is less than 8.
Vadim has a beautiful phone number. He wants to rearrange its digits in such a way that the
result is the smallest possible beautiful phone number. Help Vadim solve this problem.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (
1 ≤ t ≤ 104 ). The description of the test cases follows.
The only line of each test case contains a single string s of length 10, consisting of digits. It
is guaranteed that s is a beautiful phone number.
Output
For each test case, output a single string of length 10 — the smallest possible beautiful
phone number that Vadim can obtain.
Examples
Input Output
4 9999999999
9999999999 9876556789
9988776655 9876567890
9988776650 9899999999
9899999999
Page 14 of 21
-
Contest 3.2 Jun 01, 2025
Note
In the first test case, for the first phone number 9999999999, regardless of the
rearrangement of digits, the same phone number is obtained.
In the second test case, for the phone number 9988776655, it can be proven that
9876556789 is the smallest phone number that can be obtained by rearranging the digits.
Page 15 of 21
-
Contest 3.2 Jun 01, 2025
The heroic outlaw Robin Hood is famous for taking from the rich and giving to the poor.
Robin encounters n people starting from the 1-st and ending with the n-th. The i-th
person has ai gold. If ai≥ k , Robin will take all ai gold, and if ai = 0, Robin will give 1 gold
Input
The first line of the input contains a single integer t (1 ≤ t ≤ 104 ) — the number of test
cases.
The first line of each test case contains two integers n, k (1 ≤ n ≤ 50, 1 ≤ k ≤ 100) — the
number of people and the threshold at which Robin Hood takes the gold.
Output
For each test case, output a single integer, the number of people that will get gold from
Robin Hood.
Examples
Page 16 of 21
-
Contest 3.2 Jun 01, 2025
Input Output
4 1
2 2 2
2 0 3
3 2 0
3 0 0
6 2
0 3 0 0 0 0
2 5
5 4
Note
In the first test case, Robin takes 2 gold from the first person and gives a gold to the second
person.
In the second test case, Robin takes 3 gold and gives 1 gold to each of the next 2 people.
In the third test case, Robin takes 3 gold and so only gives gold to 3 other people.
Page 17 of 21
-
Contest 3.2 Jun 01, 2025
There is an array a with n − 1 integers. Let x be the bitwise XOR of all elements of the array.
The number x is added to the end of the array a (now it has length n), and then the
elements are shuffled.
You are given the newly formed array a. What is x? If there are multiple possible values of x,
you can output any of them.
Input
The input consists of multiple test cases. The first line contains an integer t (1 ≤ t ≤ 1000
) — the number of test cases. The description of the test cases follows.
The first line of each test case contains an integer n (2 ≤ n ≤ 100) — the number of
integers in the resulting array a.
Additional constraint on the input: the array a is made by the process described in the
statement; that is, some value of x exists.
Output
For each test case, output a single integer — the value of x, as described in the statement. If
there are multiple possible values of x, output any of them.
Examples
Page 18 of 21
-
Contest 3.2 Jun 01, 2025
Input Output
4 3
4 7
4 3 2 5 6
5 0
6 1 10 7 10
6
6 6 6 6 6 6
3
100 100 0
Note
In the second test case, one possible array a is a = [1, 10, 6, 10]. Then x = 1 ⊕ 10 ⊕ 6 ⊕
10 = 7, so the new array is [1, 10, 6, 10, 7]. Afterwards, the array is shuffled to form
[6, 1, 10, 7, 10].
In the third test case, all elements of the array are equal to 6, so x = 6.
In the fourth test case, one possible array a is a= [100, 100]. Then x = 100 ⊕ 100 = 0, so
the new array is [100, 100, 0]. Afterwards, the array is shuffled to form [100, 100, 0]. (Note
that after the shuffle, the array can remain the same.)
Page 19 of 21
-
Contest 3.2 Jun 01, 2025
One can notice the following remarkable mathematical fact: the number 2025 can be represented
as (20 + 25)2 .
You are given a year represented by a string s, consisting of exactly 4 characters. Thus,
leading zeros are allowed in the year representation. For example, "0001", "0185", "1375"
are valid year representations. You need to express it in the form (a + b)2 , where a and b are
non-negative integers, or determine that it is impossible.
For example, if s = "0001", you can choose a = 0, b = 1, and write the year as (0 + 1)2 = 1.
Input
The first line of the input contains a single integer t (1 ≤ t ≤ 104 ) — the number of test
cases.
The only line of each test case contains a string s, consisting of exactly 4 characters. Each
character is a digit from 0 to 9.
Output
Two numbers a and b (a, b ≥ 0) such that (a + b)2 = s, if they exist. If there are
multiple suitable pairs, you may output any of them.
The number −1 otherwise.
Examples
Page 20 of 21
-
Contest 3.2 Jun 01, 2025
Input Output
5 0 1
0001 -1
1001 -1
1000 34 36
4900 20 25
2025
Page 21 of 21
-