From 9c74f2847ddcb586adf1edeac2a2bb88921012e2 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Tue, 25 Jul 2023 23:06:22 +0200 Subject: [PATCH] 1851(rs): solve contest MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * “A. Escalator Conversations” * “B. Parity Sort” * “C. Tiles Comeback” Signed-off-by: Matej Focko --- 1851/index.html | 1279 +++++++++++++++++++++++++++++++++++++++++++++ 1851/src/bin/a.rs | 236 +++++++++ 1851/src/bin/b.rs | 244 +++++++++ 1851/src/bin/c.rs | 253 +++++++++ 4 files changed, 2012 insertions(+) create mode 100644 1851/index.html create mode 100644 1851/src/bin/a.rs create mode 100644 1851/src/bin/b.rs create mode 100644 1851/src/bin/c.rs diff --git a/1851/index.html b/1851/index.html new file mode 100644 index 0000000..1efd527 --- /dev/null +++ b/1851/index.html @@ -0,0 +1,1279 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Problems - Codeforces + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + +
+
+ + +
+ +
Codeforces Round 888 (Div. 3)
+
+ +
+
+ + + +
+
+ +
+ +
A. Escalator Conversations
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

One day, Vlad became curious about who he can have a conversation with on the escalator in the subway. There are a total of $$$n$$$ passengers. The escalator has a total of $$$m$$$ steps, all steps indexed from $$$1$$$ to $$$m$$$ and $$$i$$$-th step has height $$$i \cdot k$$$.

Vlad's height is $$$H$$$ centimeters. Two people with heights $$$a$$$ and $$$b$$$ can have a conversation on the escalator if they are standing on different steps and the height difference between them is equal to the height difference between the steps.

For example, if two people have heights $$$170$$$ and $$$180$$$ centimeters, and $$$m = 10, k = 5$$$, then they can stand on steps numbered $$$7$$$ and $$$5$$$, where the height difference between the steps is equal to the height difference between the two people: $$$k \cdot 2 = 5 \cdot 2 = 10 = 180 - 170$$$. There are other possible ways.

Given an array $$$h$$$ of size $$$n$$$, where $$$h_i$$$ represents the height of the $$$i$$$-th person. Vlad is interested in how many people he can have a conversation with on the escalator individually.

For example, if $$$n = 5, m = 3, k = 3, H = 11$$$, and $$$h = [5, 4, 14, 18, 2]$$$, Vlad can have a conversation with the person with height $$$5$$$ (Vlad will stand on step $$$1$$$, and the other person will stand on step $$$3$$$) and with the person with height $$$14$$$ (for example, Vlad can stand on step $$$3$$$, and the other person will stand on step $$$2$$$). Vlad cannot have a conversation with the person with height $$$2$$$ because even if they stand on the extreme steps of the escalator, the height difference between them will be $$$6$$$, while their height difference is $$$9$$$. Vlad cannot have a conversation with the rest of the people on the escalator, so the answer for this example is $$$2$$$.

Input

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases.

Then the descriptions of the test cases follow.

The first line of each test case contains integers: $$$n, m, k, H$$$ ($$$1 \le n,m \le 50$$$, $$$1 \le k,H \le 10^6$$$). Here, $$$n$$$ is the number of people, $$$m$$$ is the number of steps, $$$k$$$ is the height difference between neighboring steps, and $$$H$$$ is Vlad's height.

The second line contains $$$n$$$ integers: $$$h_1, h_2, \ldots, h_n$$$ ($$$1 \le h_i \le 10^6$$$). Here, $$$h_i$$$ represents the height of the $$$i$$$-th person.

Output

For each test case, output a single integer — the number of people Vlad can have a conversation with on the escalator individually.

Example
Input
7
5 3 3 11
5 4 14 18 2
2 9 5 6
11 9
10 50 3 11
43 44 74 98 62 60 99 4 11 73
4 8 8 49
68 58 82 73
7 1 4 66
18 66 39 83 48 99 79
9 1 1 13
26 23 84 6 60 87 40 41 25
6 13 3 28
30 70 85 13 1 55
Output
+2
+1
+4
+1
+0
+0
+3
+
Note

The first example is explained in the problem statement.

In the second example, Vlad can have a conversation with the person with height $$$11$$$.

In the third example, Vlad can have a conversation with people with heights: $$$44, 74, 98, 62$$$. Therefore, the answer is $$$4$$$.

In the fourth example, Vlad can have a conversation with the person with height $$$73$$$.

+
+ + + + + +
+
+ + +
+ +
B. Parity Sort
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You have an array of integers $$$a$$$ of length $$$n$$$. You can apply the following operation to the given array:

  • Swap two elements $$$a_i$$$ and $$$a_j$$$ such that $$$i \neq j$$$, $$$a_i$$$ and $$$a_j$$$ are either both even or both odd.

Determine whether it is possible to sort the array in non-decreasing order by performing the operation any number of times (possibly zero).

For example, let $$$a$$$ = [$$$7, 10, 1, 3, 2$$$]. Then we can perform $$$3$$$ operations to sort the array:

  1. Swap $$$a_3 = 1$$$ and $$$a_1 = 7$$$, since $$$1$$$ and $$$7$$$ are odd. We get $$$a$$$ = [$$$1, 10, 7, 3, 2$$$];
  2. Swap $$$a_2 = 10$$$ and $$$a_5 = 2$$$, since $$$10$$$ and $$$2$$$ are even. We get $$$a$$$ = [$$$1, 2, 7, 3, 10$$$];
  3. Swap $$$a_4 = 3$$$ and $$$a_3 = 7$$$, since $$$3$$$ and $$$7$$$ are odd. We get $$$a$$$ = [$$$1, 2, 3, 7, 10$$$].
Input

The first line of input data contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases.

The description of the test cases follows.

The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$.

The second line of each test case contains exactly $$$n$$$ positive integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the elements of array $$$a$$$.

It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

Output

For each test case, output on a separate line:

  • YES if the array can be sorted by applying the operation to it some number of times;
  • NO otherwise.

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

Example
Input
6
5
7 10 1 3 2
4
11 9 3 5
5
11 3 15 3 2
6
10 7 8 1 2 3
1
10
5
6 6 4 1 6
Output
+YES
+YES
+NO
+NO
+YES
+NO
Note

The first test case is explained in the problem statement.

+
+ + + + + +
+
+ + +
+ +
C. Tiles Comeback
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Vlad remembered that he had a series of $$$n$$$ tiles and a number $$$k$$$. The tiles were numbered from left to right, and the $$$i$$$-th tile had colour $$$c_i$$$.

If you stand on the first tile and start jumping any number of tiles right, you can get a path of length $$$p$$$. The length of the path is the number of tiles you stood on.

Vlad wants to see if it is possible to get a path of length $$$p$$$ such that:

  • it ends at tile with index $$$n$$$;
  • $$$p$$$ is divisible by $$$k$$$
  • the path is divided into blocks of length exactly $$$k$$$ each;
  • tiles in each block have the same colour, the colors in adjacent blocks are not necessarily different.

For example, let $$$n = 14$$$, $$$k = 3$$$.

The colours of the tiles are contained in the array $$$c$$$ = [$$$\color{red}{1}, \color{violet}{2}, \color{red}{1}, \color{red}{1}, \color{violet}{2}, \color{violet}{2}, \color{green}{3}, \color{green}{3}, \color{red}{1}, \color{green}{3}, \color{blue}{4}, \color{blue}{4}, \color{violet}{2}, \color{blue}{4}$$$]. Then we can construct a path of length $$$6$$$ consisting of $$$2$$$ blocks:

$$$\color{red}{c_1} \rightarrow \color{red}{c_3} \rightarrow \color{red}{c_4} \rightarrow \color{blue}{c_{11}} \rightarrow \color{blue}{c_{12}} \rightarrow \color{blue}{c_{14}}$$$

All tiles from the $$$1$$$-st block will have colour $$$\color{red}{\textbf{1}}$$$, from the $$$2$$$-nd block will have colour $$$\color{blue}{\textbf{4}}$$$.

It is also possible to construct a path of length $$$9$$$ in this example, in which all tiles from the $$$1$$$-st block will have colour $$$\color{red}{\textbf{1}}$$$, from the $$$2$$$-nd block will have colour $$$\color{green}{\textbf{3}}$$$, and from the $$$3$$$-rd block will have colour $$$\color{blue}{\textbf{4}}$$$.

Input

The first line of input data contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases.

The description of the test cases follows.

The first line of each test case ontains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 2 \cdot 10^5$$$)—the number of tiles in the series and the length of the block.

The second line of each test case contains $$$n$$$ integers $$$c_1, c_2, c_3, \dots, c_n$$$ ($$$1 \le c_i \le n$$$) — the colours of the tiles.

It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

Output

For each test case, output on a separate line:

  • YES if you can get a path that satisfies these conditions;
  • NO otherwise.

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

Example
Input
10
4 2
1 1 1 1
14 3
1 2 1 1 7 5 3 3 1 3 4 4 2 4
3 3
3 1 3
10 4
1 2 1 2 1 2 1 2 1 2
6 2
1 3 4 1 6 6
2 2
1 1
4 2
2 1 1 1
2 1
1 2
3 2
2 2 2
4 1
1 1 2 2
Output
+YES
+YES
+NO
+NO
+YES
+YES
+NO
+YES
+YES
+YES
+
Note

In the first test case, you can jump from the first tile to the last tile;

The second test case is explained in the problem statement.

+
+ + + + + +
+
+ + +
+ +
D. Prefix Permutation Sums
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Your friends have an array of $$$n$$$ elements, calculated its array of prefix sums and passed it to you, accidentally losing one element during the transfer. Your task is to find out if the given array can matches permutation.

A permutation of $$$n$$$ elements is an array of $$$n$$$ numbers from $$$1$$$ to $$$n$$$ such that each number occurs exactly one times in it.

The array of prefix sums of the array $$$a$$$ — is such an array $$$b$$$ that $$$b_i = \sum_{j=1}^i a_j, 1 \le i \le n$$$.

For example, the original permutation was $$$[1, 5, 2, 4, 3]$$$. Its array of prefix sums — $$$[1, 6, 8, 12, 15]$$$. Having lost one element, you can get, for example, arrays $$$[6, 8, 12, 15]$$$ or $$$[1, 6, 8, 15]$$$.

It can also be shown that the array $$$[1, 2, 100]$$$ does not correspond to any permutation.

Input

The first line contains a positive number $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of the test cases follows.

The first line of the description of each test case contains a positive number $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the size of the initial array.

The second line of the description of each test case contains $$$n - 1$$$ positive number $$$a_i$$$ ($$$1 \le a_i \le 10^{18}$$$), $$$a_{i-1} < a_i$$$ — elements of the array of prefix sums.

It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

Output

For each test case, output "YES" if such a permutation exists, and "NO" otherwise.

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

Example
Input
12
5
6 8 12 15
5
1 6 8 15
4
1 2 100
4
1 3 6
2
2
3
1 2
4
3 7 10
5
5 44 46 50
4
1 9 10
5
13 21 36 42
5
1 2 3 1000000000000000000
9
9 11 12 20 25 28 30 33
Output
+YES
+YES
+NO
+YES
+YES
+NO
+YES
+NO
+NO
+NO
+NO
+NO
+
Note

In the fourth example, for example, the permutation $$$[1, 2, 3, 4]$$$ is suitable. In the fifth example, for example, the permutation $$$[1, 2]$$$ is suitable. In the seventh example, for example, the permutation $$$[1, 2, 4, 3]$$$ is suitable.

+
+ + + + + +
+
+ + +
+ +
E. Nastya and Potions
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Alchemist Nastya loves mixing potions. There are a total of $$$n$$$ types of potions, and one potion of type $$$i$$$ can be bought for $$$c_i$$$ coins.

Any kind of potions can be obtained in no more than one way, by mixing from several others. The potions used in the mixing process will be consumed. Moreover, no potion can be obtained from itself through one or more mixing processes.

As an experienced alchemist, Nastya has an unlimited supply of $$$k$$$ types of potions $$$p_1, p_2, \dots, p_k$$$, but she doesn't know which one she wants to obtain next. To decide, she asks you to find, for each $$$1 \le i \le n$$$, the minimum number of coins she needs to spend to obtain a potion of type $$$i$$$ next.

Input

The first line of each test contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases.

Each test case is described as follows:

The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k < n \le 2 \cdot 10^5$$$) — the total number of potion types and the number of potion types Nastya already has.

The second line contains $$$n$$$ integers $$$c_1, c_2, \dots, c_n$$$ ($$$1 \le c_i \le 10^9$$$) — the costs of buying the potions.

The third line contains $$$k$$$ distinct integers $$$p_1, p_2, \dots, p_k$$$ ($$$1 \le p_i \le n$$$) — the indices of potions Nastya already has an unlimited supply of.

This is followed by $$$n$$$ lines describing ways to obtain potions by mixing.

Each line starts with the integer $$$m_i$$$ ($$$0 \le m_i < n$$$) — the number of potions required to mix a potion of the type $$$i$$$ ($$$1 \le i \le n$$$).

Then line contains $$$m_i$$$ distinct integers $$$e_1, e_2, \dots, e_{m_i}$$$ ($$$1 \le e_j \le n$$$, $$$e_j \ne i$$$) — the indices of potions needed to mix a potion of the type $$$i$$$. If this list is empty, then a potion of the type $$$i$$$ can only be bought.

It is guaranteed that no potion can be obtained from itself through one or more mixing processes.

It is guaranteed that the sum of all $$$n$$$ values across all test cases does not exceed $$$2 \cdot 10^5$$$. Similarly, it is guaranteed that the sum of all $$$m_i$$$ values across all test cases does not exceed $$$2 \cdot 10^5$$$.

Output

For each test case, output $$$n$$$ integers — the minimum number of coins Nastya needs to spend to obtain a potion of each type.

Examples
Input
4
5 1
30 8 3 5 10
3
3 2 4 5
0
0
2 3 5
0
3 2
5 143 3
1 3
1 2
0
2 1 2
5 1
5 4 1 3 4
2
2 4 5
3 3 5 4
2 1 4
1 5
0
4 2
1 1 5 4
2 4
3 2 4 3
0
2 2 4
1 2
Output
+23 8 0 5 10 
+0 143 0 
+5 0 1 3 4 
+0 0 0 0 
+
Input
3
6 3
5 5 4 5 2 2
3 4 5
2 2 5
1 5
3 4 1 6
4 2 6 1 5
0
0
6 2
1 4 4 1 5 2
3 6
4 6 3 4 5
4 6 5 3 4
0
1 5
1 6
0
2 1
4 3
1
0
1 1
Output
+0 0 0 0 0 2 
+0 0 0 0 0 0 
+0 0 
+
Note

In the first test case of the first sample, it is optimal:

  • Get a potion of the first type by buying and mixing $$$2$$$, $$$3$$$ and $$$4$$$;
  • a potion of the second type can only be obtained by purchasing it;
  • Nastya already has an unlimited number of potions of the third type;
  • a potion of the fourth type is more profitable to buy than to buy and mix other potions;
  • a potion of the fifth type can only be obtained by purchasing it.

+
+ + + + + +
+
+ + +
+ +
F. Lisa and the Martians
time limit per test
3 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Lisa was kidnapped by martians! It okay, because she has watched a lot of TV shows about aliens, so she knows what awaits her. Let's call integer martian if it is a non-negative integer and strictly less than $$$2^k$$$, for example, when $$$k = 12$$$, the numbers $$$51$$$, $$$1960$$$, $$$0$$$ are martian, and the numbers $$$\pi$$$, $$$-1$$$, $$$\frac{21}{8}$$$, $$$4096$$$ are not.

The aliens will give Lisa $$$n$$$ martian numbers $$$a_1, a_2, \ldots, a_n$$$. Then they will ask her to name any martian number $$$x$$$. After that, Lisa will select a pair of numbers $$$a_i, a_j$$$ ($$$i \neq j$$$) in the given sequence and count $$$(a_i \oplus x) \& (a_j \oplus x)$$$. The operation $$$\oplus$$$ means Bitwise exclusive OR, the operation $$$\&$$$ means Bitwise And. For example, $$$(5 \oplus 17) \& (23 \oplus 17) = (00101_2 \oplus 10001_2) \& (10111_2 \oplus 10001_2) = 10100_2 \& 00110_2 = 00100_2 = 4$$$.

Lisa is sure that the higher the calculated value, the higher her chances of returning home. Help the girl choose such $$$i, j, x$$$ that maximize the calculated value.

Input

The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of testcases.

Each testcase is described by two lines.

The first line contains integers $$$n, k$$$ ($$$2 \le n \le 2 \cdot 10^5$$$, $$$1 \le k \le 30$$$) — the length of the sequence of martian numbers and the value of $$$k$$$.

The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i < 2^k$$$) — a sequence of martian numbers.

It is guaranteed that the sum of $$$n$$$ over all testcases does not exceed $$$2 \cdot 10^5$$$.

Output

For each testcase, print three integers $$$i, j, x$$$ ($$$1 \le i, j \le n$$$, $$$i \neq j$$$, $$$0 \le x < 2^k$$$). The value of $$$(a_i \oplus x) \& (a_j \oplus x)$$$ should be the maximum possible.

If there are several solutions, you can print any one.

Example
Input
10
5 4
3 9 1 4 13
3 1
1 0 1
6 12
144 1580 1024 100 9 13
4 3
7 3 0 4
3 2
0 0 1
2 4
12 2
9 4
6 14 9 4 4 4 5 10 2
2 1
1 0
2 4
11 4
9 4
2 11 10 1 6 9 11 0 5
Output
+1 3 14
+1 3 0
+5 6 4082
+2 3 7
+1 2 3
+1 2 15
+4 5 11
+1 2 0
+1 2 0
+2 7 4
+
Note

First testcase: $$$(3 \oplus 14) \& (1 \oplus 14) = (0011_2 \oplus 1110_2) \& (0001_2 \oplus 1110_2) = 1101_2 = 1101_2 \& 1111_2 = 1101_2 = 13$$$.

Second testcase: $$$(1 \oplus 0) \& (1 \oplus 0) = 1$$$.

Third testcase: $$$(9 \oplus 4082) \& (13 \oplus 4082) = 4091$$$.

Fourth testcase: $$$(3 \oplus 7) \& (0 \oplus 7) = 4$$$.

+
+ + + + + +
+
+ + +
+ +
G. Vlad and the Mountains
time limit per test
5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Vlad decided to go on a trip to the mountains. He plans to move between $$$n$$$ mountains, some of which are connected by roads. The $$$i$$$-th mountain has a height of $$$h_i$$$.

If there is a road between mountains $$$i$$$ and $$$j$$$, Vlad can move from mountain $$$i$$$ to mountain $$$j$$$ by spending $$$h_j - h_i$$$ units of energy. If his energy drops below zero during the transition, he will not be able to move from mountain $$$i$$$ to mountain $$$j$$$. Note that $$$h_j - h_i$$$ can be negative and then the energy will be restored.

Vlad wants to consider different route options, so he asks you to answer the following queries: is it possible to construct some route starting at mountain $$$a$$$ and ending at mountain $$$b$$$, given that he initially has $$$e$$$ units of energy?

Input

The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases.

The descriptions of the test cases follow.

The first line of each test case contains two numbers $$$n$$$ and $$$m$$$ ($$$2 \le n \le 2 \cdot 10^5$$$, $$$1 \le m \le \min(\frac{n\cdot(n - 1)}{2}, 2 \cdot 10^5)$$$) — the number of mountains and the number of roads between them, respectively.

The second line contains $$$n$$$ integers $$$h_1, h_2, h_3, \dots, h_n$$$ ($$$1 \le h_i \le 10^9$$$) — the heights of the mountains.

The next $$$m$$$ lines contain two integers $$$u$$$ and $$$v$$$ ($$$1 \le u, v \le n$$$, $$$u \ne v$$$) — the numbers of the mountains connected by a road. It is guaranteed that no road appears twice.

The next line contains an integer $$$q$$$ ($$$1 \le q \le 2 \cdot 10^5$$$) — the number of queries.

The following $$$q$$$ lines contain three numbers $$$a$$$, $$$b$$$, and $$$e$$$ ($$$1 \le a, b \le n$$$, $$$0 \le e \le 10^9$$$) — the initial and final mountains of the route, and the amount of energy, respectively.

It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. The same guarantee applies to $$$m$$$ and $$$q$$$.

Output

For each query, output "YES" if Vlad can construct a route from mountain $$$a$$$ to mountain $$$b$$$, and "NO" otherwise.

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

In the examples below, the answers for different test cases are separated by an empty line, which you do not need to output.

Examples
Input
2
7 7
1 5 3 4 2 4 1
1 4
4 3
3 6
3 2
2 5
5 6
5 7
5
1 1 3
6 2 0
4 7 0
1 7 4
1 7 2
6 5
4 7 6 2 5 1
1 3
5 3
1 5
2 4
6 2
5
1 5 1
1 3 1
1 2 1000
6 2 6
6 2 5
Output
+YES
+NO
+YES
+YES
+NO
+
+YES
+NO
+NO
+YES
+NO
+
Input
2
3 2
1 3 9
1 2
2 3
5
1 1 1
3 2 2
1 1 2
3 3 0
1 2 1
3 3
1 4 1
1 2
2 3
1 3
5
3 3 9
1 3 6
1 1 2
3 3 6
3 3 4
Output
+YES
+YES
+YES
+YES
+NO
+
+YES
+YES
+YES
+YES
+YES
+
Input
1
6 10
7 9 2 10 8 6
4 2
6 1
4 5
3 5
6 4
1 3
2 6
6 5
1 2
3 6
5
4 4 8
3 3 1
5 5 9
2 1 7
6 6 10
Output
+YES
+YES
+YES
+YES
+YES
+

+
+ + + + + +
+
+ + + +
+
+ + + diff --git a/1851/src/bin/a.rs b/1851/src/bin/a.rs new file mode 100644 index 0000000..39eab84 --- /dev/null +++ b/1851/src/bin/a.rs @@ -0,0 +1,236 @@ +#![allow(unused_imports)] + +// region ‹use› +use self::input::*; +use self::math::*; +use self::output::*; +use std::cmp::{max, min}; +use std::collections::HashMap; +// endregion ‹use› + +fn can_talk_to(m: i64, k: i64, vh: i64, heights: Vec) -> usize { + heights + .iter() + .filter(|&h| { + if (vh - h) % k != 0 { + return false; + } + + let diff = (vh - h) / k; + + (1..=m) + .filter(|a| 1 <= diff + a && diff + a <= m) + .any(|a| diff + a != a) + }) + .count() +} + +fn solve(s: &mut Scanner) { + let (n, m, k, h) = ( + s.next::(), + s.next::(), + s.next::(), + s.next::(), + ); + let heights = s.next_vec::(n); + println!("{}", can_talk_to(m, k, h, heights)); +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn example_1() { + assert_eq!(1, 2); + } +} + +// region runner +const SINGLE_TEST: bool = false; +fn main() { + let mut s = Scanner::new(); + + if SINGLE_TEST { + solve(&mut s) + } else { + let n = s.next::(); + for _ in 0..n { + solve(&mut s) + } + } +} +// endregion runner + +#[allow(dead_code)] +mod math { + const MOD: i64 = 1_000_000_007; + + pub fn add(a: i64, b: i64) -> i64 { + (a + b) % MOD + } + + pub fn sub(a: i64, b: i64) -> i64 { + ((a - b) % MOD + MOD) % MOD + } + + pub fn mul(a: i64, b: i64) -> i64 { + (a * b) % MOD + } + + pub fn exp(b: i64, e: i64) -> i64 { + if e == 0 { + return 1; + } + + let half = exp(b, e / 2); + if e % 2 == 0 { + return mul(half, half); + } + + mul(half, mul(half, b)) + } + + /// A trait implementing the unsigned bit shifts. + pub trait UnsignedShift { + fn unsigned_shl(self, n: u32) -> Self; + fn unsigned_shr(self, n: u32) -> Self; + } + + /// A trait implementing the integer square root. + pub trait ISqrt { + fn isqrt(&self) -> Self + where + Self: Sized, + { + self.isqrt_checked() + .expect("cannot calculate square root of negative number") + } + + fn isqrt_checked(&self) -> Option + where + Self: Sized; + } + + macro_rules! math_traits_impl { + ($T:ty, $U: ty) => { + impl UnsignedShift for $T { + #[inline] + fn unsigned_shl(self, n: u32) -> Self { + ((self as $U) << n) as $T + } + + #[inline] + fn unsigned_shr(self, n: u32) -> Self { + ((self as $U) >> n) as $T + } + } + + impl ISqrt for $T { + #[inline] + fn isqrt_checked(&self) -> Option { + use core::cmp::Ordering; + match self.cmp(&<$T>::default()) { + // Hopefully this will be stripped for unsigned numbers (impossible condition) + Ordering::Less => return None, + Ordering::Equal => return Some(<$T>::default()), + _ => {} + } + + // Compute bit, the largest power of 4 <= n + let max_shift: u32 = <$T>::default().leading_zeros() - 1; + let shift: u32 = (max_shift - self.leading_zeros()) & !1; + let mut bit = <$T>::try_from(1).unwrap().unsigned_shl(shift); + + // Algorithm based on the implementation in: + // https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Binary_numeral_system_(base_2) + // Note that result/bit are logically unsigned (even if T is signed). + let mut n = *self; + let mut result = <$T>::default(); + while bit != <$T>::default() { + if n >= (result + bit) { + n -= result + bit; + result = result.unsigned_shr(1) + bit; + } else { + result = result.unsigned_shr(1); + } + bit = bit.unsigned_shr(2); + } + Some(result) + } + } + }; + } + + math_traits_impl!(i8, u8); + math_traits_impl!(u8, u8); + math_traits_impl!(i16, u16); + math_traits_impl!(u16, u16); + math_traits_impl!(i32, u32); + math_traits_impl!(u32, u32); + math_traits_impl!(i64, u64); + math_traits_impl!(u64, u64); + math_traits_impl!(i128, u128); + math_traits_impl!(u128, u128); + math_traits_impl!(isize, usize); + math_traits_impl!(usize, usize); +} + +#[allow(dead_code)] +mod output { + pub fn yes() { + println!("YES"); + } + + pub fn no() { + println!("NO"); + } + + pub fn yesno(ans: bool) { + println!("{}", if ans { "YES" } else { "NO" }); + } +} + +#[allow(dead_code)] +mod input { + use std::collections::VecDeque; + use std::io; + use std::str::FromStr; + + pub struct Scanner { + buffer: VecDeque, + } + + impl Scanner { + pub fn new() -> Scanner { + Scanner { + buffer: VecDeque::new(), + } + } + + pub fn next(&mut self) -> T { + if self.buffer.is_empty() { + let mut input = String::new(); + + io::stdin().read_line(&mut input).ok(); + + for word in input.split_whitespace() { + self.buffer.push_back(word.to_string()) + } + } + + let front = self.buffer.pop_front().unwrap(); + front.parse::().ok().unwrap() + } + + pub fn next_vec(&mut self, n: usize) -> Vec { + let mut arr = vec![]; + + for _ in 0..n { + arr.push(self.next::()); + } + + arr + } + } +} diff --git a/1851/src/bin/b.rs b/1851/src/bin/b.rs new file mode 100644 index 0000000..f277087 --- /dev/null +++ b/1851/src/bin/b.rs @@ -0,0 +1,244 @@ +#![allow(unused_imports)] + +// region ‹use› +use self::input::*; +use self::math::*; +use self::output::*; +use std::cmp::{max, min}; +use std::collections::HashMap; +// endregion ‹use› + +fn can_sort(nums: Vec) -> bool { + let mut sorted_nums = nums.clone(); + sorted_nums.sort(); + + nums.iter() + .zip(sorted_nums.iter()) + .all(|(src, dst)| src % 2 == dst % 2) +} + +fn solve(s: &mut Scanner) { + let n = s.next::(); + let nums = s.next_vec::(n); + + yesno(can_sort(nums)); +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn example_1() { + assert!(can_sort(vec![7, 10, 1, 3, 2])); + } + #[test] + fn example_2() { + assert!(can_sort(vec![11, 9, 3, 5])); + } + #[test] + fn example_3() { + assert!(!can_sort(vec![11, 3, 15, 3, 2])); + } + #[test] + fn example_4() { + assert!(!can_sort(vec![10, 7, 8, 1, 2, 3])); + } + #[test] + fn example_5() { + assert!(can_sort(vec![10])); + } + #[test] + fn example_6() { + assert!(!can_sort(vec![6, 6, 4, 1, 6])); + } +} + +// region runner +const SINGLE_TEST: bool = false; +fn main() { + let mut s = Scanner::new(); + + if SINGLE_TEST { + solve(&mut s) + } else { + let n = s.next::(); + for _ in 0..n { + solve(&mut s) + } + } +} +// endregion runner + +#[allow(dead_code)] +mod math { + const MOD: i64 = 1_000_000_007; + + pub fn add(a: i64, b: i64) -> i64 { + (a + b) % MOD + } + + pub fn sub(a: i64, b: i64) -> i64 { + ((a - b) % MOD + MOD) % MOD + } + + pub fn mul(a: i64, b: i64) -> i64 { + (a * b) % MOD + } + + pub fn exp(b: i64, e: i64) -> i64 { + if e == 0 { + return 1; + } + + let half = exp(b, e / 2); + if e % 2 == 0 { + return mul(half, half); + } + + mul(half, mul(half, b)) + } + + /// A trait implementing the unsigned bit shifts. + pub trait UnsignedShift { + fn unsigned_shl(self, n: u32) -> Self; + fn unsigned_shr(self, n: u32) -> Self; + } + + /// A trait implementing the integer square root. + pub trait ISqrt { + fn isqrt(&self) -> Self + where + Self: Sized, + { + self.isqrt_checked() + .expect("cannot calculate square root of negative number") + } + + fn isqrt_checked(&self) -> Option + where + Self: Sized; + } + + macro_rules! math_traits_impl { + ($T:ty, $U: ty) => { + impl UnsignedShift for $T { + #[inline] + fn unsigned_shl(self, n: u32) -> Self { + ((self as $U) << n) as $T + } + + #[inline] + fn unsigned_shr(self, n: u32) -> Self { + ((self as $U) >> n) as $T + } + } + + impl ISqrt for $T { + #[inline] + fn isqrt_checked(&self) -> Option { + use core::cmp::Ordering; + match self.cmp(&<$T>::default()) { + // Hopefully this will be stripped for unsigned numbers (impossible condition) + Ordering::Less => return None, + Ordering::Equal => return Some(<$T>::default()), + _ => {} + } + + // Compute bit, the largest power of 4 <= n + let max_shift: u32 = <$T>::default().leading_zeros() - 1; + let shift: u32 = (max_shift - self.leading_zeros()) & !1; + let mut bit = <$T>::try_from(1).unwrap().unsigned_shl(shift); + + // Algorithm based on the implementation in: + // https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Binary_numeral_system_(base_2) + // Note that result/bit are logically unsigned (even if T is signed). + let mut n = *self; + let mut result = <$T>::default(); + while bit != <$T>::default() { + if n >= (result + bit) { + n -= result + bit; + result = result.unsigned_shr(1) + bit; + } else { + result = result.unsigned_shr(1); + } + bit = bit.unsigned_shr(2); + } + Some(result) + } + } + }; + } + + math_traits_impl!(i8, u8); + math_traits_impl!(u8, u8); + math_traits_impl!(i16, u16); + math_traits_impl!(u16, u16); + math_traits_impl!(i32, u32); + math_traits_impl!(u32, u32); + math_traits_impl!(i64, u64); + math_traits_impl!(u64, u64); + math_traits_impl!(i128, u128); + math_traits_impl!(u128, u128); + math_traits_impl!(isize, usize); + math_traits_impl!(usize, usize); +} + +#[allow(dead_code)] +mod output { + pub fn yes() { + println!("YES"); + } + + pub fn no() { + println!("NO"); + } + + pub fn yesno(ans: bool) { + println!("{}", if ans { "YES" } else { "NO" }); + } +} + +#[allow(dead_code)] +mod input { + use std::collections::VecDeque; + use std::io; + use std::str::FromStr; + + pub struct Scanner { + buffer: VecDeque, + } + + impl Scanner { + pub fn new() -> Scanner { + Scanner { + buffer: VecDeque::new(), + } + } + + pub fn next(&mut self) -> T { + if self.buffer.is_empty() { + let mut input = String::new(); + + io::stdin().read_line(&mut input).ok(); + + for word in input.split_whitespace() { + self.buffer.push_back(word.to_string()) + } + } + + let front = self.buffer.pop_front().unwrap(); + front.parse::().ok().unwrap() + } + + pub fn next_vec(&mut self, n: usize) -> Vec { + let mut arr = vec![]; + + for _ in 0..n { + arr.push(self.next::()); + } + + arr + } + } +} diff --git a/1851/src/bin/c.rs b/1851/src/bin/c.rs new file mode 100644 index 0000000..b3e7e53 --- /dev/null +++ b/1851/src/bin/c.rs @@ -0,0 +1,253 @@ +#![allow(unused_imports)] + +// region ‹use› +use self::input::*; +use self::math::*; +use self::output::*; +use std::cmp::{max, min}; +use std::collections::HashMap; +// endregion ‹use› + +fn exists_path(k: usize, tiles: Vec) -> bool { + let first = *tiles.first().expect("at least one tile"); + let last = *tiles.last().expect("at least one tiles"); + + // dbg!(first, last); + + let tiles_of_first = tiles.iter().filter(|&&t| t == first).count(); + let tiles_of_last = tiles.iter().filter(|&&t| t == last).count(); + + // dbg!(tiles_of_first, tiles_of_last); + + let last_of_first = tiles + .iter() + .enumerate() + .filter(|(_, &c)| c == first) + .nth(k - 1) + .map(|(i, _)| i) + .unwrap_or(tiles.len()); + let first_of_last = tiles + .iter() + .enumerate() + .rev() + .filter(|(_, &c)| c == last) + .nth(k - 1) + .map(|(i, _)| i) + .unwrap_or(0); + + // dbg!(last_of_first, first_of_last); + + tiles.len() >= k + && ((first == last && tiles_of_first >= k) + || (first != last + && tiles_of_first >= k + && tiles_of_last >= k + && last_of_first < first_of_last)) +} + +fn solve(s: &mut Scanner) { + let n = s.next::(); + let k = s.next::(); + let tiles = s.next_vec::(n); + + yesno(exists_path(k, tiles)); +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn example_1() { + assert_eq!(1, 2); + } +} + +// region runner +const SINGLE_TEST: bool = false; +fn main() { + let mut s = Scanner::new(); + + if SINGLE_TEST { + solve(&mut s) + } else { + let n = s.next::(); + for _ in 0..n { + solve(&mut s) + } + } +} +// endregion runner + +#[allow(dead_code)] +mod math { + const MOD: i64 = 1_000_000_007; + + pub fn add(a: i64, b: i64) -> i64 { + (a + b) % MOD + } + + pub fn sub(a: i64, b: i64) -> i64 { + ((a - b) % MOD + MOD) % MOD + } + + pub fn mul(a: i64, b: i64) -> i64 { + (a * b) % MOD + } + + pub fn exp(b: i64, e: i64) -> i64 { + if e == 0 { + return 1; + } + + let half = exp(b, e / 2); + if e % 2 == 0 { + return mul(half, half); + } + + mul(half, mul(half, b)) + } + + /// A trait implementing the unsigned bit shifts. + pub trait UnsignedShift { + fn unsigned_shl(self, n: u32) -> Self; + fn unsigned_shr(self, n: u32) -> Self; + } + + /// A trait implementing the integer square root. + pub trait ISqrt { + fn isqrt(&self) -> Self + where + Self: Sized, + { + self.isqrt_checked() + .expect("cannot calculate square root of negative number") + } + + fn isqrt_checked(&self) -> Option + where + Self: Sized; + } + + macro_rules! math_traits_impl { + ($T:ty, $U: ty) => { + impl UnsignedShift for $T { + #[inline] + fn unsigned_shl(self, n: u32) -> Self { + ((self as $U) << n) as $T + } + + #[inline] + fn unsigned_shr(self, n: u32) -> Self { + ((self as $U) >> n) as $T + } + } + + impl ISqrt for $T { + #[inline] + fn isqrt_checked(&self) -> Option { + use core::cmp::Ordering; + match self.cmp(&<$T>::default()) { + // Hopefully this will be stripped for unsigned numbers (impossible condition) + Ordering::Less => return None, + Ordering::Equal => return Some(<$T>::default()), + _ => {} + } + + // Compute bit, the largest power of 4 <= n + let max_shift: u32 = <$T>::default().leading_zeros() - 1; + let shift: u32 = (max_shift - self.leading_zeros()) & !1; + let mut bit = <$T>::try_from(1).unwrap().unsigned_shl(shift); + + // Algorithm based on the implementation in: + // https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Binary_numeral_system_(base_2) + // Note that result/bit are logically unsigned (even if T is signed). + let mut n = *self; + let mut result = <$T>::default(); + while bit != <$T>::default() { + if n >= (result + bit) { + n -= result + bit; + result = result.unsigned_shr(1) + bit; + } else { + result = result.unsigned_shr(1); + } + bit = bit.unsigned_shr(2); + } + Some(result) + } + } + }; + } + + math_traits_impl!(i8, u8); + math_traits_impl!(u8, u8); + math_traits_impl!(i16, u16); + math_traits_impl!(u16, u16); + math_traits_impl!(i32, u32); + math_traits_impl!(u32, u32); + math_traits_impl!(i64, u64); + math_traits_impl!(u64, u64); + math_traits_impl!(i128, u128); + math_traits_impl!(u128, u128); + math_traits_impl!(isize, usize); + math_traits_impl!(usize, usize); +} + +#[allow(dead_code)] +mod output { + pub fn yes() { + println!("YES"); + } + + pub fn no() { + println!("NO"); + } + + pub fn yesno(ans: bool) { + println!("{}", if ans { "YES" } else { "NO" }); + } +} + +#[allow(dead_code)] +mod input { + use std::collections::VecDeque; + use std::io; + use std::str::FromStr; + + pub struct Scanner { + buffer: VecDeque, + } + + impl Scanner { + pub fn new() -> Scanner { + Scanner { + buffer: VecDeque::new(), + } + } + + pub fn next(&mut self) -> T { + if self.buffer.is_empty() { + let mut input = String::new(); + + io::stdin().read_line(&mut input).ok(); + + for word in input.split_whitespace() { + self.buffer.push_back(word.to_string()) + } + } + + let front = self.buffer.pop_front().unwrap(); + front.parse::().ok().unwrap() + } + + pub fn next_vec(&mut self, n: usize) -> Vec { + let mut arr = vec![]; + + for _ in 0..n { + arr.push(self.next::()); + } + + arr + } + } +}