CF1540 Solution

这篇博客探讨了图论中的树结构问题,包括如何构建具有特定性质的边权重和如何选择树的根节点以优化期望的逆序数。此外,还详细分析了序列操作的策略,特别是当操作不影响最终结果的情况下如何优化算法。最后,提出了动态规划方法来解决特定条件下的不等式问题,并讨论了优化算法的时间复杂度和关键步骤。
摘要由CSDN通过智能技术生成

CF1540 Solution

A

If we sort d i d_i di in the increase order, there is a clearly construction:

  1. link 1 1 1 to n n n, weight is d n d_n dn.
  2. 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 didj.
  3. 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=1ni=1j1(aiaj)=dn+i=1n(n+12i)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=n1r=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)=pf(x1,y)+f(x,y1)+(12p)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(x1,y)+f(x,y1)

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} ai2ai+ai+1bi, 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+12ai+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+1bi,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+1ai)max(ai+1ai,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+1fibi.

There is an important conclusion : if f i + 1 − f i > b i f_{i+1}-f_i > b_i fi+1fi>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+1aibi stands through the process, we cannot make it up to f i + 1 − f i f_{i+1}-f_i fi+1fi, for operation i − 1 i-1 i1 only makes a i a_i ai increase and operation i + 1 i+1 i+1 only makes a i − 1 a_{i-1} ai1 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=1kfi=i=1kai

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+1fi=bi,(1i<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=1kai

We define ∑ i = 1 n ∑ j = 1 i b j \sum_{i=1}^n\sum_{j=1}^ib_j i=1nj=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=kAkBk1

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+1fk>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(kAkBk1), 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=1kfii=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+Bk1i=1kfiAk, f 1 ≤ A k − B k − 1 k f_1\le \dfrac{A_k-B_{k-1}}{k} f1kAkBk1, 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=k0Ak0Bk01, Q.E.D

So the inequation f 1 ≥ x f_1\ge x f1x 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],iAiBi1x, that is, A i ≥ B i − 1 + i ⋅ x A_i\ge B_{i-1}+i\cdot x AiBi1+ix. 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<Bn1+nx, the answer is 0; if 0 > B n − 1 + n x 0>B_{n-1}+nx 0>Bn1+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] Bn1+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[Bn1/n,mBn1/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…

matlab程序:%cf对应的af不唯一,取af大于零的时候 ar=0:0.5:10; syms x assume(x>0) %根据魔术公式求导得到ar-cr的关系,求的cr,cf a0=1.5999;a1=-0.0048;a2=0.9328;a3=4.0847;a4=44.8338; a6=-0.0076;a7=-0.1807;a8=-0.0026;a9=0.0367; a11=0.0004;a12=-0.0115;a17=0.0009; F_zr=m*9.8*lf/(lf+lr)/1000; C=a0*(5-a)/4; D2=(a1*(F_zr^2)+a2*F_zr)*a; B2=(a3*sin(2*atan(F_zr/a4))/(C*D2))*(2-a); Sh2=a8*F_zr+a9; E2=(a6*F_zr+a7); cr=(1000*C*D2*cos(C*atan(E2*(atan(B2*ar) - B2*ar) + B2*ar)).*(B2 - E2*(B2 - B2./(B2^2*ar.^2 + 1))))./((E2*(atan(B2*ar) - B2*ar) + B2*ar).^2 + 1); cf=(m*V^2*lr*cr)./(cr*(lf+lr)*(lf+lr)-m*V^2*lf); % 已知参数 F_zf=m*9.8*(lr)/(lr+lf)/1000; D1=(a1*(F_zf^2)+a2*F_zf)*a; B1=(a3*sin(2*atan(F_zf/a4))/(C*D1))*(2-a); E1=a6*F_zf+a7; % 定义af-cf函数 f=@(x)(1000*C*D1*cos(C*atan(E1*(atan(B1*x) - B1*x) + B1*x)).*(B1 - E1*(B1 - B1./(B1^2*x.^2 + 1))))./((E1*(atan(B1*x) - B1*x) + B1*x).^2 + 1) - cf; % 反求af x0=[0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0]+1; af=fsolve(f,x0); %转化为弧度制 af1=af*pi/180;ar1=ar*pi/180; %求得侧偏角和横摆角速度 r=(V*(cetia-af1+ar1))/(lf+lr); betia=(lf*(cetia-af1)-lf*ar1)/(lf+lr); figure(5); plot(betia,r); axis([-40,40,-40,40]); title('betia-r'); xlabel('betia');ylabel('r'); hold on;报错警告: Trust-region-dogleg algorithm of FSOLVE cannot handle non-square systems; using Levenberg-Marquardt algorithm instead. > 位置:fsolve (第 342 行) 位置: untitled2 (第 36 行) No solution found. fsolve stopped because the last step was ineffective. However, the vector of function values is not near zero, as measured by the value of the function tolerance. <stopping criteria details> >> 请修改
06-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

日居月诸Rijuyuezhu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值