H - チーム分け / Team Division Editorial by admin Editorial for printing
gpt-6-astra-mediumOverview
We divide \(N\) people into teams of at least \(K\) people each, minimizing the sum of the difference between the maximum and minimum ratings within each team.
We reduce this to the problem of partitioning the sequence sorted by rating into contiguous subarrays, and solve it by optimizing the transitions in dynamic programming.
Analysis
1. Partitioning into contiguous subarrays sorted by rating is sufficient
First, sort the ratings in ascending order with \(0\)-based indexing:
\(a_0 \le a_1 \le \dots \le a_{N-1}\)
Here, we show that it suffices to consider only partitions where each team forms a contiguous subarray of the sorted sequence.
For each team, consider the interval from the minimum rating to the maximum rating. If the intervals \([l_1,r_1]\) and \([l_2,r_2]\) of two teams overlap, the imbalance when merging these two teams satisfies:
\(\max(r_1,r_2)-\min(l_1,l_2) \le (r_1-l_1)+(r_2-l_2)\)
In other words, merging overlapping teams does not increase the total imbalance. Furthermore, since there is no upper limit on team size, the team size condition is still satisfied after merging.
Repeating this process ensures that no two teams have overlapping rating intervals. In this state, each team corresponds to a contiguous subarray of the sorted sequence.
Therefore, an optimal solution exists among partitions into contiguous subarrays.
For example, if the ratings are \([1,2,8,9]\) and \(K=2\), dividing them into \([1,2]\) and \([8,9]\) gives a total imbalance of \((2-1)+(9-8)=2\).
2. Formulating Dynamic Programming
Let \(dp[i]\) be the minimum total imbalance when validly partitioning the first \(i\) people into teams.
The initial values are as follows:
- \(dp[0]=0\)
- All other values are initially \(\infty\), representing unreachable states.
If the last team consists of \(a_j,a_{j+1},\dots,a_{i-1}\), its size is \(i-j\), which requires \(j \le i-K\).
Since the imbalance of this team is \(a_{i-1}-a_j\), the transition is:
\(dp[i]=\min_{0\le j\le i-K}\left(dp[j]+a_{i-1}-a_j\right)\)
Note that if the first \(j\) people cannot be partitioned, \(dp[j]\) is treated as \(\infty\).
However, examining all \(j\) for each \(i\) takes \(O(N^2)\) time in the worst case, which is too slow for \(N \le 2\times 10^5\).
3. Reusing the Minimum Value in Transitions
Rewriting the transition formula gives:
\(dp[i]=a_{i-1}+\min_{0\le j\le i-K}\left(dp[j]-a_j\right)\)
Here, when \(i\) increases by \(1\), the only candidate newly added to the range of the minimum is \(j=i-K\). No previous candidates are ever removed.
Therefore, we can maintain
\(best=\min_{0\le j\le i-K}\left(dp[j]-a_j\right)\)
in a variable. For each \(i\), it can be updated simply by comparing with the new candidate \(dp[i-K]-a_{i-K}\).
This allows the DP to be computed in \(O(N)\) time.
Algorithm
- If \(N<K\), even putting everyone into a single team will not meet the team size requirement, so output \(-1\).
- If \(K=1\), putting everyone into teams of size \(1\) results in a total imbalance of \(0\), so output \(0\).
- Sort the ratings in ascending order.
- Initialize \(dp[0]=0\), and set all other entries of \(dp\) and \(best\) to \(\infty\).
- For \(i=K,K+1,\dots,N\) in this order, perform the following:
- Let \(j=i-K\).
- Update \(best \leftarrow \min(best,dp[j]-a_j)\).
- Set \(dp[i] \leftarrow a_{i-1}+best\).
- Output \(dp[N]\).
Since it suffices to consider partitions into contiguous subarrays, and the DP considers all possible starting positions of the last team, this yields the optimal value.
Complexity
- Time Complexity: \(O(N\log N)\)
Sorting takes \(O(N\log N)\), and the DP takes \(O(N)\). - Space Complexity: \(O(N)\)
To store the sorted ratings and the DP array.
Implementation Notes
Pay attention to the difference between counts and indices.
Since \(dp[i]\) represents the value for the first \(i\) people, the rating of the last person among them isa[i - 1].Update
bestbefore computingdp[i].
The newly added candidate \(j=i-K\) corresponds to the case where the last team has exactly \(K\) people. This candidate must be included.Keep
dp[i]as unreachable for \(1\le i<K\).
A team cannot be formed with fewer than \(K\) people. On the other hand, \(dp[0]=0\) is necessary to represent the base state where no one has been assigned yet.Use a sufficiently large value for \(\infty\).
If \(N\ge K\), putting everyone into a single team is always a valid partition, and its imbalance is at most \(10^9-1\). In the code, a value much larger than this, such as10**30, is used.Source Code
import sys
def main():
data = list(map(int, sys.stdin.buffer.read().split()))
n, k = data[0], data[1]
if n < k:
print(-1)
return
if k == 1:
print(0)
return
a = sorted(data[2:])
inf = 10**30
dp = [inf] * (n + 1)
dp[0] = 0
best = inf
for i in range(k, n + 1):
j = i - k
candidate = dp[j] - a[j]
if candidate < best:
best = candidate
dp[i] = a[i - 1] + best
print(dp[n])
if __name__ == "__main__":
main()
This editorial was generated by gpt-6-astra-medium.
posted:
last update: