CF1540 Solution
A
If we sort d i d_i di in the increase order, there is a clearly construction:
- link 1 1 1 to n n n, weight is d n d_n dn.
- Expect the situation above,for i < j i < j i<j, link no edges between i i i and j j j; instead, link j j j to i i i, weight is d i − d j d_i-d_j di−dj.
- the ans is
d n + ∑ j = 1 n ∑ i = 1 j − 1 ( a i − a j ) = d n + ∑ i = 1 n ( n + 1 − 2 i ) a i d_n+\sum_{j=1}^n\sum_{i=1}^{j-1}(a_i-a_j)=d_n+\sum_{i=1}^n(n+1-2i)a_i dn+j=1∑ni=1∑j−1(ai−aj)=dn+i=1∑n(n+1−2i)ai
We can prove that this method is one of the best ones.
B
The problem statement can be transferred to : given a tree, you should choose a root and mark it. Afterwards, extend the subtree of the marked nodes.
At first, we need to choose a root r r r. And the answer becomes a n s = 1 n ∑ r = 1 n E ( r ) ans=\dfrac{1}{n}\sum_{r=1}^nE(r) ans=n1∑r=1nE(r), E ( r ) E(r) E(r) in it refers to the expected number of inversions according to the process if we fix the root of the tree, that is, r r r.
Now we need to calculate E ( r ) E(r) E(r). Let us consider each pair ( u , v ) , u > v (u,v), u>v (u,v),u>v's contribution, that is, the expected number that u u u marks before v v v.
we assume that the latest common ancestor of u u u and v v v is l l l. If we haven’t reached l l l, the answer is the same as the situation when we reached l l l. So we consider the latter. We have two stacks, the sizes of which are d i s t ( u , l ) dist(u, l) dist(u,l) and d i s t ( v , l ) dist(v, l) dist(v,l) respectively. Define f ( x , y ) f(x, y) f(x,y) as the expected number that the stack of size x x x decrease to 0 faster that size y y y. We suppose the possibility of choosing an unmarked node is p p p, so there are :
f ( x , y ) = p ⋅ f ( x − 1 , y ) + ⋅ f ( x , y − 1 ) + ( 1 − 2 p ) ⋅ f ( x , y ) f(x,y)=p\cdot f(x-1, y) + \cdot f(x, y-1) + (1-2p)\cdot f(x, y) f(x,y)=p⋅f(x−1,y)+⋅f(x,y−1)+(1−2p)⋅f(x,y)
So
f ( x , y ) = f ( x − 1 , y ) + f ( x , y − 1 ) 2 f(x,y)=\dfrac{f(x-1,y)+f(x, y-1)}{2} f(x,y)=2f(x−1,y)+f(x,y−1)
The ans is f ( d i s t ( u , l ) , d i s t ( v , l ) ) f(dist(u, l), dist(v, l)) f(dist(u,l),dist(v,l)).
The time complexity is O ( n 3 log n ) O(n^3\log n) O(n3logn) (use O ( log n ) O(\log n) O(logn) LCA) or O ( n 3 ) O(n^3) O(n3) (use O ( 1 ) O(1) O(1) LCA).
C
What we really do when we take an operation on a i a_i ai and a i + 1 a_{i+1} ai+1?
- if a i ≤ a i + a i + 1 − b i 2 a_i\le\dfrac{a_i+a_{i+1}-b_i}{2} ai≤2ai+ai+1−bi, there is a i + 1 ≥ a i + a i + 1 + b i 2 a_{i+1}\ge \dfrac{a_i+a_{i+1}+b_i}{2} ai+1≥2ai+ai+1+bi, and the contrary are correct, too. So after the operation, ( a i , a i + 1 ) (a_i,a_{i+1 }) (ai,ai+1) becomes ( a i , a i + 1 ) (a_i,a_{i+1}) (ai,ai+1) or ( a i + a i + 1 − b i 2 , a i + a i + 1 + b i 2 ) \left(\dfrac{a_i+a_{i+1}-b_i}{2}, \dfrac{a_i+a_{i+1}+b_i}{2}\right) (2ai+ai+1−bi,2ai+ai+1+bi).
We find that a i + a i + 1 a_i+a_{i+1} ai+ai+1 keeps constant, and we make ( a i + 1 − a i ) ← max ( a i + 1 − a i , b i ) (a_{i+1}-a_i)\gets\max(a_{i+1}-a_i, b_i) (ai+1−ai)←max(ai+1−ai,bi) in the operation i i i.
Let us consider the final seqence (suppose that is f i f_i fi). We know that f i + 1 − f i ≥ b i f_{i+1} - f_i\ge b_i fi+1−fi≥bi.
There is an important conclusion : if f i + 1 − f i > b i f_{i+1}-f_i > b_i fi+1−fi>bi, then operation i i i doesn’t matter throughout the process.
prove:
if a i + 1 − a i ≤ b i a_{i+1}-a_i\le b_i ai+1−ai≤bi stands through the process, we cannot make it up to f i + 1 − f i f_{i+1}-f_i fi+1−fi, for operation i − 1 i-1 i−1 only makes a i a_i ai increase and operation i + 1 i+1 i+1 only makes a i − 1 a_{i-1} ai−1 decrease.
For operation i i i doesn’t matter, [ 1 , i ] [1,i] [1,i] and [ i + 1 , n ] [i+1,n] [i+1,n] becomes independent parts, for their sums cannot exchange.
If k k k is the first number which satisfies the condition above, then we get:
∑ i = 1 k f i = ∑ i = 1 k a i \sum_{i=1}^kf_i=\sum_{i=1}^ka_i i=1∑kfi=i=1∑kai
also we know f i + 1 − f i = b i , ( 1 ≤ i < k ) f_{i+1}-f_i=b_i, (1\le i< k) fi+1−fi=bi,(1≤i<k)
so
f 1 + ( f 1 + b 1 ) + ( f 1 + b 1 + b 2 ) + ⋯ = ∑ i = 1 k a i f_1+(f_1+b_1)+(f_1+b_1+b_2)+\cdots=\sum_{i=1}^ka_i f1+(f1+b1)+(f1+b1+b2)+⋯=i=1∑kai
We define ∑ i = 1 n ∑ j = 1 i b j \sum_{i=1}^n\sum_{j=1}^ib_j ∑i=1n∑j=1ibj as B n B_n Bn, ∑ i = 1 n a i \sum_{i=1}^na_i ∑i=1nai as A n A_n An, then
f 1 = A k − B k − 1 k f_1=\dfrac{A_k-B_{k-1}}{k} f1=kAk−Bk−1
But we do not know which k k k satisfies f k + 1 − f k > b k f_{k+1}-f_k>b_k fk+1−fk>bk in the end. There is a conclsion : f 1 = min k = 1 n ( A k − B k − 1 k ) f_1=\min_{k=1}^n(\dfrac{A_k-B_{k-1}}{k}) f1=mink=1n(kAk−Bk−1), why ?
prove:
for all k ∈ [ 1 , n ] k\in[1,n] k∈[1,n], ∑ i = 1 k f i ≤ ∑ i = 1 k a i \sum_{i=1}^kf_i\le \sum_{i=1}^ka_i ∑i=1kfi≤∑i=1kai, because operation k k k only decrease the sum of [ 1 , k ] [1,k] [1,k] and increase the sum of [ k + 1 , n ] [k+1,n] [k+1,n]( k = n k=n k=n is a special situation). So k f 1 + B k − 1 ≤ ∑ i = 1 k f i ≤ A k kf_1+B_{k-1}\le \sum_{i=1}^kf_i\le A_k kf1+Bk−1≤∑i=1kfi≤Ak, f 1 ≤ A k − B k − 1 k f_1\le \dfrac{A_k-B_{k-1}}{k} f1≤kAk−Bk−1, and there exists k 0 k_0 k0 that satisfies f 1 = A k 0 − B k 0 − 1 k 0 f_1=\dfrac{A_{k_0}-B_{k_0-1}}{k_0} f1=k0Ak0−Bk0−1, Q.E.D
So the inequation f 1 ≥ x f_1\ge x f1≥x can be described as ∀ i ∈ [ 1 , n ] , A i − B i − 1 i ≥ x \forall i\in[1,n], \dfrac{A_i-B_{i-1}}{i}\ge x ∀i∈[1,n],iAi−Bi−1≥x, that is, A i ≥ B i − 1 + i ⋅ x A_i\ge B_{i-1}+i\cdot x Ai≥Bi−1+i⋅x. We can do a dynamic programming to count the qualified A i A_i Ais. Call f ( i , s ) f(i, s) f(i,s) the number of sequences which satisfy A i = s A_i=s Ai=s. And we need use prefix summation to optimize the transition. If set m = max c i m = \max c_i m=maxci, then the time complexity is O ( q m n 2 ) O(qmn^2) O(qmn2).
How to optimize the algorithm? There is an important conclusion : we need and only need to calculate O ( m ) O(m) O(m) kinds of x x x. Why?
If n m < B n − 1 + n x nm<B_{n-1}+nx nm<Bn−1+nx, the answer is 0; if 0 > B n − 1 + n x 0>B_{n-1}+nx 0>Bn−1+nx, for all the x x x which satisfies this condition the ans is the same.so we only require to solve the situation when B n − 1 + n x ∈ [ 0 , n m ] B_{n-1}+nx\in [0, nm] Bn−1+nx∈[0,nm], that is, x ∈ [ ⌊ − B n − 1 / n ⌋ , ⌈ m − B n − 1 / n ⌉ ] x\in[\lfloor-B_{n-1}/n\rfloor, \lceil m- B_{n-1}/n\rceil] x∈[⌊−Bn−1/n⌋,⌈m−Bn−1/n⌉]. The time complexity is O ( m 2 n 2 + q ) O(m^2n^2+q) O(m2n2+q).
D
To be continued…
E
To be continued…