NOTE:
- l o g a b = l o g c b l o g c a log_ab=\frac{log_cb}{log_ca} logab=logcalogcb
- l g n = l o g 2 n lgn=log_2n lgn=log2n
p ( n ) = O ( f ( n ) ) p(n)=O(f(n)) p(n)=O(f(n))
p ( n ) ≤ c f ( n ) p(n)\le cf(n) p(n)≤cf(n).
p ( n ) = Ω ( f ( n ) ) p(n)=\Omega(f(n)) p(n)=Ω(f(n))
p ( n ) ≥ c f ( n ) p(n)\ge cf(n) p(n)≥cf(n)
p ( n ) = Θ ( f ( n ) ) p(n)=\Theta(f(n)) p(n)=Θ(f(n))
c 1 f ( n ) ≤ p ( n ) ≤ c 2 f ( n ) c_1f(n)\le p(n)\le c_2f(n) c1f(n)≤p(n)≤c2f(n)
p ( n ) = o ( f ( n ) ) p(n)=o(f(n)) p(n)=o(f(n))
p ( n ) < c f ( n ) p(n)< cf(n) p(n)<cf(n)
p ( n ) = ω ( f ( n ) ) p(n)=\omega(f(n)) p(n)=ω(f(n))
p ( n ) > c f ( n ) p(n)> cf(n) p(n)>cf(n)
Solution: Calculating l i m n → ∞ p ( n ) f ( n ) lim_{n\rightarrow\infty}\frac{p(n)}{f(n)} limn→∞f(n)p(n).
Given equation like T ( n ) = a T ( n b ) + f ( n ) T(n)=aT(\frac{n}{b})+f(n) T(n)=aT(bn)+f(n)
Big Integer Multiplication
X Y = a c 2 n + ( a b + c d ) 2 n / 2 + b d XY=ac2^n+(ab+cd)2^{n/2}+bd XY=ac2n+(ab+cd)2n/2+bd
Time complexity:
T ( n ) = 4 T ( n / 2 ) + O ( n ) T(n)=4T(n/2)+O(n) T(n)=4T(n/2)+O(n)
Divide and conquer:
X Y = a c 2 n + ( ( a + c ) ( b + d ) − a c − b d ) 2 n / 2 + b d XY=ac2^n+((a+c)(b+d)-ac-bd)2^{n/2}+bd XY=ac2n+((a+c)(b+d)−ac−bd)2n/2+bd
Time complexity:
T ( n ) = 3 T ( n / 2 ) + O ( n ) T(n)=3T(n/2)+O(n) T(n)=3T(n/2)+O(n)
FFT
Chess Board Cover
Put the “L” into center.
Binary Search
Insert a number into a sorted array. Need to move the element.
Time complexity:
T ( n ) = ∑ i = 1 i = n ( i − 1 ) = O ( n 2 ) T(n)=\sum_{i=1}^{i=n} (i-1) = O(n^2) T(n)=i=1∑i=n(i−1)=O(n2)
Divide the array into two parts, sort them separately and merge them
Time complexity:
T ( n ) = 2 T ( n 2 ) + O ( n ) T(n)=2T(\frac{n}{2})+O(n) T(n)=2T(2n)+O(n)
According to master method: a = 2 , b = 2 , f ( n ) = c n a=2,b=2,f(n)=cn a=2,b=2,f(n)=cn. Thus n l o g b a = n = Θ ( n ) n^{log_ba}=n=\Theta(n) nlogba=n=Θ(n). So that
T ( n ) = Θ ( n l g n ) T(n)=\Theta(nlgn) T(n)=Θ(nlgn)
希尔排序动图_哔哩哔哩_bilibili
Time complexity:
≈ O ( N 1.25 ) \approx O(N^{1.25}) ≈O(N1.25)
Θ ( n l g n ) \Theta(nlgn) Θ(nlgn)
Flow:
Time complexity: O ( n + k ) O(n + k) O(n+k), where n n n is the input number, k k k is equivalent to value of the maximal number.
NOTE:
- A m o d B = A − ( A ÷ B ) ∗ B A \mod B = A - (A \div B)*B AmodB=A−(A÷B)∗B
- ( A + B ) m o d N = ( A m o d N + B m o d N ) m o d N (A+B)\mod N=(A\mod N + B\mod N) \mod N (A+B)modN=(AmodN+BmodN)modN
Left subtree is less than root
Right subtree is large than root
Deletion:
Randomly built BST has O ( l g n ) O(lgn) O(lgn) search time complexity.
Treap is the BST with priority. This helps build a randomly BST.
Degree n n n (out degree): allows at least n − 1 n-1 n−1 keys, at most 2 n − 1 2n-1 2n−1 keys.
Split when insertion.
h ≤ l o g t ( ( n + 1 ) 2 ) h\le log_t(\frac{(n+1)}{2}) h≤logt(2(n+1)). See proof below.
m a x = m a x ( { n o d e . h i g h n o d e . l e f t . m a x n o d e . r i g h t . m a x ) max=max(\left\{ \begin{aligned} node.high \\ node.left.max \\ node.right.max \end{aligned} \right.) max=max(⎩⎪⎨⎪⎧node.highnode.left.maxnode.right.max)
i
in the Interval Tree:
root
is overlapped. If it is overlapped, then return root
.root.left.max
≥ i.left
, go to left. Recursively run process.Aggregated:
T = ∑ i = 1 n u n i t n T=\frac {\sum_{i=1}^n unit}{n} T=n∑i=1nunit
Accounting: Store c c c for each normal case operation, and each normal case operation spends m m m. Thus, you store c − m c-m c−m to the bank for each normal case operation. When the cost operation is coming, you spend all of your money in the bank. Make sure that the bank always has positive money.
T = ∑ i = 1 n c n = c T=\frac{\sum_{i=1}^n c}{n}=c T=n∑i=1nc=c
Potential: Design a potential function ϕ ( D i ) \phi(D_i) ϕ(Di), s.t., c i ′ = c i + ϕ ( D i ) − ϕ ( D i − 1 ) c'_i=c_i+\phi(D_i)-\phi(D_{i-1}) ci′=ci+ϕ(Di)−ϕ(Di−1). Make sure that ϕ ( D 0 ) = 0 \phi(D_0)=0 ϕ(D0)=0, and ϕ ( D i ) ≥ 0 \phi(D_i)\ge 0 ϕ(Di)≥0
Aggregated
c i = { i , i − 1 = 2 n 1 , o t h e r w i s e c_i=\left\{ \begin{aligned} i, i-1=2^n \\ 1, otherwise \\ \end{aligned} \right. ci={i,i−1=2n1,otherwise
∑ i = 1 n c i ≤ n + ∑ j = 1 l g ( n − 1 ) 2 j ≤ 3 n = Θ ( n ) \sum_{i=1}^nc_i\le n+\sum_{j=1}^{lg(n-1)} 2^j\le3n=\Theta(n) i=1∑nci≤n+j=1∑lg(n−1)2j≤3n=Θ(n)
Thus,
c i ′ = Θ ( n ) / n = Θ ( 1 ) c_i'=\Theta(n)/n= \Theta(1) ci′=Θ(n)/n=Θ(1)
Accounting
Charge c i ′ = 3 c_i'=3 ci′=3 for each operation, so that 1 1 1 for insertion, 2 2 2 is stored for extending. When extending the table, 1 1 1 for inserting old item. 1 1 1 for inserting new item. See the figure below
Potential
Define
ϕ ( D i ) = 2 i − 2 ⌈ l g i ⌉ \phi(D_i)=2i-2^{\lceil lgi\rceil } ϕ(Di)=2i−2⌈lgi⌉
So that
c i ′ = c i + ϕ ( D i ) − ϕ ( D i − 1 ) c_i'=c_i+\phi(D_i)-\phi(D_{i-1}) ci′=ci+ϕ(Di)−ϕ(Di−1)
Thus, we have c i ′ = 3 c_i'=3 ci′=3.
Aggregated
The summation of flip frequency of each bit.
n 2 + n 4 + n 8 + . . . + n 2 n = 2 n \frac{n}{2}+\frac{n}{4}+\frac{n}{8}+...+\frac{n}{2^n}=2n 2n+4n+8n+...+2nn=2n
So that c i ′ = 2 c_i'=2 ci′=2.
Accounting
Operation 0 → 1 0\rightarrow 1 0→1 store 2 2 2, and pay for 1 1 1. Operation 1 → 0 1\rightarrow 0 1→0 pay for 1 1 1 store 0. Thus, for n n n operations, we must store 2 n 2n 2n fee. Thus c i ′ = 2 c_i'=2 ci′=2.
Potential
Define
ϕ ( D i ) = n u m b e r o f 1 i n c o u n t e r \phi(D_i)= number\ of\ 1\ in \ counter ϕ(Di)=number of 1 in counter
Thus,
c i ′ = c i + ϕ ( D i ) − ϕ ( D i − 1 ) c_i'=c_i+\phi(D_i)-\phi(D_{i-1}) ci′=ci+ϕ(Di)−ϕ(Di−1)
Let operation i i i changes t i t_i ti 1 1 1 to 0 0 0. And there only one 0 0 0 is changed to 1 1 1 (But be careful to the situation that the counter is overflow). Thus
ϕ ( D i ) ≤ ϕ ( D i − 1 ) − t i + 1 \phi(D_i)\le\phi(D_{i-1})-t_i+1 ϕ(Di)≤ϕ(Di−1)−ti+1
So that
c i ′ ≤ t i + 1 + 1 − t i = 2 c_i'\le t_i+1 +1-t_i=2 ci′≤ti+1+1−ti=2
Matrix-chain Multiplication
The minimal multiplication times of A 1 ∗ A 2 ∗ . . . ∗ A n A_1*A_2*...*A_n A1∗A2∗...∗An
Let M i n [ i , j ] Min[i,j] Min[i,j] denote the minimal multiplication times of A i ∗ . . . ∗ A j A_i*...*A_j Ai∗...∗Aj
M i n [ i , j ] = { 0 i = j m i n i ≤ k ≤ j { M i n [ i , j ] , M i n [ i , k ] + M i n [ k + 1 , j ] + p i ∗ p k ∗ p j } i ≠ j Min[i,j]= \begin{cases} 0 & \text{ $ i=j $ } \\ min_{i\le k\le j}\{Min[i,j], Min[i,k]+Min[k+1,j]+p_i*p_k*p_j\} & \text{ $ i\ne j $ } \end{cases} Min[i,j]={0mini≤k≤j{Min[i,j],Min[i,k]+Min[k+1,j]+pi∗pk∗pj} i=j i=j
LCS problem
The longest common sub sequence of X = x 1 x 2 . . . x m X=x_1x_2...x_m X=x1x2...xm and Y = y 1 y 2 . . . y n Y=y_1y_2...y_n Y=y1y2...yn
Let L C S [ i , j ] LCS[i,j] LCS[i,j] denote the length of the longest common sequence of X i = x 1... x i X_i=x1...x_i Xi=x1...xi and Y j = y 1 . . . y j Y_j=y_1...y_j Yj=y1...yj
Thus,
L C S [ i , j ] = { 0 i = 0 o r j = 0 L C S [ i − 1 , j − 1 ] + 1 X i = Y j m a x { L C S [ i − 1 , j ] , L C S [ i , j − 1 ] } X i ≠ Y j LCS[i,j]= \begin{cases} 0 & \text{ $ i =0 \ or\ j=0 $} \\ LCS[i-1,j-1]+1 & \text{ $ X_i=Y_j $ } \\ max\{ LCS[i-1,j],LCS[i,j-1]\} & \text{ $ X_i\ne Y_j $ } \end{cases} LCS[i,j]=⎩⎪⎨⎪⎧0LCS[i−1,j−1]+1max{LCS[i−1,j],LCS[i,j−1]} i=0 or j=0 Xi=Yj Xi=Yj
Triangle Decomposition of Convex Polygon
Find the minimal summation of triangle decomposition.
Let M i n [ i , j ] Min[i,j] Min[i,j] denote the minimal summation triangle decomposition of vertex from i i i to j j j.
Thus,
M i n [ i , j ] = { 0 i = j m i n i < k < j { M i n [ i , k ] + M i n [ k , j ] + w ( v i v j v k ) } i ≠ j Min[i,j]= \begin{cases} 0 & \text{ $ i=j $ } \\ min_{i< k< j}\{Min[i,k]+Min[k,j]+w(v_i v_j v_k)\} & \text{ $ i\ne j $ } \end{cases} Min[i,j]={0mini<k<j{Min[i,k]+Min[k,j]+w(vivjvk)} i=j i=j
To prove a greedy algorithm is workable, you should:
To solve a FFT problem, for example:
Compute DFT of input ( x ( 0 ) , x ( 1 ) , x ( 2 ) , x ( 3 ) , x ( 4 ) , x ( 5 ) , x ( 6 ) , x ( 7 ) ) (x(0),x(1),x(2),x(3),x(4),x(5),x(6),x(7)) (x(0),x(1),x(2),x(3),x(4),x(5),x(6),x(7)) by using FFT
First, determine leaves. In this example, leaves are
( x ( 0 ) , x ( 4 ) ) (x(0),x(4)) (x(0),x(4)), ( x ( 2 ) , x ( 6 ) ) (x(2),x(6)) (x(2),x(6)), ( x ( 1 ) , x ( 5 ) ) (x(1),x(5)) (x(1),x(5)), ( x ( 3 ) , x ( 7 ) ) (x(3),x(7)) (x(3),x(7))
Second, draw a butterfly. i.e,
There are two things you should note:
- Recursively. W 8 0 = W 4 0 W_8^0=W_4^0 W80=W40, W 8 2 = W 4 1 W_8^2=W_4^1 W82=W41, and 1 = W 2 0 1=W_2^0 1=W20
- Positive for even number and Negative for odd number.
P: Solve and Verify in polynomial time.
NP: Verify in polynomial time.
NP-hard: All NP problems can be reduce to the problem.
NPC: ① It is a NP problem ② Any NP problem can be reduced to the problem.
The relations
Exponential solution
Approximate algorithm
Approximate ratio
Assume the best solution is c ∗ c^* c∗, and the approximate solution is c c c. The approximate ratio is:
m a x { c ∗ c , c c ∗ } ≤ ρ ( n ) max\{\frac{c^*}{c},\frac{c}{c^*}\} \le \rho(n) max{cc∗,c∗c}≤ρ(n)
ρ ( n ) \rho(n) ρ(n) is approximate ratio.
Relative error ϵ ( n ) \epsilon(n) ϵ(n)
∣ c − c ∗ c ∗ ∣ ≤ ϵ ( n ) |\frac{c-c^*}{c^*}|\le \epsilon(n) ∣c∗c−c∗∣≤ϵ(n)
选择 + 大题
共5题
总的来说不算难,但是第7题的证明题是真难想,希望有分~~