题解:AtCoder AT_awc0087_c Shortest Mountain Climbing Route
【题目来源】
AtCoder:C - Shortest Mountain Climbing Route
【题目描述】
Takahashi enjoys mountain climbing and is planning a traverse route through a mountain range. The mountain range has \(N\) points arranged in a line, numbered from \(1\) to \(N\), and the elevation of each point \(i\) (\(1 \leq i \leq N\)) is \(A_i\) meters.
Takahashi chooses a pair of integers \((l, r)\) satisfying \(1 \leq l \leq r \leq N\), and walks a route that passes through each point in order from point \(l\) to point \(r\) in ascending order of their numbers. The number of points included in this route is \(r - l + 1\).
The total elevation change of this route is defined as the sum of the absolute differences in elevation between adjacent points along the route, namely:
\(\sum_{i=l}^{r-1} |A_{i+1} - A_i|\)
Note that when \(l = r\), this sum is an empty sum, so the total elevation change is \(0\).
Takahashi wants to walk a route with a total elevation change of at least \(K\) for training purposes. However, since he is short on time, he wants to choose a route that satisfies the condition while containing the fewest number of points.
If a route with a total elevation change of at least \(K\) exists, find the minimum number of points included in such a route. If no such route exists, output \(-1\).
高桥热爱登山,正在规划一条穿越山脉的路线。这座山脉有 \(N\) 个点排成一行,编号从 \(1\) 到 \(N\),每个点 \(i\)(\(1 \leq i \leq N\))的海拔高度为 \(A_i\) 米。
高桥选择一对满足 \(1 \leq l \leq r \leq N\) 的整数 \((l, r)\),并按编号递增的顺序依次经过从点 \(l\) 到点 \(r\) 的每个点。这条路线包含的点的数量为 \(r - l + 1\)。
这条路线的总海拔变化定义为沿线相邻点之间海拔差的绝对值之和,即:
\(\sum_{i=l}^{r-1} |A_{i+1} - A_i|\)
注意,当 \(l = r\) 时,这是一个空和,因此总海拔变化为 \(0\)。
高桥为了训练目的,想要走一条总海拔变化至少为 \(K\) 的路线。但是,由于时间有限,他想在满足条件的前提下选择包含最少点的路线。
如果存在总海拔变化至少为 \(K\) 的路线,求此类路线中包含的最小点数。如果不存在这样的路线,输出 \(-1\)。
【输入】
\(N\) \(K\)
\(A_1\) \(A_2\) \(\ldots\) \(A_N\)
- The first line contains an integer \(N\) representing the number of points and an integer \(K\) representing the lower bound on the total elevation change, separated by a space.
- The second line contains integers \(A_1, A_2, \ldots, A_N\) representing the elevation of each point, separated by spaces.
【输出】
If a route with a total elevation change of at least \(K\) exists, output the minimum number of points included in such a route on a single line. If no such route exists, output \(-1\).
【输入样例】
5 5
1 4 2 5 3
【输出样例】
3
【核心思想】
-
问题分析:给定 \(N\) 个点的海拔高度 \(A_i\) 和目标海拔变化 \(K\)。定义区间 \([l, r]\) 的总海拔变化为 \(\sum_{i=l}^{r-1} |A_{i+1} - A_i|\)。需要找到总海拔变化至少为 \(K\) 的最短区间(包含点数最少)。这是一个滑动窗口问题,关键在于利用单调性快速找到满足条件的最短区间。
-
算法选择:
- 滑动窗口(双指针):维护一个可变长度的窗口 \([l, r]\),右指针 \(r\) 向右扩展窗口,左指针 \(l\) 在条件满足时向右收缩窗口
- 贪心策略:当窗口内的总海拔变化 \(\geq K\) 时,尝试收缩左边界以找到更短的满足条件的区间
-
关键步骤:
- 初始化:左指针 \(l = 1\),右指针 \(r = 1\),当前和 \(sum = 0\),答案 \(ans = N + 1\)(初始化为无穷大)
- 滑动窗口遍历(\(r\) 从 \(1\) 到 \(N\)):
- 扩展窗口:如果 \(r > l\),将 \(|A[r] - A[r-1]|\) 加入 \(sum\)
- 收缩窗口(当 \(l < r\) 且 \(sum \geq K\) 时循环):
- 更新答案:\(ans = \min(ans, r - l + 1)\)
- 移除左边界的贡献:\(sum -= |A[l+1] - A[l]|\)
- \(l++\)(左边界右移)
- 输出结果:如果 \(ans == N + 1\) 输出 \(-1\),否则输出 \(ans\)
-
时间/空间复杂度:
- 时间复杂度:\(O(N)\),每个指针最多移动 \(N\) 次
- 空间复杂度:\(O(N)\),存储海拔高度数组
-
滑动窗口的核心思想:
- 单调性:右指针 \(r\) 只向右移动,左指针 \(l\) 也只向右移动,不会回溯
- 双指针协同:右指针负责扩展窗口使条件满足,左指针负责收缩窗口寻找最优解
- 最优性保证:当条件满足时,当前窗口长度是一个候选答案,通过收缩左边界尝试找到更短的满足条件的窗口
- 适用场景:寻找满足某种条件的最短/最长子数组、子区间问题
- 适用于连续子数组、子区间统计问题
【算法标签】
双指针
【代码详解】
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 200005; // 定义最大数组长度
int n, k; // 数组长度n,目标和k
int a[N], sa[N]; // 数组a,前缀和数组sa(此处未使用)
signed main() // 主函数
{
cin >> n >> k; // 输入数组长度和目标和
for (int i=1; i<=n; i++) // 读取数组元素
cin >> a[i]; // 输入第i个元素
// for (int i=2; i<=n; i++) // 计算相邻元素差的前缀和(被注释掉)
// sa[i] = sa[i-1] + abs(a[i]-a[i-1]);
int sum = 0; // 当前窗口内相邻元素差的绝对值之和
int ans=n+1; // 初始化答案为n+1(表示未找到)
for (int l=1, r=1; r<=n; r++) // 滑动窗口,l为左指针,r为右指针
{
if (r>l) // 如果窗口内有多个元素
sum += abs(a[r]-a[r-1]); // 将新增的相邻差加入总和
while (l<r && sum>=k) // 当总和超过或等于k时,尝试缩小窗口
{
ans = min(ans, r - l + 1); // 更新最小窗口长度
sum -= abs(a[l+1]-a[l]); // 移除左边界相邻差
l++; // 左指针右移
}
}
if (ans==n+1) cout << -1 << endl; // 如果未找到满足条件的窗口,输出-1
else cout << ans << endl; // 否则输出最小窗口长度
return 0; // 程序正常结束
}
【运行结果】
5 5
1 4 2 5 3
3
浙公网安备 33010602011771号