Step 1 / 19 2 0 3 1 5 2 1 3 4 4 6 5 Children per house dp[j][i] 0 cost(l,r)
Bài Houses and Schools: đặt k = 2 k=2 k = 2 trường học vào n = 6 n=6 n = 6 ngôi nhà sao cho tổng quãng đường đi bộ nhỏ nhất. Số trẻ em: [ 2 , 3 , 5 , 1 , 4 , 6 ] [2, 3, 5, 1, 4, 6] [ 2 , 3 , 5 , 1 , 4 , 6 ] . Ta xây dựng d p [ j ] [ i ] dp[j][i] d p [ j ] [ i ] = chi phí nhỏ nhất khi dùng j j j trường cho các nhà 1.. i 1..i 1.. i .
Step 2 / 19 2 0 3 1 5 2 1 3 4 4 6 5 Children per house 0 dp[j][i] 0 cost(l,r)
Trường hợp cơ sở: d p [ 0 ] [ 0 ] = 0 dp[0][0] = 0 d p [ 0 ] [ 0 ] = 0 . Không trường nào phục vụ không nhà nào thì chi phí bằng 0 0 0 . Tất cả d p [ 0 ] [ i ] dp[0][i] d p [ 0 ] [ i ] khác đều là vô cực (không thể phục vụ nhà nào mà không có trường).
Step 3 / 19 2 0 3 1 5 2 1 3 4 4 6 5 Children per house 0 0 Arrow from dp.cell[0][0] to dp.cell[1][1]: cost(1,1)=0 cost(1,1)=0 dp[j][i] 0 cost(l,r) d p [ 1 ] [ 1 ] = c o s t ( 1 , 1 ) = 0 dp[1][1] = cost(1,1) = 0 d p [ 1 ] [ 1 ] = cos t ( 1 , 1 ) = 0 . Một trường tại nhà 1 1 1 chỉ phục vụ nhà 1 1 1 . Không cần đi bộ.
Step 4 / 19 2 0 3 1 5 2 1 3 4 4 6 5 Children per house 0 0 2 Arrow from dp.cell[0][0] to dp.cell[1][1]: cost(1,1)=0 cost(1,1)=0 Arrow from dp.cell[0][0] to dp.cell[1][2]: cost(1,2)=2 cost(1,2)=2 dp[j][i] 2 cost(l,r) d p [ 1 ] [ 2 ] = c o s t ( 1 , 2 ) = 2 dp[1][2] = cost(1,2) = 2 d p [ 1 ] [ 2 ] = cos t ( 1 , 2 ) = 2 . Một trường cho nhà 1 1 1 -2 2 2 : trung vị có trọng số tại nhà 2 2 2 (trọng số 3 ≥ 3 \geq 3 ≥ nửa của 5 5 5 ). Chi phí = 2 × 1 + 3 × 0 = 2 = 2 \times 1 + 3 \times 0 = 2 = 2 × 1 + 3 × 0 = 2 .
Step 5 / 19 2 0 3 1 5 2 1 3 4 4 6 5 Children per house 0 0 2 7 Arrow from dp.cell[0][0] to dp.cell[1][1]: cost(1,1)=0 cost(1,1)=0 Arrow from dp.cell[0][0] to dp.cell[1][2]: cost(1,2)=2 cost(1,2)=2 Arrow from dp.cell[0][0] to dp.cell[1][3]: cost(1,3)=7 cost(1,3)=7 dp[j][i] 7 cost(l,r) d p [ 1 ] [ 3 ] = c o s t ( 1 , 3 ) = 7 dp[1][3] = cost(1,3) = 7 d p [ 1 ] [ 3 ] = cos t ( 1 , 3 ) = 7 . Trung vị có trọng số tại nhà 3 3 3 . Chi phí = 2 × 2 + 3 × 1 + 5 × 0 = 7 = 2 \times 2 + 3 \times 1 + 5 \times 0 = 7 = 2 × 2 + 3 × 1 + 5 × 0 = 7 .
Step 6 / 19 2 0 3 1 5 2 1 3 4 4 6 5 Children per house 0 0 2 7 8 Arrow from dp.cell[0][0] to dp.cell[1][1]: cost(1,1)=0 cost(1,1)=0 Arrow from dp.cell[0][0] to dp.cell[1][2]: cost(1,2)=2 cost(1,2)=2 Arrow from dp.cell[0][0] to dp.cell[1][3]: cost(1,3)=7 cost(1,3)=7 dp[j][i] 8 cost(l,r) d p [ 1 ] [ 4 ] = c o s t ( 1 , 4 ) = 8 dp[1][4] = cost(1,4) = 8 d p [ 1 ] [ 4 ] = cos t ( 1 , 4 ) = 8 . Trung vị vẫn tại nhà 3 3 3 (trọng số tích lũy 10 ≥ 5.5 10 \geq 5.5 10 ≥ 5.5 ). Chi phí = 2 × 2 + 3 × 1 + 5 × 0 + 1 × 1 = 8 = 2 \times 2 + 3 \times 1 + 5 \times 0 + 1 \times 1 = 8 = 2 × 2 + 3 × 1 + 5 × 0 + 1 × 1 = 8 .
Step 7 / 19 2 0 3 1 5 2 1 3 4 4 6 5 Children per house 0 0 2 7 8 16 Arrow from dp.cell[0][0] to dp.cell[1][1]: cost(1,1)=0 cost(1,1)=0 Arrow from dp.cell[0][0] to dp.cell[1][2]: cost(1,2)=2 cost(1,2)=2 Arrow from dp.cell[0][0] to dp.cell[1][3]: cost(1,3)=7 cost(1,3)=7 dp[j][i] 16 cost(l,r) d p [ 1 ] [ 5 ] = c o s t ( 1 , 5 ) = 16 dp[1][5] = cost(1,5) = 16 d p [ 1 ] [ 5 ] = cos t ( 1 , 5 ) = 16 . Trung vị tại nhà 3 3 3 . Chi phí = 2 × 2 + 3 × 1 + 5 × 0 + 1 × 1 + 4 × 2 = 16 = 2 \times 2 + 3 \times 1 + 5 \times 0 + 1 \times 1 + 4 \times 2 = 16 = 2 × 2 + 3 × 1 + 5 × 0 + 1 × 1 + 4 × 2 = 16 . Thêm nhà 5 5 5 ở xa làm chi phí tăng đáng kể.
Step 8 / 19 2 0 3 1 5 2 1 3 4 4 6 5 Children per house 0 0 2 7 8 16 33 Arrow from dp.cell[0][0] to dp.cell[1][1]: cost(1,1)=0 cost(1,1)=0 Arrow from dp.cell[0][0] to dp.cell[1][2]: cost(1,2)=2 cost(1,2)=2 Arrow from dp.cell[0][0] to dp.cell[1][3]: cost(1,3)=7 cost(1,3)=7 dp[j][i] 33 cost(l,r) d p [ 1 ] [ 6 ] = c o s t ( 1 , 6 ) = 33 dp[1][6] = cost(1,6) = 33 d p [ 1 ] [ 6 ] = cos t ( 1 , 6 ) = 33 . Trung vị dịch sang nhà 4 4 4 . Chi phí = 2 × 3 + 3 × 2 + 5 × 1 + 1 × 0 + 4 × 1 + 6 × 2 = 33 = 2 \times 3 + 3 \times 2 + 5 \times 1 + 1 \times 0 + 4 \times 1 + 6 \times 2 = 33 = 2 × 3 + 3 × 2 + 5 × 1 + 1 × 0 + 4 × 1 + 6 × 2 = 33 . Một trường cho tất cả các nhà rất tốn kém!
Step 9 / 19 2 0 3 1 5 2 1 3 4 4 6 5 Children per house 0 0 2 7 8 16 33 Arrow from dp.cell[0][0] to dp.cell[1][1]: cost(1,1)=0 cost(1,1)=0 Arrow from dp.cell[0][0] to dp.cell[1][2]: cost(1,2)=2 cost(1,2)=2 Arrow from dp.cell[0][0] to dp.cell[1][3]: cost(1,3)=7 cost(1,3)=7 dp[j][i] 33 cost(l,r) Hàng j = 1 j=1 j = 1 hoàn thành. Với một trường, chi phí tăng nhanh khi đoạn mở rộng. Bây giờ ta tính hàng j = 2 j=2 j = 2 : đặt hai trường. Công thức truy hồi là d p [ 2 ] [ i ] = min m ( d p [ 1 ] [ m ] + c o s t ( m + 1 , i ) ) dp[2][i] = \min_m (dp[1][m] + cost(m+1, i)) d p [ 2 ] [ i ] = min m ( d p [ 1 ] [ m ] + cos t ( m + 1 , i )) .
Step 10 / 19 2 0 3 1 5 2 1 3 4 4 6 5 Children per house 0 0 2 7 8 16 33 0 Arrow from dp.cell[0][0] to dp.cell[1][1]: cost(1,1)=0 cost(1,1)=0 Arrow from dp.cell[0][0] to dp.cell[1][2]: cost(1,2)=2 cost(1,2)=2 Arrow from dp.cell[0][0] to dp.cell[1][3]: cost(1,3)=7 cost(1,3)=7 Arrow from dp.cell[1][1] to dp.cell[2][2]: m=1: 0+0 m=1: 0+0 dp[j][i] 0 cost(l,r) d p [ 2 ] [ 2 ] = d p [ 1 ] [ 1 ] + c o s t ( 2 , 2 ) = 0 + 0 = 0 dp[2][2] = dp[1][1] + cost(2,2) = 0 + 0 = 0 d p [ 2 ] [ 2 ] = d p [ 1 ] [ 1 ] + cos t ( 2 , 2 ) = 0 + 0 = 0 . Chia tại m = 1 m=1 m = 1 : trường thứ nhất phục vụ nhà 1 1 1 , trường thứ hai phục vụ nhà 2 2 2 . Cả hai đều không cần đi bộ.
Step 11 / 19 2 0 3 1 5 2 1 3 4 4 6 5 Children per house 0 0 2 7 8 16 33 0 Arrow from dp.cell[0][0] to dp.cell[1][1]: cost(1,1)=0 cost(1,1)=0 Arrow from dp.cell[0][0] to dp.cell[1][2]: cost(1,2)=2 cost(1,2)=2 Arrow from dp.cell[0][0] to dp.cell[1][3]: cost(1,3)=7 cost(1,3)=7 Arrow from dp.cell[1][1] to dp.cell[2][2]: m=1: 0+0 m=1: 0+0 Arrow from dp.cell[1][1] to dp.cell[2][3]: m=1: 0+3 m=1: 0+3 Arrow from dp.cell[1][2] to dp.cell[2][3]: m=2: 2+0 m=2: 2+0 dp[j][i] 3 cost(l,r) d p [ 2 ] [ 3 ] dp[2][3] d p [ 2 ] [ 3 ] : thử m = 1 m=1 m = 1 : d p [ 1 ] [ 1 ] + c o s t ( 2 , 3 ) = 0 + 3 = 3 dp[1][1] + cost(2,3) = 0 + 3 = 3 d p [ 1 ] [ 1 ] + cos t ( 2 , 3 ) = 0 + 3 = 3 (trung vị tại 3 3 3 , chi phí = 3 × 1 + 5 × 0 = 3 = 3 \times 1 + 5 \times 0 = 3 = 3 × 1 + 5 × 0 = 3 ). Thử m = 2 m=2 m = 2 : d p [ 1 ] [ 2 ] + c o s t ( 3 , 3 ) = 2 + 0 = 2 dp[1][2] + cost(3,3) = 2 + 0 = 2 d p [ 1 ] [ 2 ] + cos t ( 3 , 3 ) = 2 + 0 = 2 . Tốt nhất là m = 2 m=2 m = 2 .
Step 12 / 19 2 0 3 1 5 2 1 3 4 4 6 5 Children per house 0 0 2 7 8 16 33 0 2 Arrow from dp.cell[0][0] to dp.cell[1][1]: cost(1,1)=0 cost(1,1)=0 Arrow from dp.cell[0][0] to dp.cell[1][2]: cost(1,2)=2 cost(1,2)=2 Arrow from dp.cell[0][0] to dp.cell[1][3]: cost(1,3)=7 cost(1,3)=7 Arrow from dp.cell[1][1] to dp.cell[2][2]: m=1: 0+0 m=1: 0+0 Arrow from dp.cell[1][1] to dp.cell[2][3]: m=1: 0+3 m=1: 0+3 Arrow from dp.cell[1][2] to dp.cell[2][3]: m=2: 2+0 m=2: 2+0 dp[j][i] 0 cost(l,r) d p [ 2 ] [ 3 ] = 2 dp[2][3] = 2 d p [ 2 ] [ 3 ] = 2 . Chia tối ưu: trường thứ nhất phục vụ nhà 1 1 1 -2 2 2 (chi phí 2 2 2 ), trường thứ hai tại nhà 3 3 3 (chi phí 0 0 0 ). Tối ưu chia để trị khai thác tính đơn điệu của o p t ( j , i ) opt(j,i) o pt ( j , i ) .
Step 13 / 19 2 0 3 1 5 2 1 3 4 4 6 5 Children per house 0 0 2 7 8 16 33 0 2 Arrow from dp.cell[0][0] to dp.cell[1][1]: cost(1,1)=0 cost(1,1)=0 Arrow from dp.cell[0][0] to dp.cell[1][2]: cost(1,2)=2 cost(1,2)=2 Arrow from dp.cell[0][0] to dp.cell[1][3]: cost(1,3)=7 cost(1,3)=7 Arrow from dp.cell[1][1] to dp.cell[2][2]: m=1: 0+0 m=1: 0+0 Arrow from dp.cell[1][1] to dp.cell[2][3]: m=1: 0+3 m=1: 0+3 Arrow from dp.cell[1][2] to dp.cell[2][3]: m=2: 2+0 m=2: 2+0 Arrow from dp.cell[1][2] to dp.cell[2][4]: m=2: 2+1 m=2: 2+1 dp[j][i] 1 cost(l,r) d p [ 2 ] [ 4 ] dp[2][4] d p [ 2 ] [ 4 ] : thử m = 1 m=1 m = 1 cho 4 4 4 , m = 2 m=2 m = 2 cho d p [ 1 ] [ 2 ] + c o s t ( 3 , 4 ) = 2 + 1 = 3 dp[1][2]+cost(3,4) = 2+1 = 3 d p [ 1 ] [ 2 ] + cos t ( 3 , 4 ) = 2 + 1 = 3 (trung vị tại 3 3 3 , chi phí = 5 × 0 + 1 × 1 = 1 = 5 \times 0 + 1 \times 1 = 1 = 5 × 0 + 1 × 1 = 1 ), m = 3 m=3 m = 3 cho 7 7 7 . Tốt nhất tại m = 2 m=2 m = 2 .
Step 14 / 19 2 0 3 1 5 2 1 3 4 4 6 5 Children per house 0 0 2 7 8 16 33 0 2 3 Arrow from dp.cell[0][0] to dp.cell[1][1]: cost(1,1)=0 cost(1,1)=0 Arrow from dp.cell[0][0] to dp.cell[1][2]: cost(1,2)=2 cost(1,2)=2 Arrow from dp.cell[0][0] to dp.cell[1][3]: cost(1,3)=7 cost(1,3)=7 Arrow from dp.cell[1][1] to dp.cell[2][2]: m=1: 0+0 m=1: 0+0 Arrow from dp.cell[1][1] to dp.cell[2][3]: m=1: 0+3 m=1: 0+3 Arrow from dp.cell[1][2] to dp.cell[2][3]: m=2: 2+0 m=2: 2+0 Arrow from dp.cell[1][2] to dp.cell[2][4]: m=2: 2+1 m=2: 2+1 dp[j][i] 1 cost(l,r) d p [ 2 ] [ 4 ] = 3 dp[2][4] = 3 d p [ 2 ] [ 4 ] = 3 . Chia tại m = 2 m=2 m = 2 : trường 1 1 1 phục vụ nhà 1 1 1 -2 2 2 (chi phí 2 2 2 ), trường 2 2 2 phục vụ nhà 3 3 3 -4 4 4 (chi phí 1 1 1 ). Tính đơn điệu được giữ: o p t ( 2 , 4 ) = 2 ≥ o p t ( 2 , 3 ) = 2 opt(2,4) = 2 \geq opt(2,3) = 2 o pt ( 2 , 4 ) = 2 ≥ o pt ( 2 , 3 ) = 2 .
Step 15 / 19 2 0 3 1 5 2 1 3 4 4 6 5 Children per house 0 0 2 7 8 16 33 0 2 3 Arrow from dp.cell[0][0] to dp.cell[1][1]: cost(1,1)=0 cost(1,1)=0 Arrow from dp.cell[0][0] to dp.cell[1][2]: cost(1,2)=2 cost(1,2)=2 Arrow from dp.cell[0][0] to dp.cell[1][3]: cost(1,3)=7 cost(1,3)=7 Arrow from dp.cell[1][1] to dp.cell[2][2]: m=1: 0+0 m=1: 0+0 Arrow from dp.cell[1][1] to dp.cell[2][3]: m=1: 0+3 m=1: 0+3 Arrow from dp.cell[1][2] to dp.cell[2][3]: m=2: 2+0 m=2: 2+0 Arrow from dp.cell[1][2] to dp.cell[2][4]: m=2: 2+1 m=2: 2+1 Arrow from dp.cell[1][3] to dp.cell[2][5]: m=3: 7+1 m=3: 7+1 dp[j][i] 1 cost(l,r) d p [ 2 ] [ 5 ] dp[2][5] d p [ 2 ] [ 5 ] : m = 3 m=3 m = 3 cho d p [ 1 ] [ 3 ] + c o s t ( 4 , 5 ) = 7 + 1 = 8 dp[1][3]+cost(4,5) = 7+1 = 8 d p [ 1 ] [ 3 ] + cos t ( 4 , 5 ) = 7 + 1 = 8 (trường tại 5 5 5 , chi phí = 1 × 1 + 4 × 0 = 1 = 1 \times 1 + 4 \times 0 = 1 = 1 × 1 + 4 × 0 = 1 ). m = 4 m=4 m = 4 cũng cho 8 + 0 = 8 8+0=8 8 + 0 = 8 . Tốt nhất là 8 8 8 .
Step 16 / 19 2 0 3 1 5 2 1 3 4 4 6 5 Children per house 0 0 2 7 8 16 33 0 2 3 8 Arrow from dp.cell[0][0] to dp.cell[1][1]: cost(1,1)=0 cost(1,1)=0 Arrow from dp.cell[0][0] to dp.cell[1][2]: cost(1,2)=2 cost(1,2)=2 Arrow from dp.cell[0][0] to dp.cell[1][3]: cost(1,3)=7 cost(1,3)=7 Arrow from dp.cell[1][1] to dp.cell[2][2]: m=1: 0+0 m=1: 0+0 Arrow from dp.cell[1][1] to dp.cell[2][3]: m=1: 0+3 m=1: 0+3 Arrow from dp.cell[1][2] to dp.cell[2][3]: m=2: 2+0 m=2: 2+0 Arrow from dp.cell[1][2] to dp.cell[2][4]: m=2: 2+1 m=2: 2+1 Arrow from dp.cell[1][3] to dp.cell[2][5]: m=3: 7+1 m=3: 7+1 dp[j][i] 1 cost(l,r) d p [ 2 ] [ 5 ] = 8 dp[2][5] = 8 d p [ 2 ] [ 5 ] = 8 . Với 2 2 2 trường cho 5 5 5 nhà, chia tại m = 3 m=3 m = 3 hay m = 4 m=4 m = 4 đều cho chi phí 8 8 8 . Điểm chia tối ưu đã dịch sang phải, xác nhận tính đơn điệu.
Step 17 / 19 2 0 3 1 5 2 1 3 4 4 6 5 Children per house 0 0 2 7 8 16 33 0 2 3 8 Arrow from dp.cell[0][0] to dp.cell[1][1]: cost(1,1)=0 cost(1,1)=0 Arrow from dp.cell[0][0] to dp.cell[1][2]: cost(1,2)=2 cost(1,2)=2 Arrow from dp.cell[0][0] to dp.cell[1][3]: cost(1,3)=7 cost(1,3)=7 Arrow from dp.cell[1][1] to dp.cell[2][2]: m=1: 0+0 m=1: 0+0 Arrow from dp.cell[1][1] to dp.cell[2][3]: m=1: 0+3 m=1: 0+3 Arrow from dp.cell[1][2] to dp.cell[2][3]: m=2: 2+0 m=2: 2+0 Arrow from dp.cell[1][2] to dp.cell[2][4]: m=2: 2+1 m=2: 2+1 Arrow from dp.cell[1][3] to dp.cell[2][5]: m=3: 7+1 m=3: 7+1 Arrow from dp.cell[1][4] to dp.cell[2][6]: m=4: 8+4 m=4: 8+4 dp[j][i] 4 cost(l,r) d p [ 2 ] [ 6 ] dp[2][6] d p [ 2 ] [ 6 ] : m = 4 m=4 m = 4 cho d p [ 1 ] [ 4 ] + c o s t ( 5 , 6 ) = 8 + 4 = 12 dp[1][4]+cost(5,6) = 8+4 = 12 d p [ 1 ] [ 4 ] + cos t ( 5 , 6 ) = 8 + 4 = 12 (trường tại 6 6 6 , chi phí = 4 × 1 + 6 × 0 = 4 = 4 \times 1 + 6 \times 0 = 4 = 4 × 1 + 6 × 0 = 4 ). Các cách chia khác: m = 3 m=3 m = 3 cho 13 13 13 , m = 5 m=5 m = 5 cho 16 16 16 . Tốt nhất tại m = 4 m=4 m = 4 .
Step 18 / 19 2 0 3 1 5 2 1 3 4 4 6 5 Children per house 0 0 2 7 8 16 33 0 2 3 8 12 Arrow from dp.cell[0][0] to dp.cell[1][1]: cost(1,1)=0 cost(1,1)=0 Arrow from dp.cell[0][0] to dp.cell[1][2]: cost(1,2)=2 cost(1,2)=2 Arrow from dp.cell[0][0] to dp.cell[1][3]: cost(1,3)=7 cost(1,3)=7 Arrow from dp.cell[1][1] to dp.cell[2][2]: m=1: 0+0 m=1: 0+0 Arrow from dp.cell[1][1] to dp.cell[2][3]: m=1: 0+3 m=1: 0+3 Arrow from dp.cell[1][2] to dp.cell[2][3]: m=2: 2+0 m=2: 2+0 Arrow from dp.cell[1][2] to dp.cell[2][4]: m=2: 2+1 m=2: 2+1 Arrow from dp.cell[1][3] to dp.cell[2][5]: m=3: 7+1 m=3: 7+1 Arrow from dp.cell[1][4] to dp.cell[2][6]: m=4: 8+4 m=4: 8+4 dp[j][i] 4 cost(l,r) d p [ 2 ] [ 6 ] = 12 dp[2][6] = 12 d p [ 2 ] [ 6 ] = 12 . Chia tại m = 4 m=4 m = 4 : trường 1 1 1 tại nhà 3 3 3 phục vụ nhà 1 1 1 -4 4 4 (chi phí 8 8 8 ), trường 2 2 2 tại nhà 6 6 6 phục vụ nhà 5 5 5 -6 6 6 (chi phí 4 4 4 ). Tổng = 12 = 12 = 12 .
Step 19 / 19 2 0 3 1 5 2 1 3 4 4 6 5 Children per house 0 0 2 7 8 16 33 0 2 3 8 12 Arrow from dp.cell[0][0] to dp.cell[1][1]: cost(1,1)=0 cost(1,1)=0 Arrow from dp.cell[0][0] to dp.cell[1][2]: cost(1,2)=2 cost(1,2)=2 Arrow from dp.cell[0][0] to dp.cell[1][3]: cost(1,3)=7 cost(1,3)=7 Arrow from dp.cell[1][1] to dp.cell[2][2]: m=1: 0+0 m=1: 0+0 Arrow from dp.cell[1][1] to dp.cell[2][3]: m=1: 0+3 m=1: 0+3 Arrow from dp.cell[1][2] to dp.cell[2][3]: m=2: 2+0 m=2: 2+0 Arrow from dp.cell[1][2] to dp.cell[2][4]: m=2: 2+1 m=2: 2+1 Arrow from dp.cell[1][3] to dp.cell[2][5]: m=3: 7+1 m=3: 7+1 Arrow from dp.cell[1][4] to dp.cell[2][6]: m=4: 8+4 m=4: 8+4 dp[j][i] 12 cost(l,r) Đáp án: d p [ 2 ] [ 6 ] = 12 dp[2][6] = 12 d p [ 2 ] [ 6 ] = 12 . Trường đặt tại nhà 3 3 3 và nhà 6 6 6 . Không có tối ưu chia để trị thì mất O ( k n 2 ) O(kn^2) O ( k n 2 ) . Nhờ tính đơn điệu của opt, mỗi hàng giải trong O ( n log n ) O(n \log n) O ( n log n ) , tổng cộng O ( k n log n ) O(kn \log n) O ( kn log n ) . Với n = 3000 n=3000 n = 3000 , đó là sự khác biệt giữa TLE và AC.