There are n n n students in a class working on group projects. The students will divide into groups (some students may be in groups alone), work on their independent pieces, and then discuss the results together. It takes the i i i -th student a i a_{i} ai minutes to finish his/her independent piece.
If students work at different paces, it can be frustrating for the faster students and stressful for the slower ones. In particular, the imbalance of a group is defined as the maximum a i a_{i} ai in the group minus the minimum a i a_{i} ai in the group. Note that a group containing a single student has an imbalance of 0 0 0 . How many ways are there for the students to divide into groups so that the total imbalance of all groups is at most k k k ?
Two divisions are considered distinct if there exists a pair of students who work in the same group in one division but different groups in the other.
这道题,我们首先想到排序,对于一个数字我们可以分成 4 4 4 类:贡献为 − 1 -1 −1 的最小值,贡献为 1 1 1 的最大值,贡献为 0 0 0 的中间值或单独一组。
我们我们设 f ( i , j , k ) f(i,j,k) f(i,j,k) 表示前 i i i 个数,有 j j j 组数需要求出最大值,和为 k k k 的方案。
当前数为最小值时,贡献为 f ( i − 1 , j − 1 , k + a i ) f(i-1,j-1,k+a_i) f(i−1,j−1,k+ai)。当前数为最大值时,贡献为 f ( i − 1 , j + 1 , k − a i ) × ( j + 1 ) f(i-1,j+1,k-a_i)\times (j+1) f(i−1,j+1,k−ai)×(j+1),表示 j + 1 j+1 j+1个组任意挑选一个组来进行结尾。当前数为中间值时,贡献为 f ( i − 1 , j , k ) × j f(i-1,j,k)\times j f(i−1,j,k)×j。当前数单独为一组时,贡献为 f ( i − 1 , j , k ) f(i-1,j,k) f(i−1,j,k)。我们发现第三维难以处理,考虑优化。
我们发现对于 [ l , r ] [l,r] [l,r]的贡献,可以简化为 ∑ i = l + 1 r a i − a i − 1 \sum_{i=l+1}^{r} a_i-a_{i-1} ∑i=l+1rai−ai−1,那么我们只要对差分进行求和即可。对于每一组没有结尾的或者刚好结尾的,我们加上这个差分值。
然后就简化成了: f ( i , j , k ) = f ( i − 1 , j − 1 , k − ( j − 1 ) △ ) + ( j + 1 ) × f ( i − 1 , j + 1 , k − ( j + 1 ) △ ) + j × f ( i − 1 , j , k − j △ ) + f ( i − 1 , j , k − j △ ) f(i,j,k)=f(i-1,j-1,k-(j-1)△)\\+(j+1)\times f(i-1,j+1,k-(j+1)△)+\\j\times f(i-1,j,k-j△)+f(i-1,j,k-j△) f(i,j,k)=f(i−1,j−1,k−(j−1)△)+(j+1)×f(i−1,j+1,k−(j+1)△)+j×f(i−1,j,k−j△)+f(i−1,j,k−j△) 答案是 ∑ f ( n , 0 , i ) ( i ≤ m ) \sum f(n,0,i)(i\le m) ∑f(n,0,i)(i≤m)。