A - 山登りトレーニング / Mountain Climbing Training 解説 by admin
Claude 4.5 OpusOverview
This problem asks us to find how many lessons Takahashi needs to take in order to reach a target altitude of \(T\) or higher. The constraint is that lessons can only be received from “people with higher altitude than yourself.”
Analysis
Key Observations
- Lesson constraint: Takahashi can only learn from members who have a reachable altitude strictly higher than his own
- Altitude after lesson: After taking a lesson, Takahashi reaches the same altitude as the member he learned from
- Goal: He just needs to eventually reach an altitude of \(T\) or higher
Case Analysis
We need to consider several cases:
- Already achieved: If \(P \geq T\), no lessons are needed (cost \(0\))
- Only member: If \(N = 1\), there’s no one to learn from (impossible to achieve)
- No one can reach the goal: If the maximum altitude among all members is less than \(T\), it’s impossible to achieve
Optimal Strategy Analysis
When the goal is achievable, how can we reach it with minimum cost?
Example: \(P = 100\), \(T = 500\), other members’ altitudes are \([150, 300, 600]\) - Learn directly from the person at \(600\) → achieved in 1 lesson! (cost \(C\))
Example: \(P = 100\), \(T = 500\), other members’ altitudes are \([150, 300, 400]\) - No one can reach \(500\) or higher, so it’s impossible to achieve
Example: \(P = 100\), \(T = 500\), other members’ altitudes are \([50, 500]\) - The person at \(500\) is higher than \(P = 100\), so learning from them is possible → achieved in 1 lesson!
Justification of Greedy Approach
It is optimal to always learn from “the person with the highest altitude among those higher than your current altitude.” - Learning from someone with a mediocre altitude won’t reduce the total number of lessons needed - By learning from the person with the maximum altitude, you maximize your options for the next step
Algorithm
- If \(P \geq T\), output
0and terminate - If \(N = 1\), output
-1and terminate - Read the list of other members’ altitudes; if the maximum is less than \(T\), output
-1and terminate - Simulate using the greedy approach:
- Initialize current altitude
currentto \(P\) - While
current < T, repeat the following:- Find the maximum altitude among those greater than
current - If not found, output
-1(though this should be caught by step 3) - If found, update
currentand add \(C\) to the cost
- Find the maximum altitude among those greater than
- Initialize current altitude
- Output the final cost
Complexity
- Time complexity: \(O(N \times K)\) (where \(K\) is the number of lessons, worst case \(O(N^2)\))
- This is because we scan all members for each lesson
- However, in practice, since we greedily choose the maximum, \(K\) is at most \(N-1\)
- Space complexity: \(O(N)\) (to store the list of members’ altitudes)
Implementation Notes
Initial state check: By handling the case \(P \geq T\) first, we can keep the subsequent logic simple
Special handling for N = 1: Since the second line of input would be empty, we need to check this first
Direct achievement case: If there exists a member with altitude \(T\) or higher and greater than \(P\), the goal can be achieved in 1 lesson
Loop termination condition: Exit the loop when
current >= T. Since the goal is “\(T\) or higher,” it doesn’t matter if we don’t reach exactly \(T\)Source Code
def solve():
line1 = input().split()
N, P, T, C = int(line1[0]), int(line1[1]), int(line1[2]), int(line1[3])
# If already at or above target
if P >= T:
print(0)
return
# N=1 means only Takahashi exists, no one to learn from
if N == 1:
print(-1)
return
# Read other members' heights
S = list(map(int, input().split()))
# Find the maximum height among other members that is greater than P
# We need to find if there's anyone with height >= T
max_height = max(S)
# If no one can reach T or higher, impossible
if max_height < T:
print(-1)
return
# Otherwise, we can achieve the goal
# Strategy: We need to reach height >= T
# Each lesson costs C, and we can only learn from someone with strictly higher altitude
# The optimal strategy is to find someone with height >= T and learn from them
# But we can only learn from someone with strictly higher altitude than our current
# If there exists someone with height >= T and height > P, we can learn directly (cost C)
# Otherwise, we need to chain: learn from someone > P, then learn from someone >= T
# Check if there's anyone with height >= T and height > P
can_reach_directly = any(s >= T and s > P for s in S)
if can_reach_directly:
print(C)
return
# Otherwise, we need to check if we can reach someone with height >= T through chaining
# We need: someone with height > P, and someone with height >= T
# Since we already checked max_height >= T, we know someone has height >= T
# But that person might have height <= P
# For chaining to work:
# 1. Find someone with height h1 where h1 > P
# 2. After learning from them, find someone with height h2 where h2 > h1 and h2 >= T
# Or directly find someone with height >= T and > P
# Since the direct case is already handled, we need chaining
# For chaining: we need heights that form a strictly increasing sequence from P to >= T
# The minimum chain: find heights > P, then from the maximum reachable, find heights > that, etc.
#
# Greedy approach: always jump to the maximum available height > current height
current = P
cost = 0
while current < T:
# Find max height strictly greater than current
next_height = -1
for s in S:
if s > current:
next_height = max(next_height, s)
if next_height == -1:
# Can't improve anymore
print(-1)
return
current = next_height
cost += C
print(cost)
solve()
This editorial was generated by claude4.5opus.
投稿日時:
最終更新: