0% found this document useful (0 votes)
13 views20 pages

Day1 Solutions

The document outlines various computational problems and their solutions, including 2-SAT, Castle Exploration, and others. Each problem is summarized with its requirements and proposed algorithms, detailing complexity and methodologies for optimization. The problems range from satisfiability in logic to pathfinding in trees and dynamic programming for probabilistic outcomes.

Uploaded by

Antek Iwanowski
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)
13 views20 pages

Day1 Solutions

The document outlines various computational problems and their solutions, including 2-SAT, Castle Exploration, and others. Each problem is summarized with its requirements and proposed algorithms, detailing complexity and methodologies for optimization. The problems range from satisfiability in logic to pathfinding in trees and dynamic programming for probabilistic outcomes.

Uploaded by

Antek Iwanowski
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
You are on page 1/ 20

Problem Analysis

2 A. 2-SAT
 Problem Summary:
► Given a 2-CNF formulae, find its k distinct satisfying assignments (if there are at least k, otherwise all of
them).

 Solution:
► We start with checking if the formula is satisfiable, and determining for every variable v if all satisfying
assignments set v to the same value.
► After outputting the initial satisfying assignment, we recursively generate more assignments as follows.
► We choose a variable v such that v can be set to false in some satisfying assignment, and also to true in
some other satisfying assignment. We branch on both possibilities, and recurse.
► After fixing the value of v, we appropriately fix the values of other variables that are implied by this choice.
► The overall time is O(kn).
3 B. Castle Exploration
 Problem Summary:
► Given a tree with n nodes, where nodes from 1 to k are protected by node p_i (meaning you must first
reach p_i before entering i), and the edges of the tree have weights. For all nodes with numbers greater
than k, find the shortest path that passes through all nodes and satisfies the above conditions, or report
that such a path does not exist.

 Solution:
► Brute force method: Call {1,2,…,k} and {p_1,p_2,…,p_k} the key nodes. For each starting point S, enumerate
the ending point T, then S, T and all points in the key nodes form a virtual tree. Obviously, all edges not on
the virtual tree must be passed twice in the optimal path. Enumerating the order of passing through the
points on the virtual tree can brute force the optimal solution, but the complexity is unacceptable.
► Optimization 1: The enumeration order can be completed in O(k^3·3^k) time by state compression dynamic
programming. However, when enumerating the end point and calculating the answer, the complexity is
O(n^2·k^2+k^3·3^k), which still cannot be accepted.
4 B. Castle Exploration
► Optimization 2: Consider when the starting point S and the last key point u passed are fixed, the
contribution of the endpoint T to the shortest path equals to dist(u,v)-dist(v,T), where v is the last point on
the virtual tree formed by S and all key points on the path from u to T. When v is fixed, T must choose the
point that maximizes dist(v,T) to be optimal.
► Perform the following preprocessing: maintain the maximum distance within all subtrees connected by
edges that are not part of the virtual tree on each point with a set, and then take each key point as u, use
depth-first search to enumerate v, and maintain the optimal contribution from u to T.
► Details: The preprocessing considers the virtual tree before adding S, and some modifications are needed
each time S is added.
► Complexity: The complexity of dynamic programming is O(k^3·3^k), and the complexity of calculating the
answer is O(nk^2+nklogn).
5 C. On the Theory of Division
 Problem Summary:
► Given a positive integer n, find the number of positive integer pairs (a, b) that satisfy:
● a^2+ab+b^2 | ab(a+b) and 0 < |a-b| <= n.

 Solution:
► Without loss of generality, let's assume a>b. Transforming ab(a+b)=k(a^2+ab+b^2) we get a^3/b^3 =(a-k)/
(b-k). Write a and b as pd and qd, where d=gcd(a,b), the original equation becomes p^3/q^3=(pd-k)/(qd-k).
The difference between the numerator and denominator on the right must be a multiple of the difference
on the left, i.e., (p-q)d is a multiple of p^3-q^3.
► Enumerate p and q, calculate how many d satisfy that (p-q)d is a multiple of p^3-q^3 and (p-q)d<=n, there
are n/(p^3-q^3) such d.
► Considering the range of p, p^3-q^3<=n, i.e., p^3-(p-1)^3<=n is the condition for possible solutions. The left
side is of the order of p^2, so enumerating p is O(sqrt(n)), enumerating q is also O(sqrt(n)), calculating
gcd(p,q) (to ensure p, q are coprime) is O(log(n)), the overall complexity is O(nlogn).
6 D. City Skylines
 Problem Summary:
► There are some buildings (rectangular, opaque obstacles) on the ground, and a TV tower (a point light
source higher than all buildings). The task is to find the shortest path on the ground between a given start
and end point, where all points along the path can see the TV tower (are illuminated by the point light
source). N<=50.

 Solution:
► Consider each obstructing building. Its shadow on the ground is a convex pentagon (when two vertices
and the TV tower project to the ground on the same line) or a convex hexagon. The shadowed area is our
obstacle, and the problem is transformed into a two-dimensional one.
► Take out the vertices of these polygons as key points. The turning points of the path must pass through
these points. Construct a graph with the key points as vertices, and if the line connecting two points does
not pass through an obstacle, connect them with an edge.
7 D. City Skylines
► The method to determine whether two points can be connected is to consider whether it would pass
through the shadow, whether it's in the shadow, whether it intersects on the edge, the point, the
difference between the interior and exterior, and the handling of collinearity.
► The shortest path algorithm can use Floyd's algorithm. The complexity bottleneck is in the edge connection, which
is O(N^3log N).
8 E: Select Box
 Problem Summary:
► There are n people and n boxes, each box randomly contains a distinct number from 1 to n. Each person
independently opens k boxes. If everyone opens the box with their own number, the game is won. Each
person can adjust their strategy based on the information they know, but once the game starts, there can
be no communication between individuals. The problem asks for the maximum probability of winning the
game under the optimal strategy. k ≤ n ≤ 10^6.

 Solution:
► It can be proven that an optimal strategy is for each person to first open the box with their own number,
and then open the box with the number they find, until they find their own number. Under this strategy,
consider the box and the number inside as a permutation. The winning scenario is then transformed into
finding the number of n permutations that satisfy the condition, where the size of all cycles does not
exceed k.
9 E: Select Box
► This can be solved with dynamic programming. For each cycle, require that the cycle must include the
smallest remaining number to avoid redundancy. The following recursion formula can be written:

( − − 1) !
( − − 1)
− −1
[( − − 1) !]
∑ ∑
= =
− ≤ − ≤
( − )!

► It can be seen that the transition of f_j in the recursion formula only depends on j, so the time complexity
is O(n).
𝑖
𝑗
𝑘
𝑖
𝑗
𝑘
𝑖
𝑗
𝑛
𝑖
𝑖
𝑗
𝑗
𝑓
𝑓
𝑖
𝑗
𝑓
𝑛
𝑗
𝑛
𝑗
10 E: Select Box
► Proof of Optimal Strategy:
► Suppose there is a new game: Participant No.1 goes first, followed by No.2, No.3, and so on. Each time they
open a box, if they do not find their own number, they open another one until they find their number.
► The difference is that each participant, after finishing their own game, can go back and tell everyone what
numbers were in the boxes they opened. The participants corresponding to these numbers automatically
win and exit the game. As a cost, a participant must also stop immediately and return after finding their
own number. If every participant who plays the game finds their own number within k times, then the game
is won, otherwise it is lost.
► Since each box is only opened once, we can record the numbers opened each time in order, obtaining a
permutation _1, _2,…, _ , and we can use this permutation to restore the number of times each person
opened a box. This permutation _1, _2,…, _ is also completely random, so the probability of winning
this game does not change with the strategy. Under the given strategy, the new game is equivalent to the
original problem. Since the new game is equivalent to allowing communication, the winning rate of the
given strategy = the winning rate of the new game ≥ the probability of winning the original game.
𝑎
𝑎
𝑎
𝑛
𝑎
𝑎
𝑎
𝑛
11 F. BlackBoard
 Problem Summary:
► There are n white points and m black points, and a certain number of white and black points need to be
selected so that the convex hull of the selected white points covers all the selected black points.
► Choosing a white point costs 114, and not choosing a black point costs 514. Minimize the total cost.

 Solution:
► If a black point is covered in a scheme, then it can definitely be covered by the triangle formed by any three
white points. Noting that 114 * 3 < 514, it follows that if a black point is covered in one scheme, then it
must be covered in the optimal scheme.
► Therefore, we can first find out which black points can be covered, and then the problem is transformed
into how to cover these black points with the fewest white points.
► Build a directed graph with n points, where there is an edge from i to j if and only if all (coverable) black
points are on the left side of the ray formed by white point i to white point j. The minimum cycle length of
this graph is the answer.
► The time complexity is O(n^2(n+m)).
12 G. Map
 Problem Summary:
► There are two integer matrices, one large and one small (value range [0,5]). The task is to count how many
submatrices in the large matrix are K-similar to the small matrix.
K-similarity is defined as: the matrices are of equal size, and for each positive integer in the small matrix,
there is a same number in the vicinity of the corresponding position in the large matrix, and the
Manhattan distance between these two positions does not exceed K.

 Solution:
► The value range is small, so consider judging for each number separately.
For each number, spread each occurrence position in the large matrix within a K distance. This turns into a
two-dimensional string matching problem with wildcards. Map it to one dimension, and it becomes an
ordinary string matching problem, which can be solved using Fast Fourier Transform.
► The complexity is O(VRClog(RC)), where V is the value range and R*C is the matrix size.
13 H. Bracket Completing
 Problem Summary:
► Given a set of bracket sequences, find the minimum number of brackets needed to make them valid when
concatenated in any order.
► Output the answer each time a bracket sequence is added.

 Solution:
► Consider the left and right brackets as +1 and -1 respectively. For a bracket sequence, if the minimum value
of the prefix is m and the total sum is s, then -m + (s-m) brackets are needed to complete it.
► When concatenating, we only need to maximize m, because s is fixed. This can be solved by sorting:
► 1. Put sequences with si>=0 before those with si<0.
► 2. For si>=0, sort in descending order by mi.
► 3. For si<0, sort in ascending order by mi-si.
► After offline sorting, it can be maintained with a segment tree.
14 I . Maze
 Problem Summary:
► There are n wires, which can be seen as n rays diverging from a starting point. There are m bridges, each
bridge connects wire a_i and wire a_{i%n+1} at a distance b_i from the starting point, with all b_i being
distinct. When you get to a bridge, you must cross it. The cost to remove a bridge is p, and the cost to build
a new one is q. For each i, find the minimum cost required to walk from the starting point along wire i, and
ultimately reach the infinite end of wire 1 following the above rules.

 Solution:
► Consider each edge from far to near, then the answers at positive infinity exactly form an arithmetic series
of answers like [0,q,2*q,…,q], and at any time, the difference between the two adjacent elements in the
answer array is at most q (because you can add edges).
► Maintain the answer array. For an edge (i,i+1), if it is not deleted, it is to swap the answers of the two
wires. If it is deleted, it is to add the deletion cost to the answers of the two wires. In addition, you can also
add edge (i,i-1) or edge (i+1,i+2) in front.
15 I . Maze
► After updating the answers of i,i+1, you also need to constantly add q to update the answers of the other
points. The update of the arithmetic series can be done using a segment tree or other methods, with a
complexity of O(mlogn).
► Note that the deletion of edges can be achieved by adding edges.
16 J. Splitstream Sort
 Problem Summary:
► Construct no more than (2n-1) nodes to sort a permutation of length n. Each node is one of the following
two types:
● Split an input sequence into two output sequences in the order of [1, 2, 1, 2, …].
● Merge two input sequences into one output sequence in the order of [1, 2, 1, 2, …].
► A sequence cannot be used repeatedly, that is, an output end can be connected to an input end at most
once. Output a legal solution, where n<=1000.

 Solution:
► Using (n-1) split nodes, the original sequence can be broken down into n sequences of length 1.
► Retrospectively, from the final sorted sequence [1, 2, …, n], it can be considered as the result of merging
two sequences [1, 3, 5, …, 2floor((n+1)/2)-1] and [2, 4, 6, …, 2floor(n/2)]. This process can be recursively
continued.
► In fact, there are many possible splits, for example, [1] and [2, …, n] is also correct.
17 K. Shopping

 Problem Summary:
► Given a tree, each node has a person and a shop. Each person starts walking towards the root from their
location, and after passing a shop, the person's money is taken modulo the price. Calculate how much
money each shop gets, and it's mandatory to calculate in the order from n to 1.

 Solution:
► If a >= b, then a mod b <= a / 2, each person will only pass log number of shops.
► Merge and solve from the bottom up. After merging the subtrees each time, find all the people whose
remaining money is greater than or equal to the price of the goods and handle them.
► You can maintain a structure that supports insertion, merging, and querying the minimum value, such as a
mergeable heap, or you can use STL + heuristic merging, both with a complexity of O(nlog^2n).
18 L. Lost

 Problem Summary:
► On a n*m map, there are k identical trees, but you don't know where you are. You plan to explore your
surroundings by walking in circles (see the problem description for specific walking methods) to determine
your location. Randomly given r coordinates, you know that your starting position must be one of these
coordinates. The question is how many steps are expected to uniquely determine the starting position in
the worst-case scenario (the starting position is always the worst).

 Solution:


Based on the formula ( )= ( ≥ ). , the expectation is transformed into the probability that

the position can be definitely determined within i steps.


► According to the terrain walked through in i steps, the coordinates are divided into several equivalent
classes, with sizes of a_1, a_2, …, a_p. If the position can be determined within i steps, the r coordinates
must come from r different equivalent classes. The number of solutions
[ ](1 + )(1 + )…(1 + ). , maintain this polynomial.
𝑖
1 2
𝑝
𝐸
𝑋
𝑃
𝑋
𝑖
𝑥
𝑎
𝑥
𝑎
𝑥
𝑎
𝑥
𝑟
19 L. Lost

► From step i to step i+1, it may be necessary to split some equivalent classes. If an equivalent class of size a
is split into two equivalent classes of sizes b and c, the polynomial needs to be divided by (1+ax), and then
multiplied by (1+bx)(1+cx).
► The complexity of maintaining the polynomial is O(nmr), because the equivalent class is split at most nm
times. The complexity of maintaining the equivalent class is O(nmk), because it may only need to modify
the equivalent class when encountering a tree. The total complexity is O(nmk+nmr).
Thank you!

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy