0% found this document useful (0 votes)
9 views34 pages

2020icpc Macau Analyze

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)
9 views34 pages

2020icpc Macau Analyze

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/ 34

The 2020 ICPC Asia Macau Regional

Contest Editorial

Prepared by Zhejiang University

May 29, 2021

Prepared by Zhejiang University The 2020 ICPC Asia Macau Regional Contest Editorial
A. Accelerator

Task
We are given n accelerators a1 , a2 , . . . , an . The final velocity is

((. . . ((a1 + 1) a2 + 1) · · · + 1) an−1 + 1) an

We are asked to calculate the expected final velocity when the


order sequence a1 , a2 , . . . , an is randomly shuffled.

Prepared by Zhejiang University The 2020 ICPC Asia Macau Regional Contest Editorial
A. Accelerator

Observation
((. . . ((a1 + 1) a2 + 1) · · · + 1) an−1 + 1) an
= a1 a2 a3 . . . an + a2 a3 . . . an + · · · + an−1 an + an

Assume we pick k unordered items b1 , b2 , . . . , bk from the


sequence a.
They can form k! suffixes of the shuffled sequence, the left
n − k items can form (n − k)! prefixes.
So the contribution( to the) answer is
(b1 b2 . . . bk ) × k!×(n−k)!
n! .

n
The sum of the left part (b1 b2 . . . bk ) is [xk ] (1 + ai x),
i=1
which can be computed using divide & conquer and FFT in
O(n log2 n).
Prepared by Zhejiang University The 2020 ICPC Asia Macau Regional Contest Editorial
B. Boring Problem

Task
We are given n strings T1 , T2 , . . . , Tn of length m and a string R.
For each i (1 ≤ i ≤ |R|), we are required to compute the expected
length of the final string when it contains any string in T as
substring if we repeat appending the i-th (1 ≤ i ≤ k) lowercase
letter with with probability pi to the end of the current string, here
the initial string is R[1..i].

Prepared by Zhejiang University The 2020 ICPC Asia Macau Regional Contest Editorial
B. Boring Problem

W.L.O.G, we can assume T1 , T2 , . . . , Tn are pairwise distinct.


Insert T1 , T2 , . . . , Tn into Trie T′ , build the Aho-Corasick
Automaton G on T′ , there will be O(nm) vertices.
Let’s denote g(i, j) as the vertex we will go to when we receive
letter j at vertex i in G.
Let’s denote E(x) as the expected number of random steps to
reach any leaf from the x-th vertex in G.

If vertex x is a leaf in T
∑, E(x) = 0.
Otherwise E(x) = 1 + 1≤j≤k E(g(x, j))pj .
If we pre-compute all the values of E(·), we can find the
answer in O(|R|) time by walking on G.
There is a straightforward approach to compute E(·) using
Gauss Elimination in O(n3 m3 ), which is unfortunately too
slow to pass.

Prepared by Zhejiang University The 2020 ICPC Asia Macau Regional Contest Editorial
B. Boring Problem

Consider all the vertices according to their depth in T′ , from


root to the leaves.
Let’s look at a vertex x in T′ , which is not a leaf.
If dep(g(x, j)) > dep(x), g(x, j) must be a child of x in T′ .
Otherwise dep(g(x, j)) ≤ dep(x), so g(x, j) will be considered
before x.

Idea
Assume x has t (t ≥ 1) children g(x, c1 ), g(x, c2 ), . . . , g(x, ct ) in T′ ,
we can know E(g(x, c1 )) if we know E(x) and all other values of
E(g(x, j)) according to

E(x) − 1 − j,j̸=c1 E(g(x, j))pj
E(g(x, c1 )) =
pc1

Prepared by Zhejiang University The 2020 ICPC Asia Macau Regional Contest Editorial
B. Boring Problem

Observation
If we treat E(g(x, c2 )), E(g(x, c3 )), . . . , E(g(x, ct )) as unknown
variables, we can know E(g(x, c1 )) because
dep(g(x, j)) ≤ dep(x) < dep(g(x, c1 )), all other vertices are
considered before.

The value of the root E(root) should also be treated as


unknown.
Equations on leaves (i.e. E(x) = 0) are unused.
Hence we will get n unknowns and n equations, run Gauss
Elimination in O(n3 ), then we can get all the values of E(·).
Overall time complexity is O(n3 + n2 mk + |R|).

Prepared by Zhejiang University The 2020 ICPC Asia Macau Regional Contest Editorial
C. Club Assignment

Task
We are given n positive integers w1 , w2 , . . . , wn , divide them into
two groups such that

min {wi ⊕ wj }
1≤i<j≤n, i and j are in the same group

is maximized.

Prepared by Zhejiang University The 2020 ICPC Asia Macau Regional Contest Editorial
C. Club Assignment

Build a graph G with n vertices 1, 2, . . . , n, the weight of the


edge between i and j is wi ⊕ wj .
Initially the graph contains no edges. Add all edges in
non-decreasing order by weight.
Assume here comes an edge (u, v) with weight wu ⊕ wv :
If u and v are not connected: Link them with this edge, they
should be in different groups.
If u and v are connected, and they should be in different
groups: Ignore this edge.
If u and v are connected, and they should be in the same
groups: Conflict arises, the current assignment is the answer.

Prepared by Zhejiang University The 2020 ICPC Asia Macau Regional Contest Editorial
C. Club Assignment

Lemma
Find the minimum spanning tree of G, assign adjacent vertices into
different groups is optimal.

The algorithm to find the XOR minimum spanning tree is a


well-known recursive approach.
Divide all numbers into two groups according to their highest
digit, solve recursively for these groups, then add the minimal
possible edge between the groups.
Finding the minimal possible edge can be done using 01-Trie.
Time complexity: O(n log2 w).

Prepared by Zhejiang University The 2020 ICPC Asia Macau Regional Contest Editorial
D. Artifacts

Task
We are given 5 artifacts, and we are asked to calculate the
expected damage.

Prepared by Zhejiang University The 2020 ICPC Asia Macau Regional Contest Editorial
D. Artifacts

Read the whole statement.


Parse the input carefully.
Calculate the expected damage.

Prepared by Zhejiang University The 2020 ICPC Asia Macau Regional Contest Editorial
E. Mountain

Task
There is a mountain

(0, 0) − (1, h1 ) − (2, h2 ) − · · · − (n, hn ) − (n + 1, 0)

At each point (i, hi ), a picture is taken, which covers all the points
(x, y) where i − W ≤ x ≤ i + W and hi − H ≤ y ≤ hi + H.
For k = 1, 2, . . . , n, keep exactly k pictures, maximize the total
area of the mountain which is covered by at least one picture.

Prepared by Zhejiang University The 2020 ICPC Asia Macau Regional Contest Editorial
E. Mountain

Observation
The picture taken at (i, hi ) can only coincide with those pictures
taken at (j, hj ) where |i − j| ≤ 2W − 1.

Let’s denote dp[i][j][S] as the maximum total covered area


such that we choose to keep j pictures in the first i points,
where S is a (2W − 1)-bit binary mask denoting the kept
pictures in the previous 2W − 1 points.
The number of states: O(n2 22W−1 ).
The transition can be done in O(1) if we pre-compute the
expanded covered area.

Prepared by Zhejiang University The 2020 ICPC Asia Macau Regional Contest Editorial
F. Fixing Networks

Task
Construct a simple undirected graph with n vertices and c
components, where the degree of each vertex is d.

Prepared by Zhejiang University The 2020 ICPC Asia Macau Regional Contest Editorial
F. Fixing Networks

d = 0 or d = 1 are trivial.
The cases for no solution:
c(d + 1) > n: Number of vertices is not enough.
Both n and d are odd: The sum of degrees is not even.
When there is a solution, we can construct c − 1 components
by assign each as a complete graph with d + 1 vertices.

Prepared by Zhejiang University The 2020 ICPC Asia Macau Regional Contest Editorial
F. Fixing Networks

Now we only need to handle the case c = 1.


Let k = ⌊ d2 ⌋, construct a cycle, link each vertex to the
previous k vertices and the next k vertices.
When d is odd, link each vertex to the extra vertex opposite
to it on the cycle, because n is always even in this case.

Prepared by Zhejiang University The 2020 ICPC Asia Macau Regional Contest Editorial
G. Game on Sequence

Task
There is a sequence A1 , A2 , . . . , An with non-negative integers.
Initially a token is at position k. Two players take turns moving the
token from i to a position j on the right side such that Aj differs
from Ai on at most one bit in binary representation.
We are required to perform two types of operations:
Append an integer at the end of the sequence A.
Predict the winner when the token is at position k.

Prepared by Zhejiang University The 2020 ICPC Asia Macau Regional Contest Editorial
G. Game on Sequence

For each position k, determine the value of fk : Can the


current player win when the token is at k?
fk = OR(NOT fj ), where j > k and Aj differs from Ak on at
most one bit in binary representation.
Assume there are two positions i and j, where i < j and
Ai = Aj :
If fj = False: By the definition of fi , we can know fi = True.
If fj = True: There exists a position k such that k > j and
fk = False, so k > i and fi = True.

Prepared by Zhejiang University The 2020 ICPC Asia Macau Regional Contest Editorial
G. Game on Sequence

Lemma
For two positions i and j, where i < j and Ai = Aj , fi = True
always holds.

Assume the token is at k:


If k is not the rightmost position matches Ak : We can claim
fk = True.
Otherwise keep only the rightmost position for each value and
run brute-force dynamic programming to find the answer.
Time complexity: O(mA log A).

Prepared by Zhejiang University The 2020 ICPC Asia Macau Regional Contest Editorial
H. Fly Me To The Moon

Task
There is an infinity grid and n types of spacecrafts d1 , d2 , . . . , dn .
Assume we are at (x, y), we can choose a type of spacecraft di and
fly to (x + dx, y + dy) (dx, dy ≥ 0) where 0 < dx2 + dy2 ≤ d2i .
There are m broken points we can’t touch. We are required to
count the number of ways to reach (1000, 1000) from (0, 0).

Prepared by Zhejiang University The 2020 ICPC Asia Macau Regional Contest Editorial
H. Fly Me To The Moon

Assume the number of ways to reach (x, y) from (0, 0) is


ways(x, y) when there are no broken points.
Add (1000, 1000) into the set of broken points for
convenience.
Let’s denote fi as the number of ways to reach the i-th broken
point from (0, 0) such that we never visit any other broken
point.

fi = ways(xi , yi ) − j,j̸=i fj × ways(xi − xj , yi − yj ), where
xj ≤ xi and yj ≤ yi .
If we pre-compute ways(x, y) for all pairs of (x, y)
(0 ≤ x, y ≤ 1000), we can find the answer in O(m2 ) time.

Prepared by Zhejiang University The 2020 ICPC Asia Macau Regional Contest Editorial
H. Fly Me To The Moon

Now we are going to pre-compute ways(x, y).


Ignore all broken points, assume the number of ways to reach
(x, y) from (0, 0) in a single step is one(x, y).
∑ ∑
Let’s denote G(x, y) = i≥0 j≥0 one(i, j)xi yj .
∑ 1
ways(i, j) = [xi yj ] G(x, y)k = [xi yj ]
1 − G(x, y)
k≥0
1
Here 1−G(x,y) mod x1001 mod y1001 can be computed using
FFT in O(10002 log 1000).

Prepared by Zhejiang University The 2020 ICPC Asia Macau Regional Contest Editorial
I. Nim Cheater

Task
There is a sequence of piles of stones, Bob can pay to remove
some piles from the game.
Initially the sequence is empty. We are required to perform three
types of operations:
Append a new pile at the end of the sequence.
Delete the rightmost pile.
Find the cheapest way for Bob to cheat such that the first
player of the Nim game will lose.
The memory limit is 8MB.

Prepared by Zhejiang University The 2020 ICPC Asia Macau Regional Contest Editorial
I. Nim Cheater
The first player will lose iff the XOR sum of all numbers is
zero.
We need to find a subset of numbers such that the XOR sum
is zero and the total cost is maximized, and remove the left
part to cheat.

Observation
The modifications form a rooted tree.

Construct the rooted tree, we need to find the answer for each
vertex.
Let’s denote dp[i][j] as the maximum total cost such that the
XOR of chosen numbers is j if we choose numbers among the
path from the root to the i-th vertex.
Time & space complexity: O(na), which is unfortunately
unable to fit in the 8MB limit.
Prepared by Zhejiang University The 2020 ICPC Asia Macau Regional Contest Editorial
I. Nim Cheater

Idea
Try different DFS orders won’t affect the answer.

Find the heavy light decomposition of the tree.


For each vertex, DFS its light children first, and finally DFS
its heavy child, give its space to its heavy child.
Now dp[i][j] can be stored in f[cnt][j], cnt denoting the number
of light edges on the path from the root to the i-th vertex.

Lemma
There will be at most O(log n) light edges on a path, so the space
complexity is O(a log n).

Prepared by Zhejiang University The 2020 ICPC Asia Macau Regional Contest Editorial
J. Jewel Grab

Task
Given a sequence of jewels, each jewel has its color and its value.
We are required to perform two types of operations:
Modify a jewel in the sequence.
Find the maximum total value of taken jewels if we start at
position s and move right, skipping at most k jewels, such
that no two taken jewels share the same color.

Prepared by Zhejiang University The 2020 ICPC Asia Macau Regional Contest Editorial
J. Jewel Grab

For the i-th jewel, find the first jewel prei with the same color
on the left side of it.
For each query, let’s move right from s step by step, assume
we are at j with color cj and value vj :
If prej < s, it is the first time we meet a jewel in such color,
take it.
Otherwise prej ≥ s, we should compare it with the previous
one, greedy keep the one with larger value, and skip one of
them.
The second part takes place at most k times, which is
acceptable for k ≤ 10.
To speed up the first part, we need to find the next position j
such that prej ≥ s, and find the sum in this range, which can
be done by traveling on the segment tree in O(log n).
Time complexity: O(k log n) per query.

Prepared by Zhejiang University The 2020 ICPC Asia Macau Regional Contest Editorial
K. Candy Ads

Task
Given n advertisement posters, we are required to choose some of
them such that no two chosen advertisement posters will occupy
the same pixel at the same time.
We are also given m extra conditions, each of which requires us to
keep at least one of two listed advertisement posters.

Prepared by Zhejiang University The 2020 ICPC Asia Macau Regional Contest Editorial
K. Candy Ads

For the i-th ad poster, introduce a boolean variable xi , which


is true iff we choose the i-th ad poster.
For two pairs of ad posters (i, j), if they occupy the same pixel
at the same time, we have xi ∧ xj = False.
For an extra condition (u, v), we have xu ∨ xv = True.
We can build a graph with 2n vertices and O(n2 + m) edges
to solve the above 2-SAT problem, which is unfortunately too
slow to pass.

Prepared by Zhejiang University The 2020 ICPC Asia Macau Regional Contest Editorial
K. Candy Ads

To solve the 2-SAT problem, we need to find the strongly


connected components in the corresponding graph.
We can find SCC using the Kosaraju Algorithm by running
two similar DFS procedures on the graph.
When we are visiting a vertex in DFS, we need to know the
adjacent vertices, and we should skip visited vertices in a
clever way.

Idea
Use Bitset to find the adjacent vertices and skip visited vertices.

2
Time complexity: O( nw + m).

Prepared by Zhejiang University The 2020 ICPC Asia Macau Regional Contest Editorial
L. Random Permutation

Task
An integer sequence a1 , a2 , · · · , an is generated randomly, and the
probability of being 1, 2, · · · , n are all n1 for each ai .
We are required to calculate the expected number of permutations
p1 , p2 , · · · , pn from 1 to n such that pi ≤ ai holds for each
i = 1, 2, · · · , n.

Prepared by Zhejiang University The 2020 ICPC Asia Macau Regional Contest Editorial
L. Random Permutation

Observation
No matter what the permutation p is, the number of the
corresponding sequence a is always n!.

There are n! permutations, and nn possible sequences a.


n!n!
So the answer is nn .

Prepared by Zhejiang University The 2020 ICPC Asia Macau Regional Contest Editorial
Thank you!

Prepared by Zhejiang University The 2020 ICPC Asia Macau Regional Contest Editorial

You might also like