From 1bdd5d6d1d1d54b75e119c5a8751744f229aa138 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Sat, 29 Jul 2023 15:25:35 +0200 Subject: [PATCH] =?UTF-8?q?1853(A,cpp):=20solve=20=E2=80=9CDesorting?= =?UTF-8?q?=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko diff --git a/1853/a.cpp b/1853/a.cpp new file mode 100644 index 0000000..217b260 --- /dev/null +++ b/1853/a.cpp @@ -0,0 +1,176 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#pragma region helpers + +#pragma region math +static constexpr std::int32_t MODULO = 1000000007; + +long pow(long base, long exp) { + if (exp == 0) return 1; + long half = pow(base, exp / 2); + if (exp % 2 == 0) return half * half; + return half * half * base; +} +#pragma endregion /* math */ + +#pragma region output +template +std::ostream &operator<<(std::ostream &os, std::pair const &p) { + return os << p.first << " " << p.second; +} + +template ::value, + typename C::value_type>::type> +std::ostream &operator<<(std::ostream &os, const C &v) { + std::string sep; + for (const T &x : v) { + os << sep << x, sep = " "; + } + + return os; +} + +template +inline void answer(const T &ans) { + std::cout << ans << "\n"; +} + +inline void yes() { std::cout << "YES\n"; } +inline void no() { std::cout << "NO\n"; } +inline void yesno(bool ans) { std::cout << (ans ? "YES" : "NO") << "\n"; } +#pragma endregion /* output */ + +#pragma region debug +void dbg_out() { std::cerr << std::endl; } +template +void dbg_out(Head H, Tail... T) { + std::cerr << ' ' << H; + dbg_out(T...); +} +#ifdef LOCAL +#define dbg(...) \ + std::cerr << '[' << __FILE__ << ':' << __LINE__ << "] (" << #__VA_ARGS__ \ + << "):", \ + dbg_out(__VA_ARGS__) +#else +#define dbg(...) +#endif +#pragma endregion debug + +#pragma region input +template +std::vector load_vector(std::size_t size) { + std::vector result{}; + + for (auto i = 0u; i < size; ++i) { + T x; + std::cin >> x; + result.push_back(std::move(x)); + } + + return result; +} +#pragma endregion /* input */ + +#pragma region functional +// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0200r0.html +template +class y_combinator_result { + Fun fun_; + + public: + template + explicit y_combinator_result(T &&fun) : fun_(std::forward(fun)) {} + template + decltype(auto) operator()(Args &&...args) { + return fun_(std::ref(*this), std::forward(args)...); + } +}; +template +decltype(auto) y_combinator(Fun &&fun) { + return y_combinator_result>(std::forward(fun)); +} +#pragma endregion /* functional */ + +#define LOOP(var, n) for (auto var = 0; var < n; ++var) + +#pragma endregion /* helpers */ + +// for ‹N› test cases, uncomment for single test case +// #define SINGLE + +namespace solution { + +using namespace std; + +void solve() { + int n; + cin >> n; + + auto a = load_vector(n); + + vector ds; + for (auto i = 0; i < n - 1; ++i) { + ds.push_back(a[i + 1] - a[i]); + } + + auto distance = *min_element(ds.begin(), ds.end()); + + answer(max(0, (distance + 2) / 2)); +} + +void tests() { + // TODO +} + +} // namespace solution + +using namespace solution; + +#ifdef TEST + +int main(void) { + tests(); + return 0; +} + +#else + +int main(void) { + int N = 1; + +#ifndef SINGLE + + std::cin >> N; + +#endif + + while (N-- > 0) { + solve(); + } + + return 0; +} + +#endif diff --git a/1853/index.html b/1853/index.html new file mode 100644 index 0000000..deae7ce --- /dev/null +++ b/1853/index.html @@ -0,0 +1,1153 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Problems - Codeforces + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + +
+
+ + +
+ +
Codeforces Round 887 (Div. 2)
+
+ +
+
+ + + +
+
+ +
+ +
A. Desorting
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Call an array $$$a$$$ of length $$$n$$$ sorted if $$$a_1 \leq a_2 \leq \ldots \leq a_{n-1} \leq a_n$$$.

Ntarsis has an array $$$a$$$ of length $$$n$$$.

He is allowed to perform one type of operation on it (zero or more times):

  • Choose an index $$$i$$$ ($$$1 \leq i \leq n-1$$$).
  • Add $$$1$$$ to $$$a_1, a_2, \ldots, a_i$$$.
  • Subtract $$$1$$$ from $$$a_{i+1}, a_{i+2}, \ldots, a_n$$$.

The values of $$$a$$$ can be negative after an operation.

Determine the minimum operations needed to make $$$a$$$ not sorted.

Input

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows.

The first line of each test case contains a single integer $$$n$$$ ($$$2 \leq n \leq 500$$$) — the length of the array $$$a$$$.

The next line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 10^9$$$) — the values of array $$$a$$$.

It is guaranteed that the sum of $$$n$$$ across all test cases does not exceed $$$500$$$.

Output

Output the minimum number of operations needed to make the array not sorted.

Example
Input
4
2
1 1
4
1 8 10 13
3
1 3 2
3
1 9 14
Output
+1
+2
+0
+3
+
Note

In the first case, we can perform $$$1$$$ operation to make the array not sorted:

  • Pick $$$i = 1$$$. The array $$$a$$$ then becomes $$$[2, 0]$$$, which is not sorted.

In the second case, we can perform $$$2$$$ operations to make the array not sorted:

  • Pick $$$i = 3$$$. The array $$$a$$$ then becomes $$$[2, 9, 11, 12]$$$.
  • Pick $$$i = 3$$$. The array $$$a$$$ then becomes $$$[3, 10, 12, 11]$$$, which is not sorted.

It can be proven that $$$1$$$ and $$$2$$$ operations are the minimal numbers of operations in the first and second test cases, respectively.

In the third case, the array is already not sorted, so we perform $$$0$$$ operations.

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

Ntarsis has received two integers $$$n$$$ and $$$k$$$ for his birthday. He wonders how many fibonacci-like sequences of length $$$k$$$ can be formed with $$$n$$$ as the $$$k$$$-th element of the sequence.

A sequence of non-decreasing non-negative integers is considered fibonacci-like if $$$f_i = f_{i-1} + f_{i-2}$$$ for all $$$i > 2$$$, where $$$f_i$$$ denotes the $$$i$$$-th element in the sequence. Note that $$$f_1$$$ and $$$f_2$$$ can be arbitrary.

For example, sequences such as $$$[4,5,9,14]$$$ and $$$[0,1,1]$$$ are considered fibonacci-like sequences, while $$$[0,0,0,1,1]$$$, $$$[1, 2, 1, 3]$$$, and $$$[-1,-1,-2]$$$ are not: the first two do not always satisfy $$$f_i = f_{i-1} + f_{i-2}$$$, the latter does not satisfy that the elements are non-negative.

Impress Ntarsis by helping him with this task.

Input

The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 2 \cdot 10^5$$$), the number of test cases. The description of each test case is as follows.

Each test case contains two integers, $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$, $$$3 \leq k \leq 10^9$$$).

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

Output

For each test case output an integer, the number of fibonacci-like sequences of length $$$k$$$ such that the $$$k$$$-th element in the sequence is $$$n$$$. That is, output the number of sequences $$$f$$$ of length $$$k$$$ so $$$f$$$ is a fibonacci-like sequence and $$$f_k = n$$$. It can be shown this number is finite.

Example
Input
8
22 4
3 9
55 11
42069 6
69420 4
69 1434
1 3
1 4
Output
+4
+0
+1
+1052
+11571
+0
+1
+0
+
Note

There are $$$4$$$ valid fibonacci-like sequences for $$$n = 22$$$, $$$k = 4$$$:

  • $$$[6,8,14,22]$$$,
  • $$$[4,9,13,22]$$$,
  • $$$[2,10,12,22]$$$,
  • $$$[0,11,11,22]$$$.

For $$$n = 3$$$, $$$k = 9$$$, it can be shown that there are no fibonacci-like sequences satisfying the given conditions.

For $$$n = 55$$$, $$$k = 11$$$, $$$[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]$$$ is the only fibonacci-like sequence.

+
+ + + + + +
+
+ + +
+ +
C. Ntarsis' Set
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Ntarsis has been given a set $$$S$$$, initially containing integers $$$1, 2, 3, \ldots, 10^{1000}$$$ in sorted order. Every day, he will remove the $$$a_1$$$-th, $$$a_2$$$-th, $$$\ldots$$$, $$$a_n$$$-th smallest numbers in $$$S$$$ simultaneously.

What is the smallest element in $$$S$$$ after $$$k$$$ days?

Input

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^5$$$). The description of the test cases follows.

The first line of each test case consists of two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n,k \leq 2 \cdot 10^5$$$) — the length of $$$a$$$ and the number of days.

The following line of each test case consists of $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 10^9$$$) — the elements of array $$$a$$$.

It is guaranteed that:

  • The sum of $$$n$$$ over all test cases won't exceed $$$2 \cdot 10^5$$$;
  • The sum of $$$k$$$ over all test cases won't exceed $$$2 \cdot 10^5$$$;
  • $$$a_1 < a_2 < \cdots < a_n$$$ for all test cases.
Output

For each test case, print an integer that is the smallest element in $$$S$$$ after $$$k$$$ days.

Example
Input
7
5 1
1 2 4 5 6
5 3
1 3 5 6 7
4 1000
2 3 4 5
9 1434
1 4 7 9 12 15 17 18 20
10 4
1 3 5 7 9 11 13 15 17 19
10 6
1 4 7 10 13 16 19 22 25 28
10 150000
1 3 4 5 10 11 12 13 14 15
Output
+3
+9
+1
+12874
+16
+18
+1499986
+
Note

For the first test case, each day the $$$1$$$-st, $$$2$$$-nd, $$$4$$$-th, $$$5$$$-th, and $$$6$$$-th smallest elements need to be removed from $$$S$$$. So after the first day, $$$S$$$ will become $$$\require{cancel}$$$ $$$\{\cancel 1, \cancel 2, 3, \cancel 4, \cancel 5, \cancel 6, 7, 8, 9, \ldots\} = \{3, 7, 8, 9, \ldots\}$$$. The smallest element is $$$3$$$.

For the second case, each day the $$$1$$$-st, $$$3$$$-rd, $$$5$$$-th, $$$6$$$-th and $$$7$$$-th smallest elements need to be removed from $$$S$$$. $$$S$$$ will be changed as follows:

Day$$$S$$$ before$$$S$$$ after
1$$$\{\cancel 1, 2, \cancel 3, 4, \cancel 5, \cancel 6, \cancel 7, 8, 9, 10, \ldots \}$$$$$$\to$$$$$$\{2, 4, 8, 9, 10, \ldots\}$$$
2$$$\{\cancel 2, 4, \cancel 8, 9, \cancel{10}, \cancel{11}, \cancel{12}, 13, 14, 15, \ldots\}$$$$$$\to$$$$$$\{4, 9, 13, 14, 15, \ldots\}$$$
3$$$\{\cancel 4, 9, \cancel{13}, 14, \cancel{15}, \cancel{16}, \cancel{17}, 18, 19, 20, \ldots\}$$$$$$\to$$$$$$\{9, 14, 18, 19, 20, \ldots\}$$$

The smallest element left after $$$k = 3$$$ days is $$$9$$$.

+
+ + + + + +
+
+ + +
+ +
D. Imbalanced Arrays
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Ntarsis has come up with an array $$$a$$$ of $$$n$$$ non-negative integers.

Call an array $$$b$$$ of $$$n$$$ integers imbalanced if it satisfies the following:

  • $$$-n\le b_i\le n$$$, $$$b_i \ne 0$$$,
  • there are no two indices $$$(i, j)$$$ ($$$1 \le i, j \le n$$$) such that $$$b_i + b_j = 0$$$,
  • for each $$$1 \leq i \leq n$$$, there are exactly $$$a_i$$$ indices $$$j$$$ ($$$1 \le j \le n$$$) such that $$$b_i+b_j>0$$$, where $$$i$$$ and $$$j$$$ are not necessarily distinct.

Given the array $$$a$$$, Ntarsis wants you to construct some imbalanced array. Help him solve this task, or determine it is impossible.

Input

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^5$$$). The description of the test cases follows.

The first line of each test case has a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$).

The next line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \leq a_i \leq n$$$).

It is guaranteed that the sum of $$$n$$$ across all test cases does not exceed $$$10^5$$$.

Output

For each test case, output "NO" if there exists no imbalanced array.

Otherwise, output "YES". Then, on the next line, output $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ where $$$b_i \neq 0$$$ for all $$$1 \leq i \leq n$$$ — an imbalanced array.

Example
Input
5
1
1
4
1 4 3 4
3
0 1 0
4
4 3 2 1
3
1 3 1
Output
+YES
+1
+NO
+YES
+-3 1 -2
+YES
+4 2 -1 -3
+YES
+-1 3 -1
Note

For the first test case, $$$b = [1]$$$ is an imbalanced array. This is because for $$$i = 1$$$, there is exactly one $$$j$$$ ($$$j = 1$$$) where $$$b_1 + b_j > 0$$$.

For the second test case, it can be shown that there exists no imbalanced array.

For the third test case, $$$a = [0, 1, 0]$$$. The array $$$b = [-3, 1, -2]$$$ is an imbalanced array.

  • For $$$i = 1$$$ and $$$i = 3$$$, there exists no index $$$j$$$ such that $$$b_i + b_j > 0$$$.
  • For $$$i = 2$$$, there is only one index $$$j = 2$$$ such that $$$b_i + b_j > 0$$$ ($$$b_2 + b_2 = 1 + 1 = 2$$$).
Another possible output for the third test case could be $$$b = [-2, 1, -3]$$$.

+
+ + + + + +
+
+ + +
+ +
E. Ina of the Mountain
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

To prepare her "Takodachi" dumbo octopuses for world domination, Ninomae Ina'nis, a.k.a. Ina of the Mountain, orders Hoshimachi Suisei to throw boulders at them. Ina asks you, Kiryu Coco, to help choose where the boulders are thrown.

There are $$$n$$$ octopuses on a single-file trail on Ina's mountain, numbered $$$1, 2, \ldots, n$$$. The $$$i$$$-th octopus has a certain initial health value $$$a_i$$$, where $$$1 \leq a_i \leq k$$$.

Each boulder crushes consecutive octopuses with indexes $$$l, l+1, \ldots, r$$$, where $$$1 \leq l \leq r \leq n$$$. You can choose the numbers $$$l$$$ and $$$r$$$ arbitrarily for each boulder.

For each boulder, the health value of each octopus the boulder crushes is reduced by $$$1$$$. However, as octopuses are immortal, once they reach a health value of $$$0$$$, they will immediately regenerate to a health value of $$$k$$$.

Given the octopuses' initial health values, find the minimum number of boulders that need to be thrown to make the health of all octopuses equal to $$$k$$$.

Input

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^5$$$). The description of the test cases follows.

The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$1 \le k \le 10^9$$$) – the number of octopuses, and the upper bound of a octopus's health value.

The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le k$$$) – the initial health values of the octopuses.

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 the minimum number of boulders that need to be thrown to make the health values of all octopuses equal to $$$k$$$.

Example
Input
2
4 3
1 2 1 3
7 3
1 2 3 1 3 2 1
Output
+2
+4
+
Note

In the first test case, the minimum number of boulders thrown is $$$2$$$:

  • Throw the first boulder between $$$[l,r] = [1,3]$$$. Then, the octopuses' health values become $$$[3, 1, 3, 3]$$$.
  • Throw the second boulder between $$$[l,r] = [2,2]$$$. Then, the octopuses' health values become $$$[3, 3, 3, 3]$$$.

In the second test case, the minimum number of boulders thrown is $$$4$$$. The $$$[l,r]$$$ ranges are $$$[1,7], [2, 6], [3, 5], [4, 4]$$$.

+
+ + + + + +
+
+ + +
+ +
F. Miriany and Matchstick
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Miriany's matchstick is a $$$2 \times n$$$ grid that needs to be filled with characters A or B.

He has already filled in the first row of the grid and would like you to fill in the second row. You must do so in a way such that the number of adjacent pairs of cells with different characters$$$^\dagger$$$ is equal to $$$k$$$. If it is impossible, report so.

$$$^\dagger$$$ An adjacent pair of cells with different characters is a pair of cells $$$(r_1, c_1)$$$ and $$$(r_2, c_2)$$$ ($$$1 \le r_1, r_2 \le 2$$$, $$$1 \le c_1, c_2 \le n$$$) such that $$$|r_1 - r_2| + |c_1 - c_2| = 1$$$ and the characters in $$$(r_1, c_1)$$$ and $$$(r_2, c_2)$$$ are different.

Input

The first line consists of an integer $$$t$$$, the number of test cases ($$$1 \leq t \leq 1000$$$). The description of the test cases follows.

The first line of each test case has two integers, $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 2 \cdot 10^5, 0 \leq k \leq 3 \cdot n$$$) – the number of columns of the matchstick, and the number of adjacent pairs of cells with different characters required.

The following line contains string $$$s$$$ of $$$n$$$ characters ($$$s_i$$$ is either A or B) – Miriany's top row of the matchstick.

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

Output

For each test case, if there is no way to fill the second row with the number of adjacent pairs of cells with different characters equals $$$k$$$, output "NO".

Otherwise, output "YES". Then, print $$$n$$$ characters that a valid bottom row for Miriany's matchstick consists of. If there are several answers, output any of them.

Example
Input
4
10 1
ABBAAABBAA
4 5
AAAA
9 17
BAAABBAAB
4 9
ABAB
Output
+NO
+YES
+BABB
+YES
+ABABAABAB
+NO
Note

In the first test case, it can be proved that there exists no possible way to fill in row $$$2$$$ of the grid such that $$$k = 1$$$.

For the second test case, BABB is one possible answer.

The grid below is the result of filling in BABB as the second row.

$$$\begin{array}{|c|c|} \hline A & A & A & A \cr \hline B & A & B & B \cr \hline \end{array}$$$

The pairs of different characters are shown below in red:

$$$\begin{array}{|c|c|} \hline \color{red}{A} & A & A & A \cr \hline \color{red}{B} & A & B & B \cr \hline \end{array}$$$

—————————————————

$$$\begin{array}{|c|c|} \hline A & A & \color{red}{A} & A \cr \hline B & A & \color{red}{B} & B \cr \hline \end{array}$$$

—————————————————

$$$\begin{array}{|c|c|} \hline A & A & A & \color{red}{A} \cr \hline B & A & B & \color{red}{B} \cr \hline \end{array}$$$

—————————————————

$$$\begin{array}{|c|c|} \hline A & A & A & A \cr \hline \color{red}{B} & \color{red}{A} & B & B \cr \hline \end{array}$$$

—————————————————

$$$\begin{array}{|c|c|} \hline A & A & A & A \cr \hline B & \color{red}{A} & \color{red}{B} & B \cr \hline \end{array}$$$

There are a total of $$$5$$$ pairs, which satisfies $$$k$$$.

+
+ + + + + +
+
+ + + +
+
+ + + --- 1853/a.cpp | 176 ++++++++ 1853/index.html | 1153 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 1329 insertions(+) create mode 100644 1853/a.cpp create mode 100644 1853/index.html diff --git a/1853/a.cpp b/1853/a.cpp new file mode 100644 index 0000000..217b260 --- /dev/null +++ b/1853/a.cpp @@ -0,0 +1,176 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#pragma region helpers + +#pragma region math +static constexpr std::int32_t MODULO = 1000000007; + +long pow(long base, long exp) { + if (exp == 0) return 1; + long half = pow(base, exp / 2); + if (exp % 2 == 0) return half * half; + return half * half * base; +} +#pragma endregion /* math */ + +#pragma region output +template +std::ostream &operator<<(std::ostream &os, std::pair const &p) { + return os << p.first << " " << p.second; +} + +template ::value, + typename C::value_type>::type> +std::ostream &operator<<(std::ostream &os, const C &v) { + std::string sep; + for (const T &x : v) { + os << sep << x, sep = " "; + } + + return os; +} + +template +inline void answer(const T &ans) { + std::cout << ans << "\n"; +} + +inline void yes() { std::cout << "YES\n"; } +inline void no() { std::cout << "NO\n"; } +inline void yesno(bool ans) { std::cout << (ans ? "YES" : "NO") << "\n"; } +#pragma endregion /* output */ + +#pragma region debug +void dbg_out() { std::cerr << std::endl; } +template +void dbg_out(Head H, Tail... T) { + std::cerr << ' ' << H; + dbg_out(T...); +} +#ifdef LOCAL +#define dbg(...) \ + std::cerr << '[' << __FILE__ << ':' << __LINE__ << "] (" << #__VA_ARGS__ \ + << "):", \ + dbg_out(__VA_ARGS__) +#else +#define dbg(...) +#endif +#pragma endregion debug + +#pragma region input +template +std::vector load_vector(std::size_t size) { + std::vector result{}; + + for (auto i = 0u; i < size; ++i) { + T x; + std::cin >> x; + result.push_back(std::move(x)); + } + + return result; +} +#pragma endregion /* input */ + +#pragma region functional +// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0200r0.html +template +class y_combinator_result { + Fun fun_; + + public: + template + explicit y_combinator_result(T &&fun) : fun_(std::forward(fun)) {} + template + decltype(auto) operator()(Args &&...args) { + return fun_(std::ref(*this), std::forward(args)...); + } +}; +template +decltype(auto) y_combinator(Fun &&fun) { + return y_combinator_result>(std::forward(fun)); +} +#pragma endregion /* functional */ + +#define LOOP(var, n) for (auto var = 0; var < n; ++var) + +#pragma endregion /* helpers */ + +// for ‹N› test cases, uncomment for single test case +// #define SINGLE + +namespace solution { + +using namespace std; + +void solve() { + int n; + cin >> n; + + auto a = load_vector(n); + + vector ds; + for (auto i = 0; i < n - 1; ++i) { + ds.push_back(a[i + 1] - a[i]); + } + + auto distance = *min_element(ds.begin(), ds.end()); + + answer(max(0, (distance + 2) / 2)); +} + +void tests() { + // TODO +} + +} // namespace solution + +using namespace solution; + +#ifdef TEST + +int main(void) { + tests(); + return 0; +} + +#else + +int main(void) { + int N = 1; + +#ifndef SINGLE + + std::cin >> N; + +#endif + + while (N-- > 0) { + solve(); + } + + return 0; +} + +#endif diff --git a/1853/index.html b/1853/index.html new file mode 100644 index 0000000..deae7ce --- /dev/null +++ b/1853/index.html @@ -0,0 +1,1153 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Problems - Codeforces + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + +
+
+ + +
+ +
Codeforces Round 887 (Div. 2)
+
+ +
+
+ + + +
+
+ +
+ +
A. Desorting
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Call an array $$$a$$$ of length $$$n$$$ sorted if $$$a_1 \leq a_2 \leq \ldots \leq a_{n-1} \leq a_n$$$.

Ntarsis has an array $$$a$$$ of length $$$n$$$.

He is allowed to perform one type of operation on it (zero or more times):

  • Choose an index $$$i$$$ ($$$1 \leq i \leq n-1$$$).
  • Add $$$1$$$ to $$$a_1, a_2, \ldots, a_i$$$.
  • Subtract $$$1$$$ from $$$a_{i+1}, a_{i+2}, \ldots, a_n$$$.

The values of $$$a$$$ can be negative after an operation.

Determine the minimum operations needed to make $$$a$$$ not sorted.

Input

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows.

The first line of each test case contains a single integer $$$n$$$ ($$$2 \leq n \leq 500$$$) — the length of the array $$$a$$$.

The next line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 10^9$$$) — the values of array $$$a$$$.

It is guaranteed that the sum of $$$n$$$ across all test cases does not exceed $$$500$$$.

Output

Output the minimum number of operations needed to make the array not sorted.

Example
Input
4
2
1 1
4
1 8 10 13
3
1 3 2
3
1 9 14
Output
+1
+2
+0
+3
+
Note

In the first case, we can perform $$$1$$$ operation to make the array not sorted:

  • Pick $$$i = 1$$$. The array $$$a$$$ then becomes $$$[2, 0]$$$, which is not sorted.

In the second case, we can perform $$$2$$$ operations to make the array not sorted:

  • Pick $$$i = 3$$$. The array $$$a$$$ then becomes $$$[2, 9, 11, 12]$$$.
  • Pick $$$i = 3$$$. The array $$$a$$$ then becomes $$$[3, 10, 12, 11]$$$, which is not sorted.

It can be proven that $$$1$$$ and $$$2$$$ operations are the minimal numbers of operations in the first and second test cases, respectively.

In the third case, the array is already not sorted, so we perform $$$0$$$ operations.

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

Ntarsis has received two integers $$$n$$$ and $$$k$$$ for his birthday. He wonders how many fibonacci-like sequences of length $$$k$$$ can be formed with $$$n$$$ as the $$$k$$$-th element of the sequence.

A sequence of non-decreasing non-negative integers is considered fibonacci-like if $$$f_i = f_{i-1} + f_{i-2}$$$ for all $$$i > 2$$$, where $$$f_i$$$ denotes the $$$i$$$-th element in the sequence. Note that $$$f_1$$$ and $$$f_2$$$ can be arbitrary.

For example, sequences such as $$$[4,5,9,14]$$$ and $$$[0,1,1]$$$ are considered fibonacci-like sequences, while $$$[0,0,0,1,1]$$$, $$$[1, 2, 1, 3]$$$, and $$$[-1,-1,-2]$$$ are not: the first two do not always satisfy $$$f_i = f_{i-1} + f_{i-2}$$$, the latter does not satisfy that the elements are non-negative.

Impress Ntarsis by helping him with this task.

Input

The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 2 \cdot 10^5$$$), the number of test cases. The description of each test case is as follows.

Each test case contains two integers, $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$, $$$3 \leq k \leq 10^9$$$).

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

Output

For each test case output an integer, the number of fibonacci-like sequences of length $$$k$$$ such that the $$$k$$$-th element in the sequence is $$$n$$$. That is, output the number of sequences $$$f$$$ of length $$$k$$$ so $$$f$$$ is a fibonacci-like sequence and $$$f_k = n$$$. It can be shown this number is finite.

Example
Input
8
22 4
3 9
55 11
42069 6
69420 4
69 1434
1 3
1 4
Output
+4
+0
+1
+1052
+11571
+0
+1
+0
+
Note

There are $$$4$$$ valid fibonacci-like sequences for $$$n = 22$$$, $$$k = 4$$$:

  • $$$[6,8,14,22]$$$,
  • $$$[4,9,13,22]$$$,
  • $$$[2,10,12,22]$$$,
  • $$$[0,11,11,22]$$$.

For $$$n = 3$$$, $$$k = 9$$$, it can be shown that there are no fibonacci-like sequences satisfying the given conditions.

For $$$n = 55$$$, $$$k = 11$$$, $$$[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]$$$ is the only fibonacci-like sequence.

+
+ + + + + +
+
+ + +
+ +
C. Ntarsis' Set
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Ntarsis has been given a set $$$S$$$, initially containing integers $$$1, 2, 3, \ldots, 10^{1000}$$$ in sorted order. Every day, he will remove the $$$a_1$$$-th, $$$a_2$$$-th, $$$\ldots$$$, $$$a_n$$$-th smallest numbers in $$$S$$$ simultaneously.

What is the smallest element in $$$S$$$ after $$$k$$$ days?

Input

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^5$$$). The description of the test cases follows.

The first line of each test case consists of two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n,k \leq 2 \cdot 10^5$$$) — the length of $$$a$$$ and the number of days.

The following line of each test case consists of $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 10^9$$$) — the elements of array $$$a$$$.

It is guaranteed that:

  • The sum of $$$n$$$ over all test cases won't exceed $$$2 \cdot 10^5$$$;
  • The sum of $$$k$$$ over all test cases won't exceed $$$2 \cdot 10^5$$$;
  • $$$a_1 < a_2 < \cdots < a_n$$$ for all test cases.
Output

For each test case, print an integer that is the smallest element in $$$S$$$ after $$$k$$$ days.

Example
Input
7
5 1
1 2 4 5 6
5 3
1 3 5 6 7
4 1000
2 3 4 5
9 1434
1 4 7 9 12 15 17 18 20
10 4
1 3 5 7 9 11 13 15 17 19
10 6
1 4 7 10 13 16 19 22 25 28
10 150000
1 3 4 5 10 11 12 13 14 15
Output
+3
+9
+1
+12874
+16
+18
+1499986
+
Note

