NCPC 17 Slides
NCPC 17 Slides
Presentation of solutions
The Jury
2017-10-07
solve(0, 0) :-
!, write('Not a moose').
solve(L, R) :-
type(L, R, Type),
Val is 2*max(L, R),
write(Type), write(' '), write(Val).
type(L, L, "Even") :- !.
type(_, _, "Odd").
Pick best relay team, given runners' standing and ying start times.
Solution
1 Pre-sort runners by their ying start time
Pick best relay team, given runners' standing and ying start times.
Solution
1 Pre-sort runners by their ying start time
Pick best relay team, given runners' standing and ying start times.
Solution
1 Pre-sort runners by their ying start time
Pick best relay team, given runners' standing and ying start times.
Solution
1 Pre-sort runners by their ying start time
Pick best relay team, given runners' standing and ying start times.
Solution
1 Pre-sort runners by their ying start time
Solution
1 Maintain a set S: the teams whose score is better than your
team's score. Your rank is |S | + 1.
Solution
1 Maintain a set S: the teams whose score is better than your
team's score. Your rank is |S | + 1.
2 When your team solves a problem, remove all teams with a
worse score from S.
Solution
1 Maintain a set S: the teams whose score is better than your
team's score. Your rank is |S | + 1.
2 When your team solves a problem, remove all teams with a
worse score from S.
3 When another team solves a problem, add it to S if its score
becomes better than your team's score.
Solution
1 Maintain a set S: the teams whose score is better than your
team's score. Your rank is |S | + 1.
2 When your team solves a problem, remove all teams with a
worse score from S.
3 When another team solves a problem, add it to S if its score
becomes better than your team's score.
Solution
1 Maintain a set S: the teams whose score is better than your
team's score. Your rank is |S | + 1.
2 When your team solves a problem, remove all teams with a
worse score from S.
3 When another team solves a problem, add it to S if its score
becomes better than your team's score.
Solution
1 Use the FloydWarshall all pairs shortest path algorithm with
diagonals initialized to ∞
Solution
1 Use the FloydWarshall all pairs shortest path algorithm with
diagonals initialized to ∞
2 Afterwards diagonal entry d (u , u ) gives length of shortest cycle
passing through u.
Solution
1 Use the FloydWarshall all pairs shortest path algorithm with
diagonals initialized to ∞
2 Afterwards diagonal entry d (u , u ) gives length of shortest cycle
passing through u.
3 Reconstruct shortest cycle using the distance matrix.
Solution
1 Use the FloydWarshall all pairs shortest path algorithm with
diagonals initialized to ∞
2 Afterwards diagonal entry d (u , u ) gives length of shortest cycle
passing through u.
3 Reconstruct shortest cycle using the distance matrix.
Solution
1 Use the FloydWarshall all pairs shortest path algorithm with
diagonals initialized to ∞
2 Afterwards diagonal entry d (u , u ) gives length of shortest cycle
passing through u.
3 Reconstruct shortest cycle using the distance matrix.
Solution
1 Use the FloydWarshall all pairs shortest path algorithm with
diagonals initialized to ∞
2 Afterwards diagonal entry d (u , u ) gives length of shortest cycle
passing through u.
3 Reconstruct shortest cycle using the distance matrix.
How much water can we drain from a point at the bottom of the
sea?
Solution
How much water can we drain from a point at the bottom of the
sea?
Solution
1 Similar to Dijkstra's or Prim's algorithms:
How much water can we drain from a point at the bottom of the
sea?
Solution
1 Similar to Dijkstra's or Prim's algorithms:
1 Keep track of tentative depth of each square upper bound on
the nal water level.
How much water can we drain from a point at the bottom of the
sea?
Solution
1 Similar to Dijkstra's or Prim's algorithms:
1 Keep track of tentative depth of each square upper bound on
the nal water level.
2 At the start, only the drainage point has known depth.
How much water can we drain from a point at the bottom of the
sea?
Solution
1 Similar to Dijkstra's or Prim's algorithms:
1 Keep track of tentative depth of each square upper bound on
the nal water level.
2 At the start, only the drainage point has known depth.
3 In each iteration, pick the square s with the lowest tentative
depth and mark it nal. Update tentative depth of all
neighbours of s .
How much water can we drain from a point at the bottom of the
sea?
Solution
1 Similar to Dijkstra's or Prim's algorithms:
1 Keep track of tentative depth of each square upper bound on
the nal water level.
2 At the start, only the drainage point has known depth.
3 In each iteration, pick the square s with the lowest tentative
depth and mark it nal. Update tentative depth of all
neighbours of s .
Time complexity is O (n log n), where n = w · h is the size of the
grid.
How much water can we drain from a point at the bottom of the
sea?
Solution
1 Similar to Dijkstra's or Prim's algorithms:
1 Keep track of tentative depth of each square upper bound on
the nal water level.
2 At the start, only the drainage point has known depth.
3 In each iteration, pick the square s with the lowest tentative
depth and mark it nal. Update tentative depth of all
neighbours of s .
Time complexity is O (n log n), where n = w · h is the size of the
grid.
Solution
1 k
There are a total of 2 possible bit vectors.
Solution
1 k
There are a total of 2 possible bit vectors.
Solution
1 k
There are a total of 2 possible bit vectors.
Solution
1 k
There are a total of 2 possible bit vectors.
Time complexity is O (n + k · 2k )
Solution
1 k
There are a total of 2 possible bit vectors.
Time complexity is O (n + k · 2k )
Statistics: 461 submissions, 40 accepted, rst after 00:17
Solution
Solution
1 When card is sold, at most 6 other cards (the 2 adjacent
cards of each color) can change their uniqueness values.
Solution
1 When card is sold, at most 6 other cards (the 2 adjacent
cards of each color) can change their uniqueness values.
Solution
1 When card is sold, at most 6 other cards (the 2 adjacent
cards of each color) can change their uniqueness values.
Solution
1 When card is sold, at most 6 other cards (the 2 adjacent
cards of each color) can change their uniqueness values.
Solution
1 When card is sold, at most 6 other cards (the 2 adjacent
cards of each color) can change their uniqueness values.
Solution
1 When card is sold, at most 6 other cards (the 2 adjacent
cards of each color) can change their uniqueness values.
Solution
Solution
1 Binary search over the answer.
Solution
1 Binary search over the answer.
2 Check feasibility by greedily assigning people to kayaks
kayaks requiring strong+strong or strong+normal get that
kayaks that can handle weak+weak or weak+normal get that
pair up remaining weaks with strongs and normals with
normals and check if this can make all kayaks fast enough
Solution
1 Binary search over the answer.
2 Check feasibility by greedily assigning people to kayaks
kayaks requiring strong+strong or strong+normal get that
kayaks that can handle weak+weak or weak+normal get that
pair up remaining weaks with strongs and normals with
normals and check if this can make all kayaks fast enough
Time complexity is O (n log n) for n people.
Solution
1 Binary search over the answer.
2 Check feasibility by greedily assigning people to kayaks
kayaks requiring strong+strong or strong+normal get that
kayaks that can handle weak+weak or weak+normal get that
pair up remaining weaks with strongs and normals with
normals and check if this can make all kayaks fast enough
Time complexity is O (n log n) for n people.
Solution
Solution
xi xj L
1 LetS (i ) be best time if we start from i 'th cart.
2 Easy dynamic programming: for each j > i , try buying next
coee at cart j
Solution
xi xj L
1 LetS (i ) be best time if we start from i 'th cart.
2 Easy dynamic programming: for each j > i , try buying next
coee at cart j
Solution
xi xj L
1 From each xi , three categories of choice for best next cart xj :
During cooldown, during drinking, and after nishing the coee
Solution
xi xj L
1 From each xi , three categories of choice for best next cart xj :
During cooldown, during drinking, and after nishing the coee
Solution
xi xj L
1 From each xi , three categories of choice for best next cart xj :
During cooldown, during drinking, and after nishing the coee
Solution
xi xj L
1 Only need to consider three values of j when computing S (i ).
Solution
xi xj L
1 Only need to consider three values of j when computing S (i ).
2 Can use binary search over cart positions to nd them in
O (log n) time.
Solution
xi xj L
1 Only need to consider three values of j when computing S (i ).
2 Can use binary search over cart positions to nd them in
O (log n) time.
Overall complexity O (n log n).
(Exercise: can be improved to O (n) time.)
Solution
xi xj L
1 Only need to consider three values of j when computing S (i ).
2 Can use binary search over cart positions to nd them in
O (log n) time.
Overall complexity O (n log n).
(Exercise: can be improved to O (n) time.)
Given a set of rays from in 2D (0, 0), and some points, assign max
#points to a ray of minimum angular distance from the point,
subject to ray capacities.
Phase 1
Given a set of rays from in 2D (0, 0), and some points, assign max
#points to a ray of minimum angular distance from the point,
subject to ray capacities.
Phase 1
1 Sort all rays and points by angle.
Given a set of rays from in 2D (0, 0), and some points, assign max
#points to a ray of minimum angular distance from the point,
subject to ray capacities.
Phase 1
1 Sort all rays and points by angle.
Given a set of rays from in 2D (0, 0), and some points, assign max
#points to a ray of minimum angular distance from the point,
subject to ray capacities.
Phase 1
1 Sort all rays and points by angle.
Given a set of rays from in 2D (0, 0), and some points, assign max
#points to a ray of minimum angular distance from the point,
subject to ray capacities.
Phase 1
1 Sort all rays and points by angle.
Given a set of rays from in 2D (0, 0), and some points, assign max
#points to a ray of minimum angular distance from the point,
subject to ray capacities.
Phase 1
1 Sort all rays and points by angle.
Given a set of rays from in 2D (0, 0), and some points, assign max
#points to a ray of minimum angular distance from the point,
subject to ray capacities.
Phase 2
1 For remaining points (having two closest rays) we get a
bipartite matching problem between points and rays.
Given a set of rays from in 2D (0, 0), and some points, assign max
#points to a ray of minimum angular distance from the point,
subject to ray capacities.
Phase 2
1 For remaining points (having two closest rays) we get a
bipartite matching problem between points and rays.
Given a set of rays from in 2D (0, 0), and some points, assign max
#points to a ray of minimum angular distance from the point,
subject to ray capacities.
Phase 2
1 For remaining points (having two closest rays) we get a
bipartite matching problem between points and rays.
Given a set of rays from in 2D (0, 0), and some points, assign max
#points to a ray of minimum angular distance from the point,
subject to ray capacities.
Phase 2
1 For remaining points (having two closest rays) we get a
bipartite matching problem between points and rays.
Given a set of rays from in 2D (0, 0), and some points, assign max
#points to a ray of minimum angular distance from the point,
subject to ray capacities.
Phase 2
1 For remaining points (having two closest rays) we get a
bipartite matching problem between points and rays.
Given a set of rays from in 2D (0, 0), and some points, assign max
#points to a ray of minimum angular distance from the point,
subject to ray capacities.
Phase 2
1 For remaining points (having two closest rays) we get a
bipartite matching problem between points and rays.
Given a set of rays from in 2D (0, 0), and some points, assign max
#points to a ray of minimum angular distance from the point,
subject to ray capacities.
Phase 2
1 For remaining points (having two closest rays) we get a
bipartite matching problem between points and rays.
Given a set of rays from in 2D (0, 0), and some points, assign max
#points to a ray of minimum angular distance from the point,
subject to ray capacities.
Phase 2
1 For remaining points (having two closest rays) we get a
bipartite matching problem between points and rays.
Given a set of rays from in 2D (0, 0), and some points, assign max
#points to a ray of minimum angular distance from the point,
subject to ray capacities.
Phase 2
1 For remaining points (having two closest rays) we get a
bipartite matching problem between points and rays.
Given a set of rays from in 2D (0, 0), and some points, assign max
#points to a ray of minimum angular distance from the point,
subject to ray capacities.
Phase 2
1 For remaining points (having two closest rays) we get a
bipartite matching problem between points and rays.
Solution
Solution
2 If k > 30, only relevant part is bottom-left copy of F30 and the
path to this subtree.
Solution
2 If k > 30, only relevant part is bottom-left copy of F30 and the
path to this subtree.
Solution
F0 F2
1
2 3
4 5
a=
Solution
F0 F2
1 1
2 3
2 3
4 5
4 5
a = (5,
Solution
F0 F2
1
2 3
1
4 5
2 3
a 4 5
a = (5, 2,
Solution
F0 F2
1
2 3
4 5
1
2 3
a = (5, 2, 3) 4 5
Solution
F0 F2
1
2 3
4 5
a = (5, 2, 3)
Solution
1 What is distance between (a1 , . . . , ap ) and (b1 , . . . , bq )?
Solution
1 What is distance between (a1 , . . . , ap ) and (b1 , . . . , bq )?
2 First remove any common prex
(moving into the same subtree does not aect distance)
Solution
1 What is distance between (a1 , . . . , ap ) and (b1 , . . . , bq )?
2 First remove any common prex
(moving into the same subtree does not aect distance)
3 Then, when a1 6= b1 , distance is
p q
d ( a1 , b 1 ) + h(a ) + h(b )
X X
i i
i =2 i =2
4 d (a1 , b1 ) = distance between a1 and b1 in F0
(can compute it using a lowest common ancestor (LCA) algorithm)
5 h(x ) = depth of node x in F0
Solution
1 What is distance between (a1 , . . . , ap ) and (b1 , . . . , bq )?
2 First remove any common prex
(moving into the same subtree does not aect distance)
3 Then, when a1 6= b1 , distance is
p q
d ( a1 , b 1 ) + h(a ) + h(b )
X X
i i
i =2 i =2
4 d (a1 , b1 ) = distance between a1 and b1 in F0
(can compute it using a lowest common ancestor (LCA) algorithm)
5 h(x ) = depth of node x in F0
Statistics: 5 submissions, 0 accepted
Problem Author: Johan Sannemo NCPC 2017 solutions
Random numbers
253 teams
611 contestants
Exceptions:
Exceptions:
D (Distinctive Character) O (n + k · 2k ) solution
I (Import Spaghetti) O (n 3 ) solution.
Exceptions:
D (Distinctive Character) O (n + k · 2k ) solution
I (Import Spaghetti) O (n 3 ) solution.
Exceptions:
D (Distinctive Character) O (n + k · 2k ) solution
I (Import Spaghetti) O (n 3 ) solution.
Exceptions:
D (Distinctive Character) O (n + k · 2k ) solution
I (Import Spaghetti) O (n 3 ) solution.