NCPC 2024 Solutions Overview
NCPC 2024 Solutions Overview
October 5, 2024
NCPC 2024 solutions
Problems prepared by
Solution
Solution
1 Most greedy solutions succeed.
Solution
1 Most greedy solutions succeed.
2 Subtract twos until sum is a multiple of threes, then print threes, or vice versa.
Solution
1 Most greedy solutions succeed.
2 Subtract twos until sum is a multiple of threes, then print threes, or vice versa.
Solution
1 Most greedy solutions succeed.
2 Subtract twos until sum is a multiple of threes, then print threes, or vice versa.
Statistics at 4-hour mark: 394 submissions, 209 accepted, rst after 00:02
Solutiom
Solutiom
1 We place the pattern in the centre, reaching p /2 away from the middle in either
direction. Then we have (n − p )/2 left on either side.
Solutiom
1 We place the pattern in the centre, reaching p /2 away from the middle in either
direction. Then we have (n − p )/2 left on either side.
2 We take this modulo p to get the leftover space on one side, so multiply by two to
get our answer.
Solutiom
1 We place the pattern in the centre, reaching p /2 away from the middle in either
direction. Then we have (n − p )/2 left on either side.
2 We take this modulo p to get the leftover space on one side, so multiply by two to
get our answer.
3 The only exception is if we t one more pattern exactly in the middle on the back,
so when the leftover space is exactly p or equivalently when p divides n. In this
case the answer is zero instead.
Solutiom
1 We place the pattern in the centre, reaching p /2 away from the middle in either
direction. Then we have (n − p )/2 left on either side.
2 We take this modulo p to get the leftover space on one side, so multiply by two to
get our answer.
3 The only exception is if we t one more pattern exactly in the middle on the back,
so when the leftover space is exactly p or equivalently when p divides n. In this
case the answer is zero instead.
Statistics at 4-hour mark: 687 submissions, 152 accepted, rst after 00:04
Problem
Given a starting point, target point, and one point in a rectangular swimming pool, nd
a way to walk from the start to the target without intersecting the pool.
Solution
Problem
Given a starting point, target point, and one point in a rectangular swimming pool, nd
a way to walk from the start to the target without intersecting the pool.
Solution
1 Observation 1
You can't intersect the pool if you are outside of −104 ≤ x , y ≤ 104
Problem
Given a starting point, target point, and one point in a rectangular swimming pool, nd
a way to walk from the start to the target without intersecting the pool.
Solution
1 Observation 1
You can't intersect the pool if you are outside of −104 ≤ x , y ≤ 104
2 Observation 2
You can't intersect the pool while walking away from the pool point.
Statistics at 4-hour mark: 412 submissions, 126 accepted, rst after 00:16
Given the starting money, salary and salary frequency of a scally unsound man, nd
out how long he has before he goes broke (if ever).
Solution
Given the starting money, salary and salary frequency of a scally unsound man, nd
out how long he has before he goes broke (if ever).
Solution
1 There are two ways to go about this. The rst is to notice that the number of
days Svalur lasts can not be so high, and simply simulate ≈ 106 days and return an
answer, answering innite if he has not gone broke yet.
Given the starting money, salary and salary frequency of a scally unsound man, nd
out how long he has before he goes broke (if ever).
Solution
1 There are two ways to go about this. The rst is to notice that the number of
days Svalur lasts can not be so high, and simply simulate ≈ 106 days and return an
answer, answering innite if he has not gone broke yet.
2 The other is to do things more by hand. Let b(x ) be the number of binary digits in
x . Then to check if he goes broke before his rst payment, check whether
b(m) < d . To check whether he never goes broke, check whether d ≤ b(s ). Then
if b (d ) > 12 just print b (m ). Finally simulate the salary periods one by one and
print when he runs out of money.
Given the starting money, salary and salary frequency of a scally unsound man, nd
out how long he has before he goes broke (if ever).
Solution
1 There are two ways to go about this. The rst is to notice that the number of
days Svalur lasts can not be so high, and simply simulate ≈ 106 days and return an
answer, answering innite if he has not gone broke yet.
2 The other is to do things more by hand. Let b(x ) be the number of binary digits in
x . Then to check if he goes broke before his rst payment, check whether
b(m) < d . To check whether he never goes broke, check whether d ≤ b(s ). Then
if b (d ) > 12 just print b (m ). Finally simulate the salary periods one by one and
print when he runs out of money.
Solve the longest common subsequence problem when the number of occurrences of
any specic value is ≤ 15.
Solution
Solve the longest common subsequence problem when the number of occurrences of
any specic value is ≤ 15.
Solution
1 We consider the classic dynamic programming solution and the space optimization
of that solution.
Solve the longest common subsequence problem when the number of occurrences of
any specic value is ≤ 15.
Solution
1 We consider the classic dynamic programming solution and the space optimization
of that solution.
2 Next we place this single row of the dynamic programming memoization table into
a data structure like a segment tree or fenwick tree that can query maximum
values and update points.
Solve the longest common subsequence problem when the number of occurrences of
any specic value is ≤ 15.
Solution
1 We consider the classic dynamic programming solution and the space optimization
of that solution.
2 Next we place this single row of the dynamic programming memoization table into
a data structure like a segment tree or fenwick tree that can query maximum
values and update points.
3 Now the number of updates we have to make in the tree is bounded due to the
few occurrences of a given value.
Solve the longest common subsequence problem when the number of occurrences of
any specic value is ≤ 15.
Solution
1 We consider the classic dynamic programming solution and the space optimization
of that solution.
2 Next we place this single row of the dynamic programming memoization table into
a data structure like a segment tree or fenwick tree that can query maximum
values and update points.
3 Now the number of updates we have to make in the tree is bounded due to the
few occurrences of a given value.
Compute the faces of the provided planar graph, and nd the areas (squared) of all
faces (except the outer face).
Solution
Compute the faces of the provided planar graph, and nd the areas (squared) of all
faces (except the outer face).
Solution
1 For each vertex, sort its neighborhood counter-clockwise (or cw).
Compute the faces of the provided planar graph, and nd the areas (squared) of all
faces (except the outer face).
Solution
1 For each vertex, sort its neighborhood counter-clockwise (or cw).
2 For each edge uv , nd the left-hand face of uv by going from u to v , and then go
to the next neighbor (after u) of v, and repeat until you return to u .
Compute the faces of the provided planar graph, and nd the areas (squared) of all
faces (except the outer face).
Solution
1 For each vertex, sort its neighborhood counter-clockwise (or cw).
2 For each edge uv , nd the left-hand face of uv by going from u to v , and then go
to the next neighbor (after u) of v, and repeat until you return to u .
Compute the faces of the provided planar graph, and nd the areas (squared) of all
faces (except the outer face).
Solution
1 For each vertex, sort its neighborhood counter-clockwise (or cw).
2 For each edge uv , nd the left-hand face of uv by going from u to v , and then go
to the next neighbor (after u) of v, and repeat until you return to u .
4 Detect the outer face (the leftmost vertex (break even on y coordinate) and its
neighbor with angle highest ≤ 90◦ )
Compute the faces of the provided planar graph, and nd the areas (squared) of all
faces (except the outer face).
Solution
1 For each vertex, sort its neighborhood counter-clockwise (or cw).
2 For each edge uv , nd the left-hand face of uv by going from u to v , and then go
to the next neighbor (after u) of v, and repeat until you return to u .
4 Detect the outer face (the leftmost vertex (break even on y coordinate) and its
neighbor with angle highest ≤ 90◦ )
5 Use the shoelace algorithm for computing the area of each face/polygon.
You are given N forbidden pairs (Pi , Si ) such that 1 ≤ Pi , Si ≤ 2N . Find N dierent
pairs (pi , si ) satisfying 1 ≤ pi , si ≤ N , such that (pi + pj , si + sj ) is never forbidden.
Solution
You are given N forbidden pairs (Pi , Si ) such that 1 ≤ Pi , Si ≤ 2N . Find N dierent
pairs (pi , si ) satisfying 1 ≤ pi , si ≤ N , such that (pi + pj , si + sj ) is never forbidden.
Solution
1 Case 1.
If there is an even number S such that (P , S ) is never forbidden, then you can
answer (1, S /2), (2, S /2), . . . , (N , S /2).
You are given N forbidden pairs (Pi , Si ) such that 1 ≤ Pi , Si ≤ 2N . Find N dierent
pairs (pi , si ) satisfying 1 ≤ pi , si ≤ N , such that (pi + pj , si + sj ) is never forbidden.
Solution
1 Case 1.
If there is an even number S such that (P , S ) is never forbidden, then you can
answer (1, S /2), (2, S /2), . . . , (N , S /2).
2 Case 2.
If there is an even number P such that (P , S ) is never forbidden, then you can
answer (P /2, 1), (P /2, 2), . . . , (P /2, N ).
You are given N forbidden pairs (Pi , Si ) such that 1 ≤ Pi , Si ≤ 2N . Find N dierent
pairs (pi , si ) satisfying 1 ≤ pi , si ≤ N , such that (pi + pj , si + sj ) is never forbidden.
Solution
1 Case 1.
If there is an even number S such that (P , S ) is never forbidden, then you can
answer (1, S /2), (2, S /2), . . . , (N , S /2).
2 Case 2.
If there is an even number P such that (P , S ) is never forbidden, then you can
answer (P /2, 1), (P /2, 2), . . . , (P /2, N ).
3 Case 3:
Otherwise, both lists Pi and Si N.
are permutations of 2, 4, 6, . . . , 2
2 Otherwise, nd the unique forbidden point (2, S ). Add the points
(2, S /2), (3, S /2), . . . (N , S /2) to the solution. We now need to nd just one more
point.
2 Otherwise, nd the unique forbidden point (2, S ). Add the points
(2, S /2), (3, S /2), . . . (N , S /2) to the solution. We now need to nd just one more
point.
3 If S /2 is even, then either (1, 1) or (2, 1) will work. Otherwise, either (2, 1) or
(2, 2) will work. Try all four.
4 Running time: O (N ).
Question: If the order is randomized, does the above solution work with high
Given a bipartite graph where all but the last node in the left half have the same
degree, nd an edge-colouring such that each colour touches exactly one node in the
left half or only the last node in the left half.
Solution
Given a bipartite graph where all but the last node in the left half have the same
degree, nd an edge-colouring such that each colour touches exactly one node in the
left half or only the last node in the left half.
Solution
1 Ignore the last node for now. If such an edge colouring is possible, we can
construct it by greedily making one colour at a time. The only constraint is that
each colour must match every vertex on the right hand side of maximum degree.
Given a bipartite graph where all but the last node in the left half have the same
degree, nd an edge-colouring such that each colour touches exactly one node in the
left half or only the last node in the left half.
Solution
1 Ignore the last node for now. If such an edge colouring is possible, we can
construct it by greedily making one colour at a time. The only constraint is that
each colour must match every vertex on the right hand side of maximum degree.
2 This can be done with MCMF or by augmenting the standard bipartite match ow
graph to use normal ow algorithms.
Given a bipartite graph where all but the last node in the left half have the same
degree, nd an edge-colouring such that each colour touches exactly one node in the
left half or only the last node in the left half.
Solution
1 Ignore the last node for now. If such an edge colouring is possible, we can
construct it by greedily making one colour at a time. The only constraint is that
each colour must match every vertex on the right hand side of maximum degree.
2 This can be done with MCMF or by augmenting the standard bipartite match ow
graph to use normal ow algorithms.
3 The last row is dealt with by ignoring the Ys in the columns with the most Ys
already, so that it has as many Ys as the other rows.
Given a bipartite graph where all but the last node in the left half have the same
degree, nd an edge-colouring such that each colour touches exactly one node in the
left half or only the last node in the left half.
Solution
1 Ignore the last node for now. If such an edge colouring is possible, we can
construct it by greedily making one colour at a time. The only constraint is that
each colour must match every vertex on the right hand side of maximum degree.
2 This can be done with MCMF or by augmenting the standard bipartite match ow
graph to use normal ow algorithms.
3 The last row is dealt with by ignoring the Ys in the columns with the most Ys
already, so that it has as many Ys as the other rows.
Problem
Solution
Problem
Solution
1 First consider the case without the bounds a, b .
Problem
Solution
1 First consider the case without the bounds a, b .
2 We dene f (n , d ) as the number of partitions of n with ρi + i = d + 1 for the
aforementioned indices.
Problem
Solution
1 First consider the case without the bounds a, b .
2 We dene f (n , d ) as the number of partitions of n with ρi + i = d + 1 for the
aforementioned indices.
d
f (n, d ) = f (n − k (d + 1 − k ), d − k )
X
k =1
d
f (n, d ) = f (n − k (d + 1 − k ), d − k )
X
k =1
d
f (n, d ) = f (n − k (d + 1 − k ), d − k )
X
k =1
d
f (n, d ) = f (n − k (d + 1 − k ), d − k )
X
k =1
Given N = 20 random points in a grid, visit all of them in order while never intersecting
yourself.
Solution
Given N = 20 random points in a grid, visit all of them in order while never intersecting
yourself.
Solution
1 Greedily walk to each point? You will probably intersect yourself.
2 Also avoid visiting the same point twice? The plane will probably get divided into
disjoint regions where future points can't be visited.
Given N = 20 random points in a grid, visit all of them in order while never intersecting
yourself.
Solution
1 Greedily walk to each point? You will probably intersect yourself.
2 Also avoid visiting the same point twice? The plane will probably get divided into
disjoint regions where future points can't be visited.
3 Main idea: Try to make a snake region that doesn't contain any holes. This way,
points will not become unreachable.
Solution
2 The snake region will probably create 1x1 holes, some care is needed to make sure
you don't walk into a dead end.
Given a program that prints every unique substring of a string along with its number of
occurrences, nd out how many copies of each non-whitespace character that program
would print for a given string.
Solution
Given a program that prints every unique substring of a string along with its number of
occurrences, nd out how many copies of each non-whitespace character that program
would print for a given string.
Solution
1 To solve this we need sux arrays and segment trees. We start by constructing the
sux array and its longest common prex array.
Given a program that prints every unique substring of a string along with its number of
occurrences, nd out how many copies of each non-whitespace character that program
would print for a given string.
Solution
1 To solve this we need sux arrays and segment trees. We start by constructing the
sux array and its longest common prex array.
Given a program that prints every unique substring of a string along with its number of
occurrences, nd out how many copies of each non-whitespace character that program
would print for a given string.
Solution
1 To solve this we need sux arrays and segment trees. We start by constructing the
sux array and its longest common prex array.
3 For the letters we use a lazy propagation segment tree that allows for a range
update where the rst element is incremented by b, the next by a + b, the third by
2a+b and so on.
2 The collect operation might be slow worst case, but is amortized fast. We let the
values of the segment tree count the number of occurrences of the substrings
starting at our current position in the string as we iterate through it.
2 The collect operation might be slow worst case, but is amortized fast. We let the
values of the segment tree count the number of occurrences of the substrings
starting at our current position in the string as we iterate through it.
2 The collect operation might be slow worst case, but is amortized fast. We let the
values of the segment tree count the number of occurrences of the substrings
starting at our current position in the string as we iterate through it.
4 Everything but the rst L substrings are no longer valid, so we count those and
delete them using the collect operation on indices L to n − 1.
2 The collect operation might be slow worst case, but is amortized fast. We let the
values of the segment tree count the number of occurrences of the substrings
starting at our current position in the string as we iterate through it.
4 Everything but the rst L substrings are no longer valid, so we count those and
delete them using the collect operation on indices L to n − 1.
5 Next we add the substrings found at our current position, incrementing the values
at indices 0 through n − S − 1.
2 The collect operation might be slow worst case, but is amortized fast. We let the
values of the segment tree count the number of occurrences of the substrings
starting at our current position in the string as we iterate through it.
4 Everything but the rst L substrings are no longer valid, so we count those and
delete them using the collect operation on indices L to n − 1.
5 Next we add the substrings found at our current position, incrementing the values
at indices 0 through n − S − 1.
6 This way we collect all digits in the output.
2 The collect operation might be slow worst case, but is amortized fast. We let the
values of the segment tree count the number of occurrences of the substrings
starting at our current position in the string as we iterate through it.
4 Everything but the rst L substrings are no longer valid, so we count those and
delete them using the collect operation on indices L to n − 1.
5 Next we add the substrings found at our current position, incrementing the values
at indices 0 through n − S − 1.
6 This way we collect all digits in the output.