For the first test case, each day the $$$1$$$-st, $$$2$$$-nd, $$$4$$$-th, $$$5$$$-th, and $$$6$$$-th smallest elements need to be removed from $$$S$$$. So after the first day, $$$S$$$ will become $$$\require{cancel}$$$ $$$\{\cancel 1, \cancel 2, 3, \cancel 4, \cancel 5, \cancel 6, 7, 8, 9, \ldots\} = \{3, 7, 8, 9, \ldots\}$$$. The smallest element is $$$3$$$.

For the second case, each day the $$$1$$$-st, $$$3$$$-rd, $$$5$$$-th, $$$6$$$-th and $$$7$$$-th smallest elements need to be removed from $$$S$$$. $$$S$$$ will be changed as follows:

Day$$$S$$$ before$$$S$$$ after
1$$$\{\cancel 1, 2, \cancel 3, 4, \cancel 5, \cancel 6, \cancel 7, 8, 9, 10, \ldots \}$$$$$$\to$$$$$$\{2, 4, 8, 9, 10, \ldots\}$$$
2$$$\{\cancel 2, 4, \cancel 8, 9, \cancel{10}, \cancel{11}, \cancel{12}, 13, 14, 15, \ldots\}$$$$$$\to$$$$$$\{4, 9, 13, 14, 15, \ldots\}$$$
3$$$\{\cancel 4, 9, \cancel{13}, 14, \cancel{15}, \cancel{16}, \cancel{17}, 18, 19, 20, \ldots\}$$$$$$\to$$$$$$\{9, 14, 18, 19, 20, \ldots\}$$$

The smallest element left after $$$k = 3$$$ days is $$$9$$$.

+
+ + + + + +
+
+ + +
+ +
D. Imbalanced Arrays
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Ntarsis has come up with an array $$$a$$$ of $$$n$$$ non-negative integers.

Call an array $$$b$$$ of $$$n$$$ integers imbalanced if it satisfies the following:

  • $$$-n\le b_i\le n$$$, $$$b_i \ne 0$$$,
  • there are no two indices $$$(i, j)$$$ ($$$1 \le i, j \le n$$$) such that $$$b_i + b_j = 0$$$,
  • for each $$$1 \leq i \leq n$$$, there are exactly $$$a_i$$$ indices $$$j$$$ ($$$1 \le j \le n$$$) such that $$$b_i+b_j>0$$$, where $$$i$$$ and $$$j$$$ are not necessarily distinct.

Given the array $$$a$$$, Ntarsis wants you to construct some imbalanced array. Help him solve this task, or determine it is impossible.

Input

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^5$$$). The description of the test cases follows.

The first line of each test case has a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$).

The next line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \leq a_i \leq n$$$).

It is guaranteed that the sum of $$$n$$$ across all test cases does not exceed $$$10^5$$$.

Output

For each test case, output "NO" if there exists no imbalanced array.

Otherwise, output "YES". Then, on the next line, output $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ where $$$b_i \neq 0$$$ for all $$$1 \leq i \leq n$$$ — an imbalanced array.

Example
Input
5
1
1
4
1 4 3 4
3
0 1 0
4
4 3 2 1
3
1 3 1
Output
+YES
+1 
+NO
+YES
+-3 1 -2 
+YES
+4 2 -1 -3 
+YES
+-1 3 -1
Note

For the first test case, $$$b = [1]$$$ is an imbalanced array. This is because for $$$i = 1$$$, there is exactly one $$$j$$$ ($$$j = 1$$$) where $$$b_1 + b_j > 0$$$.

For the second test case, it can be shown that there exists no imbalanced array.

For the third test case, $$$a = [0, 1, 0]$$$. The array $$$b = [-3, 1, -2]$$$ is an imbalanced array.

  • For $$$i = 1$$$ and $$$i = 3$$$, there exists no index $$$j$$$ such that $$$b_i + b_j > 0$$$.
  • For $$$i = 2$$$, there is only one index $$$j = 2$$$ such that $$$b_i + b_j > 0$$$ ($$$b_2 + b_2 = 1 + 1 = 2$$$).
Another possible output for the third test case could be $$$b = [-2, 1, -3]$$$.

+
+ + + + + +
+
+ + +
+ +
E. Ina of the Mountain
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

To prepare her "Takodachi" dumbo octopuses for world domination, Ninomae Ina'nis, a.k.a. Ina of the Mountain, orders Hoshimachi Suisei to throw boulders at them. Ina asks you, Kiryu Coco, to help choose where the boulders are thrown.

There are $$$n$$$ octopuses on a single-file trail on Ina's mountain, numbered $$$1, 2, \ldots, n$$$. The $$$i$$$-th octopus has a certain initial health value $$$a_i$$$, where $$$1 \leq a_i \leq k$$$.

Each boulder crushes consecutive octopuses with indexes $$$l, l+1, \ldots, r$$$, where $$$1 \leq l \leq r \leq n$$$. You can choose the numbers $$$l$$$ and $$$r$$$ arbitrarily for each boulder.

For each boulder, the health value of each octopus the boulder crushes is reduced by $$$1$$$. However, as octopuses are immortal, once they reach a health value of $$$0$$$, they will immediately regenerate to a health value of $$$k$$$.

Given the octopuses' initial health values, find the minimum number of boulders that need to be thrown to make the health of all octopuses equal to $$$k$$$.

Input

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^5$$$). The description of the test cases follows.

The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$1 \le k \le 10^9$$$) – the number of octopuses, and the upper bound of a octopus's health value.

The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le k$$$) – the initial health values of the octopuses.

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 the minimum number of boulders that need to be thrown to make the health values of all octopuses equal to $$$k$$$.

Example
Input
2
4 3
1 2 1 3
7 3
1 2 3 1 3 2 1
Output
+2
+4
+
Note

In the first test case, the minimum number of boulders thrown is $$$2$$$:

  • Throw the first boulder between $$$[l,r] = [1,3]$$$. Then, the octopuses' health values become $$$[3, 1, 3, 3]$$$.
  • Throw the second boulder between $$$[l,r] = [2,2]$$$. Then, the octopuses' health values become $$$[3, 3, 3, 3]$$$.

In the second test case, the minimum number of boulders thrown is $$$4$$$. The $$$[l,r]$$$ ranges are $$$[1,7], [2, 6], [3, 5], [4, 4]$$$.

+
+ + + + + +
+
+ + +
+ +
F. Miriany and Matchstick
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Miriany's matchstick is a $$$2 \times n$$$ grid that needs to be filled with characters A or B.

He has already filled in the first row of the grid and would like you to fill in the second row. You must do so in a way such that the number of adjacent pairs of cells with different characters$$$^\dagger$$$ is equal to $$$k$$$. If it is impossible, report so.

$$$^\dagger$$$ An adjacent pair of cells with different characters is a pair of cells $$$(r_1, c_1)$$$ and $$$(r_2, c_2)$$$ ($$$1 \le r_1, r_2 \le 2$$$, $$$1 \le c_1, c_2 \le n$$$) such that $$$|r_1 - r_2| + |c_1 - c_2| = 1$$$ and the characters in $$$(r_1, c_1)$$$ and $$$(r_2, c_2)$$$ are different.

Input

The first line consists of an integer $$$t$$$, the number of test cases ($$$1 \leq t \leq 1000$$$). The description of the test cases follows.

The first line of each test case has two integers, $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 2 \cdot 10^5, 0 \leq k \leq 3 \cdot n$$$) – the number of columns of the matchstick, and the number of adjacent pairs of cells with different characters required.

The following line contains string $$$s$$$ of $$$n$$$ characters ($$$s_i$$$ is either A or B) – Miriany's top row of the matchstick.

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

Output

For each test case, if there is no way to fill the second row with the number of adjacent pairs of cells with different characters equals $$$k$$$, output "NO".

Otherwise, output "YES". Then, print $$$n$$$ characters that a valid bottom row for Miriany's matchstick consists of. If there are several answers, output any of them.

Example
Input
4
10 1
ABBAAABBAA
4 5
AAAA
9 17
BAAABBAAB
4 9
ABAB
Output
+NO
+YES
+BABB
+YES
+ABABAABAB
+NO
Note

In the first test case, it can be proved that there exists no possible way to fill in row $$$2$$$ of the grid such that $$$k = 1$$$.

For the second test case, BABB is one possible answer.

The grid below is the result of filling in BABB as the second row.

$$$\begin{array}{|c|c|} \hline A & A & A & A \cr \hline B & A & B & B \cr \hline \end{array}$$$

The pairs of different characters are shown below in red:

$$$\begin{array}{|c|c|} \hline \color{red}{A} & A & A & A \cr \hline \color{red}{B} & A & B & B \cr \hline \end{array}$$$

—————————————————

$$$\begin{array}{|c|c|} \hline A & A & \color{red}{A} & A \cr \hline B & A & \color{red}{B} & B \cr \hline \end{array}$$$

—————————————————

$$$\begin{array}{|c|c|} \hline A & A & A & \color{red}{A} \cr \hline B & A & B & \color{red}{B} \cr \hline \end{array}$$$

—————————————————

$$$\begin{array}{|c|c|} \hline A & A & A & A \cr \hline \color{red}{B} & \color{red}{A} & B & B \cr \hline \end{array}$$$

—————————————————

$$$\begin{array}{|c|c|} \hline A & A & A & A \cr \hline B & \color{red}{A} & \color{red}{B} & B \cr \hline \end{array}$$$

There are a total of $$$5$$$ pairs, which satisfies $$$k$$$.

+
+ + + + + +
+
+ + + +
+
+ + +