Frog Jumping Across Stones
- Memory limit
- 256 MB
- Correctness time limit
- 10000 ms
- Solvers
- 0
N stepping stones lie in a row across a stream; stone i has height h[i]. A frog starts on stone 0 and wants to reach
stone N-1. From stone i it may jump to any stone j > i in one jump, which costs
where c is a fixed cost per jump. Return the minimum total cost to reach stone N-1.
Examples
| h | c | result |
|---|---|---|
[1, 3, 6, 10] | 5 | 44 |
[5, 1, 5] | 100 | 100 |
[1, 1000000] | 0 | 999998000001 |
Example 1 — stepping on every stone costs ; jumping straight to the end costs 86 and
0 → 2 → 3 costs 51.
Example 2 — the fixed cost is large, so jumping 0 → 2 directly (height difference 0) costs only .
Example 3 — the answer may not fit in 32 bits.
Efficiency
This problem has performance tests. Judge the required time complexity from the input sizes in the constraints.
Function signature
solution(h: int[], c: long) → long
Constraints
-
h.length -
h[i](heights are not necessarily sorted) -
c
Expected complexity
- Time
- O(N log N)
- Space
- O(N)
Examples
| h | c | Expected |
|---|---|---|
[1,3,6,10] | 5 | 44 |
[5,1,5] | 100 | 100 |
[1,1000000] | 0 | 999998000001 |
Hint
Hint
dp[i] = min_{j<i} dp[j] + (h[i]-h[j])^2 + c is as written. Expanding the square, each earlier stone j is a line
and dp[i] is the minimum of those lines at , plus . Heights are unsorted,
so use a Li Chao tree over the compressed heights ( per insert/query), or CDQ divide and conquer with a monotone
hull. Use 64-bit integers. The all-pairs DP exceeds the time limit.
Time limits per language
| Language | Efficiency time limit | |
|---|---|---|
| C (GCC, gnu17) | 200 ms | estimated |
| C++ (GCC, gnu++23) | 200 ms | derived |
| Python 3.13 | 2950 ms | derived |
| Java 25 | 1344 ms | derived |
| Kotlin 2.4 (JVM) | 1400 ms | estimated |
| C# 14 (.NET 10) | 1400 ms | estimated |
Test groups
| Group | Weight | Tests | Visibility |
|---|---|---|---|
| sample | 0 | 3 | inputs and outputs |
| correctness | 40 | 12 | verdict only |
| performance | 60 | 4 | verdict only |