0% found this document useful (0 votes)
91 views78 pages

NCPC 2024 Solutions Overview

Uploaded by

ashbourne9527
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views78 pages

NCPC 2024 Solutions Overview

Uploaded by

ashbourne9527
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Presentation of solutions

October 5, 2024
NCPC 2024 solutions
Problems prepared by

Hlíf Arnbjargardóttir (Atli's Mom)

Arnar Bjarni Arnarson (Reykjavik University)

Pål Grønås Drange (University of Bergen)

Atli Fannar Franklín (University of Iceland)

Nils Gustafsson (KTH Royal Institute of Technology)

Björn Martinsson (KTH Royal Institute of Technology)

Bergur Snorrason (University of Iceland)

NCPC 2024 solutions


C  Composed Rhythms
Problem

Given an integer n ≤ 106 , write it as a sum of twos and threes.

Solution

Author: Arnar Bjarni Arnarson NCPC 2024 solutions


C  Composed Rhythms
Problem

Given an integer n ≤ 106 , write it as a sum of twos and threes.

Solution
1 Most greedy solutions succeed.

Author: Arnar Bjarni Arnarson NCPC 2024 solutions


C  Composed Rhythms
Problem

Given an integer n ≤ 106 , write it as a sum of twos and threes.

Solution
1 Most greedy solutions succeed.

2 Subtract twos until sum is a multiple of threes, then print threes, or vice versa.

Author: Arnar Bjarni Arnarson NCPC 2024 solutions


C  Composed Rhythms
Problem

Given an integer n ≤ 106 , write it as a sum of twos and threes.

Solution
1 Most greedy solutions succeed.

2 Subtract twos until sum is a multiple of threes, then print threes, or vice versa.

3 Solve 2 x + 3y = n also works, but more involved since x , y ≥ 0.

Author: Arnar Bjarni Arnarson NCPC 2024 solutions


C  Composed Rhythms
Problem

Given an integer n ≤ 106 , write it as a sum of twos and threes.

Solution
1 Most greedy solutions succeed.

2 Subtract twos until sum is a multiple of threes, then print threes, or vice versa.

3 Solve 2 x + 3y = n also works, but more involved since x , y ≥ 0.

Statistics at 4-hour mark: 394 submissions, 209 accepted, rst after 00:02

Author: Arnar Bjarni Arnarson NCPC 2024 solutions


K  Knitting Pattern
Problem

Figure out how to centre a knitting pattern on a sweater.

Solutiom

Author: Hlíf Arnbjargardóttir NCPC 2024 solutions


K  Knitting Pattern
Problem

Figure out how to centre a knitting pattern on a sweater.

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.

Author: Hlíf Arnbjargardóttir NCPC 2024 solutions


K  Knitting Pattern
Problem

Figure out how to centre a knitting pattern on a sweater.

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.

Author: Hlíf Arnbjargardóttir NCPC 2024 solutions


K  Knitting Pattern
Problem

Figure out how to centre a knitting pattern on a sweater.

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.

Author: Hlíf Arnbjargardóttir NCPC 2024 solutions


K  Knitting Pattern
Problem

Figure out how to centre a knitting pattern on a sweater.

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

Author: Hlíf Arnbjargardóttir NCPC 2024 solutions


A  Avoiding the Abyss

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

Author: Nils Gustafsson NCPC 2024 solutions


A  Avoiding the Abyss

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

Author: Nils Gustafsson NCPC 2024 solutions


A  Avoiding the Abyss

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.

Author: Nils Gustafsson NCPC 2024 solutions


A  Avoiding the Abyss
Solution

Statistics at 4-hour mark: 412 submissions, 126 accepted, rst after 00:16

Author: Nils Gustafsson NCPC 2024 solutions


I  Innite Cash
Problem

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

Author: Arnar Bjarni Arnarson NCPC 2024 solutions


I  Innite Cash
Problem

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.

Author: Arnar Bjarni Arnarson NCPC 2024 solutions


I  Innite Cash
Problem

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.

Author: Arnar Bjarni Arnarson NCPC 2024 solutions


I  Innite Cash
Problem

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.

Statistics at 4-hour mark: 575 submissions, 81 accepted, rst after 00:12


Author: Arnar Bjarni Arnarson NCPC 2024 solutions
D  Double Deck
Problem

Solve the longest common subsequence problem when the number of occurrences of
any specic value is ≤ 15.

Solution

Author: Bergur Snorrason NCPC 2024 solutions


D  Double Deck
Problem

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.

Author: Bergur Snorrason NCPC 2024 solutions


D  Double Deck
Problem

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.

Author: Bergur Snorrason NCPC 2024 solutions


D  Double Deck
Problem

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.

Author: Bergur Snorrason NCPC 2024 solutions


D  Double Deck
Problem

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.

Statistics at 4-hour mark: 214 submissions, 23 accepted, rst after 00:10

Author: Bergur Snorrason NCPC 2024 solutions


F  Fence Fee
Problem

Compute the faces of the provided planar graph, and nd the areas (squared) of all
faces (except the outer face).

Solution

Author: Pål Grønås Drange NCPC 2024 solutions


F  Fence Fee
Problem

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).

Author: Pål Grønås Drange NCPC 2024 solutions


F  Fence Fee
Problem

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 .

Author: Pål Grønås Drange NCPC 2024 solutions


F  Fence Fee
Problem

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 .

3 Repeat the process for vu to nd the right-hand face.

Author: Pål Grønås Drange NCPC 2024 solutions


F  Fence Fee
Problem

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 .

3 Repeat the process for vu to nd the right-hand face.

4 Detect the outer face (the leftmost vertex (break even on y coordinate) and its
neighbor with angle highest ≤ 90◦ )

Author: Pål Grønås Drange NCPC 2024 solutions


F  Fence Fee
Problem

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 .

3 Repeat the process for vu to nd the right-hand face.

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.

Statistics at 4-hour mark: 61 submissions, 16 accepted, rst after 00:50


Author: Pål Grønås Drange NCPC 2024 solutions
J  Jungle Game
Problem

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

Author: Nils Gustafsson NCPC 2024 solutions


J  Jungle Game
Problem

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).

Author: Nils Gustafsson NCPC 2024 solutions


J  Jungle Game
Problem

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 ).

Author: Nils Gustafsson NCPC 2024 solutions


J  Jungle Game
Problem

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

Author: Nils Gustafsson NCPC 2024 solutions


J  Jungle Game
Case 3

Both lists Pi and Si are permutations of 2, 4, 6, . . . , 2 N.

Author: Nils Gustafsson NCPC 2024 solutions


J  Jungle Game
Case 3

Both lists Pi and Si are permutations of 2, 4, 6, . . . , 2 N.


1 If N = 1, it is impossible.

Author: Nils Gustafsson NCPC 2024 solutions


J  Jungle Game
Case 3

Both lists Pi and Si are permutations of 2, 4, 6, . . . , 2 N.


1 If N = 1, it is impossible.

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.

Author: Nils Gustafsson NCPC 2024 solutions


J  Jungle Game
Case 3

Both lists Pi and Si are permutations of 2, 4, 6, . . . , 2 N.


1 If N = 1, it is impossible.

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 ).

Author: Nils Gustafsson NCPC 2024 solutions


J  Jungle Game
Heuristic solutions

The intended solution is O (N ), but checking the answer takes O (N 2 ). Unfortunately,


this means that various O (N 2 ) heuristics can pass as well.
1 Iterate through every pair (p , s ) in a random order.

2 For each pair, check if it can be added to the answer.

Question: If the order is randomized, does the above solution work with high

probability (or should we have made better test data)?

Statistics at 4-hour mark: 35 submissions, 8 accepted, rst after 01:23

Author: Nils Gustafsson NCPC 2024 solutions


G  Guessing Passwords
Problem

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

Author: Atli Fannar Franklín NCPC 2024 solutions


G  Guessing Passwords
Problem

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.

Author: Atli Fannar Franklín NCPC 2024 solutions


G  Guessing Passwords
Problem

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.

Author: Atli Fannar Franklín NCPC 2024 solutions


G  Guessing Passwords
Problem

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.

Author: Atli Fannar Franklín NCPC 2024 solutions


G  Guessing Passwords
Problem

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.

Statistics at 4-hour mark: 11 submissions, 1 accepted, rst after 03:15


Author: Atli Fannar Franklín NCPC 2024 solutions
B  Baseball Court

Problem

Given bounds a, b how many partitions ρ of n satisfy |ρ| ≤ b, max ρ ≤ a and ρi + i is


constant across all indices i such that ρi 6= ρi +1 .

Solution

Author: Atli Fannar Franklín NCPC 2024 solutions


B  Baseball Court

Problem

Given bounds a, b how many partitions ρ of n satisfy |ρ| ≤ b, max ρ ≤ a and ρi + i is


constant across all indices i such that ρi 6= ρi +1 .

Solution
1 First consider the case without the bounds a, b .

Author: Atli Fannar Franklín NCPC 2024 solutions


B  Baseball Court

Problem

Given bounds a, b how many partitions ρ of n satisfy |ρ| ≤ b, max ρ ≤ a and ρi + i is


constant across all indices i such that ρi 6= ρi +1 .

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.

Author: Atli Fannar Franklín NCPC 2024 solutions


B  Baseball Court

Problem

Given bounds a, b how many partitions ρ of n satisfy |ρ| ≤ b, max ρ ≤ a and ρi + i is


constant across all indices i such that ρi 6= ρi +1 .

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.

3 The problem can now be solved using dynamic programming.

Author: Atli Fannar Franklín NCPC 2024 solutions


B  Baseball Court
Solution
1 Consider the maximum index k such that ρ1 = ρk and ρk 6= ρk +1 . Then
ρk + k = d + 1. Then by cutting o these front values we remove k values and a
sum of k (d + 1 − k ). This gives us the recurrence

d
f (n, d ) = f (n − k (d + 1 − k ), d − k )
X

k =1

Author: Atli Fannar Franklín NCPC 2024 solutions


B  Baseball Court
Solution
1 Consider the maximum index k such that ρ1 = ρk and ρk 6= ρk +1 . Then
ρk + k = d + 1. Then by cutting o these front values we remove k values and a
sum of k (d + 1 − k ). This gives us the recurrence

d
f (n, d ) = f (n − k (d + 1 − k ), d − k )
X

k =1

2 This is O(n3 ) which is not quite good enough. But f (n − k (d + 1 − k ), d − k ) has



the rst argument <0 for all but the rst and last n terms. So we can skip
those and get a time complexity of O(n2.5 ).

Author: Atli Fannar Franklín NCPC 2024 solutions


B  Baseball Court
Solution
1 Consider the maximum index k such that ρ1 = ρk and ρk 6= ρk +1 . Then
ρk + k = d + 1. Then by cutting o these front values we remove k values and a
sum of k (d + 1 − k ). This gives us the recurrence

d
f (n, d ) = f (n − k (d + 1 − k ), d − k )
X

k =1

2 This is O(n3 ) which is not quite good enough. But f (n − k (d + 1 − k ), d − k ) has



the rst argument <0 for all but the rst and last n terms. So we can skip
those and get a time complexity of O(n2.5 ).
3 Then we simply subtract all partitions that have |ρ| > a or max ρ > b, then add
back all the ones that violate both to get the right answer by inclusion-exclusion.

Author: Atli Fannar Franklín NCPC 2024 solutions


B  Baseball Court
Solution
1 Consider the maximum index k such that ρ1 = ρk and ρk 6= ρk +1 . Then
ρk + k = d + 1. Then by cutting o these front values we remove k values and a
sum of k (d + 1 − k ). This gives us the recurrence

d
f (n, d ) = f (n − k (d + 1 − k ), d − k )
X

k =1

2 This is O(n3 ) which is not quite good enough. But f (n − k (d + 1 − k ), d − k ) has



the rst argument <0 for all but the rst and last n terms. So we can skip
those and get a time complexity of O(n2.5 ).
3 Then we simply subtract all partitions that have |ρ| > a or max ρ > b, then add
back all the ones that violate both to get the right answer by inclusion-exclusion.

Statistics at 4-hour mark: 14 submissions, 0 accepted, rst after ??:??


Author: Atli Fannar Franklín NCPC 2024 solutions
E  Elapid Errands
Problem

Given N = 20 random points in a grid, visit all of them in order while never intersecting
yourself.

Solution

Author: Nils Gustafsson NCPC 2024 solutions


E  Elapid Errands
Problem

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.

Author: Nils Gustafsson NCPC 2024 solutions


E  Elapid Errands
Problem

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.

Author: Nils Gustafsson NCPC 2024 solutions


E  Elapid Errands

Solution

For each point P you want to visit:

1 Find a point Q on the snake boundary that is as close as possible to P.


2 Walk along the boundary of the snake to Q.
3 Greedily walk from Q to P. You will not intersect yourself since Q was as close as
possible to P.

Author: Nils Gustafsson NCPC 2024 solutions


E  Elapid Errands

Author: Nils Gustafsson NCPC 2024 solutions


E  Elapid Errands

Author: Nils Gustafsson NCPC 2024 solutions


E  Elapid Errands

Author: Nils Gustafsson NCPC 2024 solutions


E  Elapid Errands

Author: Nils Gustafsson NCPC 2024 solutions


E  Elapid Errands

Author: Nils Gustafsson NCPC 2024 solutions


E  Elapid Errands

Author: Nils Gustafsson NCPC 2024 solutions


E  Elapid Errands
Solution

For each point P you want to visit:

1 Find a point Q on the snake boundary that is as close as possible to P.


2 Walk along the boundary of the snake to Q.
3 Greedily walk from Q to P. You will not intersect yourself since Q was as close as
possible to P.
Issues
1 In the third step, it is possible that you accidentally pass too close to a future
point. Put a "danger zone" around every future point so that you don't step on
them too early.

2 The snake region will probably create 1x1 holes, some care is needed to make sure
you don't walk into a dead end.

Statistics at 4-hour mark: 25 submissions, 0 accepted, rst after ??:??


Author: Nils Gustafsson NCPC 2024 solutions
H  Hotx
Problem

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

Author: Atli Fannar Franklín NCPC 2024 solutions


H  Hotx
Problem

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.

Author: Atli Fannar Franklín NCPC 2024 solutions


H  Hotx
Problem

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.

2 We now solve the characters and numbers separately.

Author: Atli Fannar Franklín NCPC 2024 solutions


H  Hotx
Problem

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.

2 We now solve the characters and numbers separately.

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.

Author: Atli Fannar Franklín NCPC 2024 solutions


Solution
1 The segment tree will keep track of the number of occurrences of each character
in the input string as we iterate over the sux array. Let S be the i -th element of
the sux array and L be the (i − 1)-st element of the longest common prex array.

Author: Atli Fannar Franklín NCPC 2024 solutions


Solution
1 The segment tree will keep track of the number of occurrences of each character
in the input string as we iterate over the sux array. Let S be the i -th element of
the sux array and L be the (i − 1)-st element of the longest common prex array.
2 Then n − S − L new prexes start at position S . The rst L possible substrings
starting at S are duplicates. So we increment the segment tree by n − S − L at
positions S through S + L − 1. Then we increment the segment tree by n − S − L,
n − S − L − 1, n − S − L − 2, and so on at positions S + L through n − 1. At the
end of this all we can read o the number of occurrences of each character from
the segment tree.

Author: Atli Fannar Franklín NCPC 2024 solutions


Solution
1 Finally we consider the digits. For this we use a lazy propagation segment tree that
allows incrementing values on a range and then a collect operation that both
counts the digits in the values on a range before then zeroing them out.

Author: Atli Fannar Franklín NCPC 2024 solutions


Solution
1 Finally we consider the digits. For this we use a lazy propagation segment tree that
allows incrementing values on a range and then a collect operation that both
counts the digits in the values on a range before then zeroing them out.

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.

Author: Atli Fannar Franklín NCPC 2024 solutions


Solution
1 Finally we consider the digits. For this we use a lazy propagation segment tree that
allows incrementing values on a range and then a collect operation that both
counts the digits in the values on a range before then zeroing them out.

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.

3 As we move from position i −1 to i, let S and L be as before.

Author: Atli Fannar Franklín NCPC 2024 solutions


Solution
1 Finally we consider the digits. For this we use a lazy propagation segment tree that
allows incrementing values on a range and then a collect operation that both
counts the digits in the values on a range before then zeroing them out.

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.

3 As we move from position i −1 to i, let S and L be as before.

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.

Author: Atli Fannar Franklín NCPC 2024 solutions


Solution
1 Finally we consider the digits. For this we use a lazy propagation segment tree that
allows incrementing values on a range and then a collect operation that both
counts the digits in the values on a range before then zeroing them out.

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.

3 As we move from position i −1 to i, let S and L be as before.

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.

Author: Atli Fannar Franklín NCPC 2024 solutions


Solution
1 Finally we consider the digits. For this we use a lazy propagation segment tree that
allows incrementing values on a range and then a collect operation that both
counts the digits in the values on a range before then zeroing them out.

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.

3 As we move from position i −1 to i, let S and L be as before.

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.

Author: Atli Fannar Franklín NCPC 2024 solutions


Solution
1 Finally we consider the digits. For this we use a lazy propagation segment tree that
allows incrementing values on a range and then a collect operation that both
counts the digits in the values on a range before then zeroing them out.

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.

3 As we move from position i −1 to i, let S and L be as before.

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.

Statistics at 4-hour mark: 42 submissions, 0 accepted, rst after ??:??

Author: Atli Fannar Franklín NCPC 2024 solutions


Results!

NCPC 2024 solutions

You might also like