B - チーム編成 / Team Formation 解説 by admin
gemini-3-flash-previewOverview
Given \(N\) candidate players, the problem asks you to form a team of \(K\) players that must include a specific player (Aoki), and then fill the remaining \(K-1\) spots by selecting players with the highest ability values, computing the team’s total ability value.
Approach
The key idea of this problem is “fix Aoki, then optimally fill the remaining spots.”
- Handling Aoki: Player number \(T\) (Aoki) is always included in the team regardless of their ability value. Therefore, we first secure Aoki’s ability value \(A_T\).
- Selecting the remaining members: Since the team has \(K\) slots, we need to select \(K-1\) players from the remaining candidates (excluding Aoki). To maximize the total, we simply pick the top \(K-1\) players by ability value from the \(N-1\) remaining candidates.
- Efficient selection: To “select in descending order,” the most efficient approach is to sort the data in descending order.
For example, if the players’ ability values are \([10, 30, 20, 40]\), \(K=3\), and Aoki is player 1 (\(A_1=10\)): - Select Aoki (10). - The remaining are \([30, 20, 40]\). - Sorting in descending order gives \([40, 30, 20]\). - Select the top \(K-1 = 2\) players (40 and 30). - The total is \(10 + 40 + 30 = 80\).
Algorithm
- Read \(N, K, T\) and the ability value list \(A\) for each player from input.
- Store Aoki’s ability value \(A_T\) in a variable (note that arrays in most programming languages are 0-indexed, so be careful with index handling).
- Remove Aoki’s ability value from list \(A\).
- Sort the remaining \(N-1\) players’ ability values in descending order.
- Sum the first \(K-1\) elements of the sorted list and add Aoki’s ability value — this is the answer.
Complexity
- Time complexity: \(O(N \log N)\)
- Reading input takes \(O(N)\), sorting the list takes \(O(N \log N)\), and computing the sum of the top \(K-1\) players takes \(O(K)\). Overall, the sorting step dominates. Since \(N = 2 \times 10^5\), this complexity is well within the time limit.
- Space complexity: \(O(N)\)
- We store all players’ ability values in a list, using memory proportional to the number of players \(N\).
Implementation Notes
Handling 1-indexed input: The problem gives player numbers from \(1\) to \(N\), but Python lists start from \(0\), so Aoki’s index becomes
T-1.Case \(K=1\): When \(K=1\), the team consists of Aoki alone. In this case, the number of players to select from the sorted list is \(K-1 = 0\), so only Aoki’s ability value is reflected in the total (Python’s slice
a[:0]returns an empty list, so this works correctly).Fast I/O: Since \(N\) can be as large as \(2 \times 10^5\), it is advisable to process input efficiently, for example by using
sys.stdin.read().split().Source Code
import sys
def solve():
# 入力をすべて読み込む
input_data = sys.stdin.read().split()
if not input_data:
return
n = int(input_data[0])
k = int(input_data[1])
t = int(input_data[2])
# 選手の実力値をリストに格納
a = list(map(int, input_data[3:]))
# 青木君の実力値を取得(選手番号 T は 1-indexed)
aoki_ability = a[t-1]
# 青木君を候補から除外する
a.pop(t-1)
# 残りの候補を実力値の高い順(降順)にソート
a.sort(reverse=True)
# 青木君の実力値と、残りから選ぶ K-1 人の実力値の合計を計算
# K=1 の場合は sum(a[:0]) となり 0 が加算される
ans = aoki_ability + sum(a[:k-1])
# 結果を出力
print(ans)
if __name__ == '__main__':
solve()
This editorial was generated by gemini-3-flash-preview.
投稿日時:
最終更新: