You are given $$$N$$$ of sticks. The length of the $$$i$$$-th stick is denoted by $$$L_i$$$. You have to arrange each of these sticks horizontally on a line of cells in the range $$$[1, \infty ]$$$. It can happen that two or more sticks overlap with each other, so a cell can be occupied by more than one stick.
You have to answer $$$Q$$$ queries.
For every query, you are given two integers $$$R$$$ and $$$K$$$, you have to answer with YES if and only if there exists an arrangement of the sticks such that all the below conditions are satisfied:
Else answer with NO.
Constraints
In all inputs,
For each testcase, print either "YES" or "NO" (without quotes). Note that the answer is case-insensitive.
2 5 3 1 2 3 4 5 15 15 7 2 8 6 3 2 1 3 1 4 0 5 3
YES YES NO NO YES
Below is a possible arrangement for the second query of the second test case.
You want to create a grid with $$$\le 500$$$ rows and $$$\le 500$$$ columns, and then color each cell of the grid either red or blue.
Let's say your grid contains $$$n$$$ rows numbered $$$1, 2, \ldots n$$$ from top to bottom and $$$m$$$ columns numbered $$$1, 2, \ldots m$$$ from left to right. Let $$$(i, j)$$$ denote the cell with row number $$$i$$$ and column number $$$j$$$.
Consider a path from top left to bottom right $$$$$$(x_1 = 1, y_1 = 1), (x_2, y_2), \ldots (x_{n+m - 2}, y_{n+m - 2}), (x_{n + m - 1} = n, y_{n + m - 1} = m),$$$$$$ that goes either right or down, that is, for every $$$1 \lt i \le n + m - 1$$$, either $$$(x_i, y_i) = (x_{i-1}, y_{i-1} + 1)$$$ or $$$(x_i, y_i) = (x_{i-1}+1, y_{i-1})$$$. We call such a path almost red if at max $$$1$$$ of the $$$n + m - 1$$$ cells on the path is colored blue. Similarly, we such a path almost blue if at max one of the $$$n + m - 1$$$ cells on the path is colored red.
Your task is to construct a grid and its coloring such that:
Constraints:
For each testcase, first print the number of rows and the number of columns, $$$n$$$ and $$$m$$$ on a new line. Then, print $$$n$$$ lines. The $$$i-$$$th line should contain a string describing the $$$i-$$$th row of the grid. The $$$j-$$$th character of this string should be R if the cell $$$(i, j)$$$ is colored red and B otherwise.
2 1 1 0 5
2 2 RB RB 3 4 BBBR RBRR BBBB
In the output of the first testcase, there are two paths, the first being almost red and the second being almost blue:
In the output of the second testcase, there are no almost red paths. There are $$$5$$$ almost blue paths as required:
There is a graph $$$G$$$ with $$$A = \sum_{i=1}^{n} a_i$$$ vertices, numbered $$$1, 2, \ldots A$$$. For each $$$1 \leq i \leq n$$$, there are $$$a_i$$$ vertices with type $$$i$$$. The vertices numbered $$$(\sum_{j=1}^{i-1} a_j) + r$$$ are of type $$$a_i$$$ for each $$$r \in \{1, 2, \ldots a_i\}$$$. That is, the first $$$a_1$$$ vertices have type $$$1$$$, the next $$$a_2$$$ vertices have type $$$2$$$, and so on.
The edges of the graph are described through $$$m$$$ 4-tuples. Each tuple is of the form $$$t \text{ } u \text{ } v \text{ } w$$$, where $$$t \in \{1,2\}$$$, and $$$u \neq v$$$:
Please note that there might be multiple edges between the same pair of vertices in the graph.
Find the cost of the minimum spanning tree of the graph, or print $$$-1$$$ if the graph is disconnected.
The first line contains two integers $$$n (1 \leq n \leq 10^5)$$$ and $$$m(1 \leq m \leq 2 \cdot 10^5$$$)
The second line contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots a_n$$$, where $$$1 \leq a_i \leq 10^9$$$ and the sum of all $$$a_i$$$ does not exceed $$$10^9$$$.
Each of the next $$$m$$$ lines contains a $$$4-$$$tuple, of the form $$$t \text{ } u \text{ } v \text{ } w$$$
Print the cost of the minimum spanning tree of the graph. If the graph is disconnected, print $$$-1$$$ instead.
3 4 2 3 1 2 1 2 5 2 2 3 7 1 1 6 3 1 1 2 10
23
The graph $$$G$$$ is the following:
The minimum spanning tree is the following:
A tournament is a directed graph in which for every unordered pair of vertices $$$(i, j)$$$ where $$$i \neq j$$$, there is either an edge from $$$i$$$ to $$$j$$$ or an edge from $$$j$$$ to $$$i$$$, but not both.
Call a vertex $$$u$$$ in a tournament good, if for every other vertex $$$v$$$, there exists a path from $$$u$$$ to $$$v$$$ with at max $$$3$$$ edges. Your task is to find a good vertex. There is a hidden tournament with $$$n$$$ vertices numbered $$$0, 1, \ldots n - 1$$$, and the only way you can extract information about it is by asking queries of the form (without quotes):
"? i j"
For each such query, the judge replies 1 if there is an edge from $$$i$$$ to $$$j$$$ in the tournament and 0 otherwise. After making some queries (limit on the number of queries specified later), you should output(without quotes):
"! v"
where $$$v$$$ is any good vertex. It can be proved that a good vertex always exists. If there exist multiple good vertices, you can output any of them.
Note that the grader is adaptive, which means that the graph is not fixed before the interaction begins. However, the judge will never contradict the replies given in the past. For example, if the judge has replied 1 for a query "? i j" in the past, then for every future query "? i j", the judge will reply 1, and for every future query "? j i", the judge will reply 0
First read an integer $$$T(1 \leq T \leq 2000)$$$, which means the number of test cases. Each test case should be considered independently. For each test case,
The sum of $$$n$$$ over all test cases doesn't exceed $$$2000$$$. Your solution is accepted if you output a good vertex for every test case, and the sum of the number of queries asked over all the testcases doesn't exceed $$${\bf 15000}$$$.
2 3 1 0 0 1 2 0
? 0 1 ? 2 1 ? 1 0 ? 0 2 ! 0 ? 0 1 ! 1
Construct a set of $$$m \leq 50$$$ permutations of $$$\{1, 2, \ldots 500\}$$$, such that for each tuple $$$(i, j, k)$$$, where $$$1 \leq i, j, k \leq 500$$$, with $$$i \neq j, j \neq k$$$ and $$$k \neq i$$$, there exists a permutation in the set that contains $$$(i, j, k)$$$ as a subsequence.
There is no input for this problem.
On the first line print $$$m(1 \leq m \leq 50)$$$, the number of permutations you want to print. Print a permutation of $$$\{1, 2, \ldots 500\}$$$ on each of the next $$$m$$$ lines.
You have a strip with $$$n$$$ cells, numbered $$$1, 2, \ldots n$$$, each colored either black or white. You have a paint brush of length $$$k$$$, and a cost array $$$c_1, c_2, \ldots c_{n-k+1}$$$. You repeat the following step until all cells are black:
What is the total expected cost?
The first line contains two integers $$$n (1 \leq n \leq 4000)$$$ and $$$k(1 \leq k \leq n$$$).
The second line contains a string of length $$$n$$$ consisting of W and B, describing the initial colors of the cells $$$1, 2, \ldots n$$$.
The third line contains $$$n - k + 1$$$ space separated integers $$$c_1, c_2, \ldots c_{n-k+1}$$$.
Print a single line, containing the expected cost modulo $$$998244353$$$. It can be proved that the expected cost is rational, and can be written in the form $$$\frac{P}{Q}$$$, where $$$Q \neq 0 \pmod {998244353}$$$
4 2 WBWW 3 1 1
499122184
The expected value of the cost is $$$15/2$$$
You are given an array of $$$N$$$ integers $$$A_1, A_2, \ldots A_N$$$. For each $$$k$$$ from $$$0$$$ to $$$N$$$ inclusive, find the length of the longest possible strictly increasing subsequence of the array if you are allowed to change at max $$$k$$$ values of the array to any integer (in particular, you're allowed to change to a zero or negative integer as well).
A sequence $$$X$$$ is a subsequence of a sequence $$$Y$$$ if $$$X$$$ can be obtained from $$$Y$$$ by deleting several (possibly zero) elements. A sequence is strictly increasing if every element in it is greater than the previous element. In particular, a sequence of length $$$1$$$ is strictly increasing.
Constraints
For each testcase, print for each $$$k$$$ from $$$0$$$ to $$$N$$$ inclusive, the length of the longest possible strictly increasing subsequence of the array if you are allowed to change at max $$$k$$$ values of the array to any integer.
2 4 1 2 1 3 4 1 1 1 1
3 3 4 4 4 1 2 3 4 4
You are given a rooted tree with $$$n$$$ vertices numbered $$$1, 2, .. n$$$, where every edge has a character on it. You want to find an euler tour, such that the string formed by the concatenation of the characters on the edges is minimized. Formally, let $$$c(x, y)$$$ denote the character written on the edge $$$(x, y)$$$. Initialize an empty string $$$S$$$, and order the children of each node arbitrarily and then run dfs(1):
dfs(s):
for v in children(s):
append c(s, v) to S
dfs(v)
append c(s, v) to S
In the end, you get a string $$$S$$$ of length $$$2n - 2$$$. What is the lexicographically minimum $$$S$$$ that you can achieve?
The first line contains $$$n (2 \leq n \leq 5 \cdot 10^5)$$$, the number of vertices in the tree. The $$$i^{th}$$$ of the next $$$n - 1$$$ lines contains two integers, $$$p_i$$$ and $$$c_i$$$, where $$$p_i$$$ is the parent of the node $$$i + 1$$$, and $$$c_i$$$ is the character write on the edge between $$$i + 1$$$ and $$$p_i$$$ respectively. Here $$$1 \leq p_i \lt i$$$ and $$$c_i$$$ is a lower case english alphabet.
Print the lexicographically minimum possible $$$S$$$.
5 1 a 2 c 2 b 1 a
aaabbcca
The tree is the following:
The optimal euler tour is $$$(1, 5, 1, 2, 4, 2, 3, 2, 1)$$$
You are given a tree with $$$N$$$ vertices numbered $$$1, 2, \ldots N$$$. The $$$i-$$$th vertex has a color $$$C_i$$$. Each edge of the tree has some positive weight. Let's define the value of a pair $$$(x, y)$$$ of distinct vertices $$$x$$$ and $$$y$$$ as the sum of the weights of the edges that appear on the unique simple path between $$$x$$$ and $$$y$$$. Let's call such a pair valid if $$$x$$$ and $$$y$$$ have different colors. You want to select some ($$$\geq 0$$$) valid pairs of vertices, such that each vertex is contained in at max one pair, and the sum of values of all the selected pairs is maximized. Print this maximum value.
Constraints
For each testcase, print the answer on a new line.
2 4 1 1 2 2 1 2 3 2 3 1 2 4 5 2 1 1 1 2 1
9 0
There is a hidden permutation $$$P$$$ of $$$\{1, 2, \ldots n\}$$$. In one query, you can output a permutation $$$Q$$$ of $$$\{1, 2, \ldots n\}$$$ and in return get the value:
$$$$$$ \sum_{i=1}^{n} |P_i - Q_i| $$$$$$
You can ask at most $$$1300$$$ queries. Recover the permutation $$$P$$$.
First, read an integer $$$n$$$ ($$$1 \leq n \leq 1000$$$). Then, you can output a permutation $$$Q$$$ of $$$\{1, 2, \ldots n\}$$$ at most $$$1301$$$ times (It is $$$1301$$$ instead of $$$1300$$$ because the last query is not counted, as you will see later). The judge returns its sum of absolute differences when compared with $$$P$$$.
The judge returns with an ok verdict when $$$Q = P$$$ (or equivalently, the return value is $$$0$$$). Else, if $$$Q$$$ is never equal to $$$P$$$, you get a wrong answer verdict.
4 4 6 0
1 2 3 4 2 3 4 1 2 3 1 4