Bangladesh Olympiad in Informatics Practice Round 4
A. Winter Charity
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

A winter cloth distribution campaign will be held in the city of BugBlossoms. There are two orphanages separated by a river, and each orphanage has $$$n$$$ rooms numbered from $$$1$$$ to $$$n$$$ from left to right. Crossing the river takes $$$k$$$ units of time. Each room is either empty or occupied by a single child (represented by each character 1 in the input strings).

As Ad-Duha is managing the campaign alone, she has decided to keep all the winter clothing packages in a single room — that is, in exactly one of the $$$2n$$$ rooms across both orphanages — due to a lack of volunteers. The children from every occupied room in both orphanages must come to that chosen room to collect their winter clothes.

If the packages are located in room $$$i$$$ of an orphanage and a child is in room $$$j$$$:

  • of the same orphanage, the child takes $$$|i-j|$$$ units of time to reach the packages;
  • of the other orphanage, the child must cross the river, which takes $$$k+i+j$$$ units of time.

Ad-Duha wants to choose the room so as to minimize the maximum time any child spends reaching the room containing the packages.

Once this room is chosen, the children are split into two groups:

  • Group A: the children who live in the same orphanage as the chosen room. Let $$$a$$$ be their number.
  • Group B: the children who live in the other orphanage (who have to cross the river). Let $$$b$$$ be their number.

Ad-Duha felt sad that the children in Group B had to cross the river to receive their winter clothes, so she decided to give them books, flowers and chocolates as well. She has $$$m$$$ gift boxes in total. Each child in Group A gets exactly $$$x$$$ boxes, and each child in Group B gets exactly $$$y$$$ boxes, where:

  • $$$x \ge 1$$$,
  • $$$y \ge 2$$$,
  • $$$y \gt x$$$.

The total $$$a \cdot x + b \cdot y$$$ must not be more than $$$m$$$. Ad-Duha wants to give away as many boxes as possible. Find the minimum number of boxes that remain ungifted.

Input

The first line contains an integer $$$T$$$ — the number of test cases.

Each test case is described as follows:

  • Line 1: three integers $$$n$$$, $$$k$$$, and $$$m$$$ — the number of rooms in each orphanage, the time required to cross the river, and the number of gift boxes, respectively.
  • Line 2: a string of $$$n$$$ characters, each either 0 or 1. This string represents the children staying in the rooms of the first orphanage. If the $$$i$$$-th character is 1, a child stays in room $$$i$$$ of the first orphanage. Otherwise that room is empty.
  • Line 3: a string of $$$n$$$ characters, each either 0 or 1. This string represents the children staying in the rooms of the second orphanage, in the same way.

It is guaranteed that each of the two strings contains at least two 1s, so whichever orphanage is chosen, $$$a \ge 1$$$ and $$$b \ge 2$$$.

It is also guaranteed that $$$a + 2b \le m$$$ for whichever orphanage is chosen. That is, both $$$c_1 + 2c_2 \le m$$$ and $$$c_2 + 2c_1 \le m$$$, where $$$c_1$$$ and $$$c_2$$$ are the numbers of 1s in the first and second string.

Constraints:

  • $$$1 \le T \le 10^4$$$;
  • $$$1 \le n \le 10^5$$$;
  • $$$1 \le k \le 1000$$$;
  • $$$3 \le m \le 1000$$$;
  • the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
Output

For each test case, print the following three lines:

  • Line 1: one integer — the minimum possible maximum travel time among all possible locations for the winter clothing packages.
  • Line 2: two integers — the index of the orphanage and the index of the room where the packages should be placed. If there are multiple optimal answers, print any of them.
  • Line 3: one integer — the minimum possible number of gift boxes left ungifted.

If the minimum time can be reached by placing the packages in orphanage 1 and also by placing them in orphanage 2, you may take either orphanage as the one with the packages when you compute $$$a$$$ and $$$b$$$. Any of the resulting answers for the third line is accepted.

Every test case must print exactly three lines. If you cannot compute some part, print -1 in its place:

  • Line 1 is the minimum requirement and must always be printed.
  • If you do not print a valid room, print -1 -1 on line 2.
  • If you do not print the number of leftover boxes, print -1 on line 3.
Scoring
SubtaskPoints$$$T$$$$$$n$$$Additional Constraints
00--The examples
130$$$\le 10$$$$$$\le 10^3$$$No additional constraints.
230-$$$\le 10^3$$$No additional constraints.
340--No additional constraints.

All other constraints are the same as in the Input section.

Each test case in a file is scored separately, and the score of the file is the average over its test cases. For one test case:

  • If you print the minimum possible maximum travel time on the first line, you get $$$30\%$$$ of the score.
  • If you print a valid room on the second line whose maximum travel time equals that minimum, you get another $$$30\%$$$ of the score.
  • If you print the minimum number of ungifted boxes on the third line, you get the remaining $$$40\%$$$ of the score. The number is computed for the orphanage printed on the second line (or for either orphanage, if the minimum time is reached by both).

