Contest Document F22
Contest Document F22
This document includes 12 problems. Novice teams do problems 1-8; standard teams do problems 5-
12.
Any team which submits a correct solution for any of problems 1-4 will be assumed to be a novice
team. If you are not a novice team, please skip problems 1-4.
Problems 1-4 are easier than problems 5-8, which are easier than problems 9-12. These problems are
correspondingly labeled “Novice”, “Intermediate”, and “Advanced.” Order does not otherwise relate
to difficulty.
You may use the Internet only for submitting your solutions, reading Javadocs or Python
documentation, and referring to any documents we link you to. You may not use the Internet for
things like StackOverflow, Google, or outside communication.
As you may know, you can choose to solve any given problem in Java, Python, or C++. We have
provided stub files for all questions in all languages that take care of the input parsing for you. Do not
modify any of the parsing or output code. Just fill out the stub methods in each file and submit it
with exactly the same name.
Do not modify any of the given methods or method headers in our stub files! They are all specified in
the desired format. You may add class fields, helper methods, etc as you like, but modifying given
parts will cause your code to fail in our testers.
There is no penalty for incorrect submissions. You will receive 1 point per problem solved. A team’s
number of incorrect submissions will be used only as a tiebreaker.
Some problems use Java’s “long” type; if you are unfamiliar with them, they’re like an “int”, but with a
(much) bigger upper bound, and you have to add “L” to the end of an explicit value assignment:
long myLong = 1000000000000L;
Otherwise, the “long” type functions just like the “int” type.
1. The Smartest People Work on Mechanical Bees
Implementation Details:
Implement the following function. It will be called by the grader once for each test case.
findOdd(arr):
- arr: an array of integers
Your function should return an integer.
Constraints
1 ≤∨arr ∨≤ 10 4
5
0 ≤ x ≤ 10 where x is an element of arr.
Input
The first line contains a single integer t (1 <= t <= 105), denoting the number of test cases.
For each test case, the first line contains a single integer n (1 <= n <= 104) representing the
number of bees Nick captures.
The next line contains n integers x1, x2, …, xn (0 <= xi <= 105), where xi is the number of
follicles on the ith bee.
We guarantee that the sum over all n does not exceed 2*105.
Output
For each test case, return an integer representing the number of fuzz follicles on the
mechanical bee. If there is no mechanical bee, output 0.
Implementation Details:
Implement the following function. It will be called by the grader once for each test case.
rotateString(s, n):
- s: the string to rotate
- n: the index that remains fixed
Constraints
1 ≤∨s∨≤ 10000
0 ≤ n ≤∨s∨−1 where n is an index of s
Each test case consists of multiple lines of input. The first line contains a lowercase string s (1
<= |s| <= 104), the string to rotate. The second line contains n (0 <= n <= |s| - 1), the index of the
letter which stays fixed.
We guarantee that the sum over all |s| does not exceed 2 * 105
Output
For each test case, return a string where characters to the left of index n are rotated left, and
characters to the right of index n are rotated right.
Implementation Details
getNth(c1, c2, c3, n):
- c1: represents the first element of the sequence
- c2: represents the second element of the sequence
- c3: represents the third element of the sequence
- n: represents the number element of the sequence that you should return
Your function should return an integer.
Constraints
1 ≤ c 1 , c 2 , c 3 ≤ 100000
1 ≤n ≤ 10000
Input
The first line contains a single integer t (1 <= t <= 104), denoting the number of test cases.
The first line of each test case contains three integers representing c1, c2, and c3 respectively
1 <= c1, c2, c3 <= 105. The next line contains n (1 <= n <= 104).
We guarantee that the sum of n over all test cases does not exceed 3*104.
Output
For each test case, return an integer that represents the nth element of the sequence mod 100000007
(10^8 + 7).
Implementation Details
Implement the following function. It will be called by the grader once for each test case.
monsterMash(arr, k):
- arr: array of integers
- k: number of consecutive integers needed to fuse
Your function should return an array.
Constraints
4
1 ≤∨arr ∨≤ 10
5
0 ≤ x ≤ 10 where x is an element of arr.
5
2≤k≤10
(cont. on next page)
Input
The first line contains a single integer t (1 <= t <= 105), denoting the number of test cases.
For each test case, the first line contains a single integer n (1 <= n <= 104), representing the
number of monsters. The second line contains n integers x1, x2, ..., xn (0 <= xi <= 105),
representing the species of the ith monster. The third line contains a single integer k, the
number of contiguous monsters of the same species required for fusing.
We guarantee that the sum over all n does not exceed 2 * 105.
Output
For each test case, return a comma-separated list of values surrounded by square brackets
that represents the final line of monsters after all possible fusions.
Grace is given 4 pieces of information: 1) the order in which teleporters will 'open', 2) how
many seconds the nth teleporter will be 'open', 3) the start and end destination of k civilians,
and 4) the time each civilian arrives at the intersection. Using this information, help Grace
find the shortest time it takes each civilian to traverse the intersection and evacuate
successfully. There are no restrictions on the number of civilians that can cross at one time.
Note: a portal will close right before the next portal opens. For example, if the portal at B
opens at time t=0 and its duration is 5 seconds, a civilian arriving at time t=5 cannot use this
portal.
Implementation Details
Implement the following function. It will be called by the grader once for each test case.
Your function should return an array that represents the time it takes each civilian to cross the
intersection.
Constraints
1 <= n <= 1000
1 <= k <= 1000
We guarantee that the sum of n over all test cases is <= 1000
orderi <= 106
xi <= 109
Input
The first line contains a single integer t (1 <= t <= 100), denoting the number of test cases.
For each test case, the first line contains two space-separated integers n and k (1 <= n, k <=
1000), representing the number of teleporters and the number of civilians respectively. The
third line contains n characters order1, order2, ..., ordern, where orderi is an element of {A, B, C,
D} denoting the order that the teleporters open. The fourth line contains n integers duration1,
duration2, ..., durationn, (1 <= di <= 106) where durationi is the duration that the ith teleporter
stays open.
The following k lines describe the civilians where the ith line contains si, fi which are elements
of {A, B, C, D} and x i (0 <= xi <= 109). si and fi (integers) represent the start and destination
teleporters of the ith civilian, and xi denotes the time that the civilian arrives at teleporter si.
We guarantee that the sum of n over all test cases does not exceed 1000.
Output
For each test case, output k lines, where the ith line is the minimum amount of time it takes
the ith civilian to reach teleporter fi after arriving at the teleporter s i at time xi. If the civilian
cannot reach their destination, output -1.
Implementation Details
Implement the following function. It will be called by the grader once for each test case.
pathExists(w, h, portalArr):
- w: width of grid (y-axis)
- h: height of grid (x-axis)
- n: number of portals (length of portalArr)
- portalArr: an array of tuples of length 3 - the first element is the x coordinate ( 0 ≤ xi < w ), the
second element is the y coordinate ( 0 ≤ y i< h), and the third element is its color (
0 ≤ c i< 10 000)
Your function should return a boolean where the function returns True if a path exists that goes from
top left (0, 0) to bottom right (w-1, h-1) , or False if a path does not exist.
Constraints
The grid is at least of size 2 by 2, so the bottom-left and top-right coordinate will not be the
same.
2 ≤ w ≤10,000
2 ≤ h≤ 10,000
1 ≤∨ portalArr∨≤ 10000
portalArr contains tuples of length 3 in the form (x, y, c) where x, y, and c are integers
In the tuple (x, y, c), portal coordinates x and y represent distinct (x, y) coordinates
0 ≤ xi < w
0 ≤ y i< h
0 ≤ c i< 10,000
Input
The first line contains a single integer t (1 <= t <= 104), denoting the number of test cases.
For each test case, the first line contains two space-separated integers w and h (2 <= w,h <=
104). The second line contains a single integer n, the number of portals. The following n lines
describe the portals, where the ith line contains xi, yi, ci (0 <= xi, yi, ci <= 104) which represent
the x coordinate, y coordinate, and color of the ith portal.
We guarantee that each portal has a distinct location and that the sum over all n does not
exceed 105.
Output
For each test case, output true if Pluto is able to get from (0, 0) to (w - 1, h - 1) and false
otherwise.
Tien discovered that AH-FAC will always move optimally, but can only teleport at most k times. Given
a map of the city, the starting building of Tien and AH-FAC, and the number of teleports that AH-FAC
has, return the minimum number of turns before Tien catches AH-FAC.
It can be shown that Tien can always catch AH-FAC after a finite number of turns.
Implementation Details
Implement the following function. It will be called by the grader once for each test case.
T <= 105
Across all test cases in the multitest: Sum over all buildings is <= 3*105
Input
The first line contains a single integer t (1 <= t <= 3 * 104), denoting the number of test cases.
For each test case, the first line contains a single integer n (3 <= n <= 105) representing the number of
buildings in the city. The second line contains a single integer k (0 <= k <= 109) representing the
number of times AH-FAC can teleport. The third line contains b a (0 <= ba < n), the building that AH-FAC
starts in. The fourth line contains b t (0 <= bt < n, bt != ba)$, the building that Tien starts in. The
following n - 1 lines describe the road of the city where the ith line contains ui, vi, (0 <= ui, vi < n, ui != vi)
$ meaning there is a road between buildings ui and vi.
We guarantee that the sum over all n does not exceed 105.
Output
For each test case, output a single integer representing the minimum number of turns before Tien
catches AH-FAC given that AH-FAC and Tien move optimally.
Sample Input Parsed Input Sample Output Explanation
1 minTurns(3, 0, 2 T-A-2
3 1, 0, [(0, 1), Initial starting state, where T is
0 (1, 2]) Tien and A is AH-FAC.
1
0
T-1-A
0 1
AH-FAC makes his only move
1 2
away from Tien.
0-T-A
Tien moves closer to the AH-
FAC.
0-T-A
AH-FAC has nowhere to move,
without getting caught so he
does not move.
0-1-x
On the next move, Tien will
catch AH-FAC. That is two
streets moved.
1 minTurns(4, 1, 4 T-A-2-3
4 1, 0, [(0, 1), Initial starting state, where c is
1 (1, 2), (2, Tien and r is AH-FAC.
1 3)])
0
T-1-A-3
0 1
AH-FAC moves away from Tien.
1 2
2 3
0-T-A-3
Tien moves closer to AH-FAC.
0-T-2-A
AH-FAC moves away from Tien.
0-1-T-A
Tien moves closer to AH-FAC.
A-1-T-3
AH-FAC cannot move without
being caught next round so
they teleport to node 0.
A-T-2-3
Tien moves closer to AH-FAC.
A-T-2-3
AH-FAC cannot make any more
moves without being caught.
x-1-2-3
Tien catches AH-FAC.
8. Michelle’s Molasses Manos
Oh no! AI robots are on the hunt for Michelle, and
they have invaded her home! Luckily, she sees them
coming and calls for help. Unfortunately, no one
answers. Locked in her room and shaking with fear,
Michelle realizes she must come up with a plan to
stop the robots. She decides to turn to her laptop, as
it's unlikely she'll be able to stop the robots by force.
She gains access to the AI network and discovers she
can shut down the robots in her house if she types in
a compressed secret message composed entirely of
a's, b's and c's. To save memory, the secret message
was compressed into a character array c1, c2, ..., cn
and a length array l1, l2, ..., ln such that the ith character ci is repeated li times. For example, if c
= [a, c, b] and l = [2, 1, 3], the uncompressed message is 'aacbbb'.
Unfortunately, Michelle is SO slow and can only type one letter in 1 second. However, she can
take 1 second to copy a substring of the text she has already typed as long as each letter of
the substring is the same. For example, if Michelle has typed 'abaabbb', the valid substrings
she can copy are 'a', 'aa', 'b', 'bb', 'bbb'}.
Although Michelle can only copy once, she can paste the substring as many times as she likes,
where pasting the substring also takes 1 second. Given a secret message and these
constraints, find the shortest amount of time it will take Michelle to type out the message and
shut down the robots.
Implementation Details
Implement the following function. It will be called by the grader once for each test case.
Constraints
There are T test cases. The sum of all N over the T test cases <= 2*105
The sum over all string lengths is <= 2 * 106
Input
The first line contains a single integer t (1 <= t <= 1000), denoting the number of test cases.
For each test case, the first line contains a single integer n (1 <= n <= 2 * 105) denoting the size of the
length and character arrays. The second line contains n characters c1, c2, …, cn, where ci is an element
of {a, b, c}, and the third line contains n integers l1, l2, …, ln (1 <= li <= 106).
We guarantee that the sum over all the lengths of the uncompressed messages does not exceed 2*10 6.
Output
For each test case, output a single integer representing the amount of time it will take Michelle to type
out the uncompressed message.
Sample Input Parsed Input Sample Explanation
Output
With electric shock bars coming closer, help her figure out the passcode before it's too late.
Implementation Details
passcode(arr, k):
- arr: an array of integers
- k: an integer
Your function should return an integer.
Constraints
6
2 ≤∨arr ∨≤ 10
0 ≤ arr [i] ≤1 06
Input
The first line contains a single integer t (1 <= t <= 104), denoting the number of test cases.
The first line of each test case contains two space-separated integers n and k (2 <= n <=106, 1 <= k <=
106)$, denoting the number of elements and value of k for ∗.
The second line of each test case contains n integers a1, a2, … ,an (1<= ai <=106), denoting the elements
of the array a.
We guarantee that the sum of n across all test cases does not exceed 106.
Output
For each test case, print a single integer - the passcode as above.
1 passcode([13, 5], 14
2 3 3)
13 5
1 passcode([5,3],4) 7
2 4
5 3
1 passcode([17,26,11] 62
3 7 ,7)
17 26 11
1 passcode([17,22,29, 173
5 9 3,30],9)
17 22 29 3 30
10. Ziwen’s Zapper
Implementation Details
Implement the following function. It will be called by the grader once for each test case.
Input
The first line represents the number of test cases T.
For each test case:
The second line contains n, the number of batteries (arr1).
The third line contains arr1, which consists of n space separated integers.
The fourth line contains m, the number of computers (arr2).
The fifth line contains arr2, which consists of m space separated integers
Output
For each test case return true if we can power on Ziwen’s Zapper else return false.
In addition to the walls, there are k bombs that the AI have planted (bomb coordinates are
not necessarily unique). A bomb at cell (i, j) means it is surrounded by coordinates (i, j), (i+1,
j), (i, j+1), and (i+1, j+1).
If the AI ignites a bomb, all bombs in the same region will go off. Each bomb's power is equal
to the number of cells in the region that it is in and the damage in the Metaverse is equal to
the sum of all ignited bombs' power. The mischievous AI will always ignite one and only one
bomb that causes the most damage in the Metaverse.
Ben has enough resources to construct one more horizontal wall anywhere of any size in the
Metaverse before the AI chooses a bomb to ignite. He needs to try to minimize the amount of
damage the AI will cause. Can you help Ben determine the minimum amount of damage that
will occur in the Metaverse?
Implementation Details
Implement the following function. It will be called by the grader once for each test case.
Constraints
6
1 ≤r , c ≤3∗1 0
1 ≤n ≤ 1000
1 ≤ k ≤ 100000
Input
The first line contains two space-separated integers r and c (1<= r,c <= 3*106)$.
The second line contains two space-separated integers n and k (1 <= n <= 1000, 1 <= k <= 105).
The next n lines contain four integers rs, cs, re, ce (0<= r_s,r_e <= r and 0 <= cs, ce <= c), the coordinates
for one wall with start s and end e.
The next k lines contain two integers rb, cb (0 <= rb< r and 0<= cb < c)$, the coordinates for one bomb.
Output
Print a single integer - the minimum damage in the Metaverse after building an extra wall.
Sample Input Parsed Input Sample Explanation
Output
7 metaverse(7, 7, 4, 5, 34
7 [[1, 1, 4, 1], [1, 1,
4 1, 3], [1, 3, 4, 3],
5 [4, 1, 4, 3]], [[2,
1 1 4 1 2], [3, 2], [2, 5],
1 1 1 3 [2, 6], [3, 6]])
1 3 4 3
4 1 4 3
2 2
3 2
2 5 An illustration of the sample
2 6 test case. It is best to place a
3 6 horizontal wall from (3,0) to
(3,7) to reduce the maximum
damage to 34.
12. Sheltering Survivors
Expert industrial engineer, Brian, wants to create
shelters for the survivors of the AI apocalypse. He
wants to make sure each shelter has electricity. Brian
has found n locations for shelters and m locations for
power plants. Brian has extension cords that are 10
miles long. This means that a power plant can only
power a shelter that is at most 10 miles away.
Help Brian figure out how many pairs exist in this maximized arrangement. Note that if there
does not exist a valid assignment such that each shelter gets at least q power plants then we
say there are 0 pairs in the maximized arrangement.
Implementation Details
Implement the following function. It will be called by the grader once for each test case.
Input
The first line contains a single integer t (1 <= t <= 10), denoting the number of test cases.
For each test case, the first line contains a single integer m (1 <= m <= 100), the number of power
plants.
The second line contains a single integer n (1 <= n <= 100), the number of shelters.
The next m lines contain two integers, px j, pyj (-105 <= pxj, pyj <= 105), the coordinates (pxj, pyj) in miles
of power plant j.
The next n lines contain two integers, sxi, syi (-105 <= sxi, syi <= 105), the coordinates (sxi, syi) in miles of
shelter i.
The next line contains a single integer p (1 <= p <= 100), the maximum number of shelters that a power
plant can be paired with.
The next line contains a single integer q (1 <= q <= 100), the minimum number of power plants that a
shelter can be paired with.
The next line contains a single integer o (q <= o <= 100), the maximum number of power plants that a
shelter can be paired with.
We guarantee that the sum over all n does not exceed 100 and the sum over all m does not exceed
100.
Output
For each test case, print a single integer - the maximum number of shelter and power plant pairings.
Sample Input Parsed Input Sample Explanation
Output