If the first line is wrong, the whole test case scores $$$0$$$.

Example
Input
5
5 3 20
10100
00101
6 10 41
100001
010010
4 1 15
1110
0011
4 2 11
1101
0110
3 1 8
111
110
Output
7
2 1
0
16
1 1
1
5
2 1
1
6
1 1
0
4
1 1
1
Note

Test 1: The first orphanage has children in rooms $$$1$$$ and $$$3$$$, and the second in rooms $$$3$$$ and $$$5$$$.

Placing the packages in room $$$1$$$ of the second orphanage:

  • the children in the same orphanage need $$$|1-3| = 2$$$ and $$$|1-5| = 4$$$;
  • the children in the first orphanage need $$$3+1+1 = 5$$$ and $$$3+1+3 = 7$$$.
The maximum is $$$7$$$, and no other room does better. Here $$$a = 2$$$ and $$$b = 2$$$. Give each of the $$$2$$$ children $$$4$$$ boxes and each of the $$$2$$$ children $$$6$$$ boxes. That uses $$$2 \cdot 4 + 2 \cdot 6 = 20$$$ boxes, so $$$0$$$ are left.

Test 2: Placing the packages in room $$$1$$$ of the first orphanage:

  • the same-orphanage children need $$$0$$$ and $$$5$$$;
  • the children from the second orphanage need $$$10+1+2 = 13$$$ and $$$10+1+5 = 16$$$.
The answer is $$$16$$$. Here $$$a = 2$$$ and $$$b = 2$$$. Give $$$9$$$ boxes and $$$11$$$ boxes. That uses $$$2 \cdot 9 + 2 \cdot 11 = 40$$$ boxes, so $$$1$$$ is left. Giving $$$10$$$ and $$$11$$$ would need $$$42 \gt 41$$$.
B. Number Roulette
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

"This town ain't big enough for the two of us."

Arthur Morgan has been challenged to a game of Russian Roulette. However, this isn't normal Russian Roulette — it is played on an $$$N \times M$$$ grid where each cell $$$(r, c)$$$ has an integer value $$$A_{r,c}$$$.

Before the game starts, Arthur chooses a positive integer $$$X$$$:

  • A cell $$$(r, c)$$$ is safe if $$$A_{r,c} \ge X$$$.
  • A cell $$$(r, c)$$$ is unsafe if $$$A_{r,c} \lt X$$$.

Connected safe cells (sharing an edge up, down, left, or right) form a safe zone. The size of a safe zone is the total number of cells in it.

Arthur starts at $$$(1, 1)$$$ and can move up, down, left, or right. To win the game, he must reach any cell in a safe zone of size at least $$$S$$$, while stepping on at most $$$K$$$ unsafe cells along his path (including $$$(1, 1)$$$ if it is unsafe).

Find the maximum integer $$$X$$$ Arthur can choose to win the game, or output 0 if it is impossible.

Input
  • The first line contains four integers $$$N$$$, $$$M$$$, $$$K$$$ and $$$S$$$.
  • The next $$$N$$$ lines describe the grid, one row per line. The $$$r$$$-th of these lines contains $$$M$$$ space-separated integers $$$A_{r,1}, A_{r,2}, \dots, A_{r,M}$$$, where $$$A_{r,c}$$$ is the value of the cell in row $$$r$$$ and column $$$c$$$.

Rows are numbered from top to bottom and columns from left to right, so $$$(1, 1)$$$ is the top-left cell.

Constraints:

  • $$$1 \le N, M \le 500$$$;
  • $$$0 \le K \le N \cdot M$$$;
  • $$$1 \le S \le N \cdot M$$$;
  • $$$1 \le A_{r,c} \le 10^9$$$.
Output

Print a single integer — the maximum valid $$$X$$$, or 0 if Arthur cannot win.

Scoring
SubtaskPoints$$$N$$$$$$M$$$$$$K$$$$$$S$$$$$$A_{r,c}$$$Additional Constraints
00-----The examples
120$$$\le 40$$$$$$\le 40$$$--$$$\le 50$$$No additional constraints.
220---$$$= 1$$$$$$\in \{1, 2\}$$$An optimal path only moves Down and Right.
325--$$$= N \times M$$$--No additional constraints.
435-----No additional constraints.

All other constraints are the same as in the Input section.

Example
Input
3 4 1 3
9 2 8 8
1 1 2 9
4 4 1 7
Output
8
Note

The grid is:


9 2 8 8
1 1 2 9
4 4 1 7

Choose $$$X = 8$$$. The safe cells are $$$(1,1)$$$, $$$(1,3)$$$, $$$(1,4)$$$ and $$$(2,4)$$$.

  • $$$(1,1)$$$ forms a safe zone of size $$$1$$$.
  • $$$(1,3)$$$, $$$(1,4)$$$ and $$$(2,4)$$$ are connected and form a safe zone of size $$$3 \ge S$$$.
Arthur starts at $$$(1,1)$$$, steps onto the unsafe cell $$$(1,2)$$$ (value $$$2 \lt 8$$$), and then reaches $$$(1,3)$$$. He stepped on $$$1 \le K$$$ unsafe cell, so he wins.

Choose $$$X = 9$$$. Only $$$(1,1)$$$ and $$$(2,4)$$$ are safe, and each forms a zone of size $$$1 \lt S$$$, so Arthur cannot win. Therefore the maximum valid $$$X$$$ is $$$8$$$.

C. Anya and Prime Numbers
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Anya has sat for her mid-term exam in her school, Eden Academy. She must achieve all the stella stars to keep her family together and save world peace. Otherwise, Loid's "Operation Strix" will fail. The students next to her are struggling to answer the questions, so she can't take any worthy help from her telepathy ability. Help her to find the answers.

She is given an array $$$A$$$ with $$$N$$$ integers. Let's denote: $$$$$$\pi(x) = \text{the number of prime numbers from } 1 \text{ to } x.$$$$$$

There are $$$Q$$$ questions (queries) to answer, of two types:

  • 1 l r — output $$$\sum_{i=l}^{r} \pi(A_i)$$$;
  • 2 u v — update $$$A_u := v$$$.

Help Anya cook in her exam and make Damian fall for her, making Operation Strix succeed.

Input
  • The first line contains a single integer $$$N$$$.
  • The second line contains $$$N$$$ distinct integers $$$A_1, A_2, \dots, A_N$$$.
  • The third line contains a single integer $$$Q$$$.
  • Each of the next $$$Q$$$ lines describes a query in one of the following forms:
    • 1 l r — a query of type $$$1$$$;
    • 2 u v — a query of type $$$2$$$.

Constraints:

  • $$$1 \le N \le 2 \cdot 10^5$$$;
  • $$$1 \le Q \le 2 \cdot 10^5$$$;
  • $$$1 \le A_i \le 10^8$$$;
  • $$$A_i \ne A_j$$$ for $$$i \ne j$$$ ($$$1 \le i, j \le N$$$);
  • $$$1 \le l \le r \le N$$$;
  • $$$1 \le u \le N$$$;
  • $$$1 \le v \le 10^8$$$.
Output

For every query of type $$$1$$$, print one integer: $$$\sum_{i=l}^{r} \pi(A_i)$$$, where $$$\pi(A_i)$$$ is the number of prime numbers from $$$1$$$ to $$$A_i$$$.

Print the answers in the order the queries are given.

Scoring
SubtaskPoints$$$N$$$$$$Q$$$$$$A_i$$$$$$v$$$Additional Constraints
00----The examples
110$$$\le 1000$$$$$$\le 1000$$$$$$\le 10^4$$$$$$\le 10^4$$$No additional constraints.
215$$$\le 5000$$$$$$\le 5000$$$$$$\le 10^6$$$$$$\le 10^6$$$No additional constraints.
320--$$$\le 10^7$$$-There are no type $$$2$$$ queries.
425--$$$\le 10^7$$$$$$\le 10^7$$$No additional constraints.
530--$$$\le 10^8$$$$$$\le 10^8$$$No additional constraints.
Example
Input
10
4 3 0 50 49 1 3 4 4 3
8
1 5 6
1 4 4
1 9 9
2 7 50
1 2 8
1 8 9
2 1 3
2 9 3
Output
15
15
2
49
4
Note

The initial array is $$$A = [4, 3, 0, 50, 49, 1, 3, 4, 4, 3]$$$. Useful values: $$$\pi(0) = \pi(1) = 0$$$, $$$\pi(3) = \pi(4) = 2$$$, $$$\pi(49) = \pi(50) = 15$$$.

  1. 1 5 6: $$$\pi(49) + \pi(1) = 15 + 0 = 15$$$.
  2. 1 4 4: $$$\pi(50) = 15$$$.
  3. 1 9 9: $$$\pi(4) = 2$$$.
  4. 2 7 50: $$$A_7$$$ becomes $$$50$$$, so the array is $$$[4, 3, 0, 50, 49, 1, 50, 4, 4, 3]$$$.
  5. 1 2 8: the values are $$$3, 0, 50, 49, 1, 50, 4$$$, so the answer is $$$2 + 0 + 15 + 15 + 0 + 15 + 2 = 49$$$.
  6. 1 8 9: $$$\pi(4) + \pi(4) = 2 + 2 = 4$$$.
  7. 2 1 3: $$$A_1$$$ becomes $$$3$$$.
  8. 2 9 3: $$$A_9$$$ becomes $$$3$$$.