题解:AtCoder AT_awc0098_a Error Analysis of Temperature Forecasts
【题目来源】
AtCoder:A - Error Analysis of Temperature Forecasts
【题目描述】
Takahashi aims to become a weather forecaster and is practicing by forecasting the daily temperature at \(N\) locations.
Takahashi forecasted the temperature at each location over \(D\) days and recorded the results. Since he has just started practicing, his forecasted temperature for location \(i\) is the same value \(T_i\) degrees (Celsius) every day. On the other hand, the actual temperature at location \(i\) on day \(j\) was \(A_{i,j}\) degrees.
The "forecast error" for location \(i\) on day \(j\) is defined as the absolute difference between the forecasted temperature and the actual temperature, \(|A_{i,j} - T_i|\).
To review the accuracy of his forecasts, Takahashi decided to find the sum of the forecast errors over the \(D\) days for each location. Specifically, for each location \(i\) (\(1 \leq i \leq N\)), he calculates the total forecast error:
\(S_i = \sum_{j=1}^{D} |A_{i,j} - T_i|\)
Find the maximum value among \(S_1, S_2, \ldots, S_N\).
高橋立志成为一名天气预报员,正在通过预测 \(N\) 个地点的每日气温来练习。
高橋预测了每个地点 \(D\) 天的气温并记录了结果。由于他刚开始练习,他对地点 \(i\) 预测的气温每天都是相同的值 \(T_i\) 度(摄氏度)。另一方面,地点 \(i\) 在第 \(j\) 天的实际气温为 \(A_{i,j}\) 度。
地点 \(i\) 在第 \(j\) 天的"预报误差"定义为预测气温与实际气温的绝对差值,即 \(|A_{i,j} - T_i|\)。
为了检查预报的准确性,高橋决定计算每个地点 \(D\) 天的预报误差之和。具体地,对于每个地点 \(i\)(\(1 \le i \le N\)),他计算总预报误差:
\(S_i = \sum_{j=1}^{D} |A_{i,j} - T_i|\)
求 \(S_1, S_2, \ldots, S_N\) 中的最大值。
【输入】
\(N\) \(D\)
\(T_1\) \(T_2\) \(\ldots\) \(T_N\)
\(A_{1,1}\) \(A_{1,2}\) \(\ldots\) \(A_{1,D}\)
\(A_{2,1}\) \(A_{2,2}\) \(\ldots\) \(A_{2,D}\)
\(\vdots\)
\(A_{N,1}\) \(A_{N,2}\) \(\ldots\) \(A_{N,D}\)
- The first line contains the number of locations \(N\) and the number of days \(D\), separated by a space.
- The second line contains the forecasted temperatures for each location, \(T_1, T_2, \ldots, T_N\), separated by spaces.
- The third to \((N + 2)\)-th lines contain the actual temperatures for each location.
- The \((2 + i)\)-th line (\(1 \leq i \leq N\)) contains the actual temperatures at location \(i\) over the \(D\) days, \(A_{i,1}, A_{i,2}, \ldots, A_{i,D}\), separated by spaces.
【输出】
Print the maximum value among \(S_1, S_2, \ldots, S_N\) as an integer in a single line.
【输入样例】
3 4
10 0 -5
12 8 10 15
0 -3 4 1
-10 -5 0 -4
【输出样例】
11
【核心思想】
-
问题分析:给定 \(N\) 个地点,每个地点有固定预测气温 \(T_i\) 和 \(D\) 天实际气温 \(A_{i,j}\)。需计算每个地点 \(D\) 天预报误差的绝对值之和 \(S_i = \sum_{j=1}^{D} |A_{i,j} - T_i|\),并求 \(\max(S_1, \ldots, S_N)\)。这是一个矩阵逐行统计问题,关键在于按地点独立计算累加和,再取最大值。
-
算法选择:
- 直接模拟:逐行读取实际气温,对每个地点计算 \(|A_{i,j} - T_i|\) 的累加和
- 在线比较:每计算完一个地点的 \(S_i\) 立即与当前最大值比较,无需存储所有 \(S_i\)
-
关键步骤:
- 读入数据:\(N, D\),预测气温数组 \(T[1..N]\),实际气温矩阵 \(A[1..N][1..D]\)
- 逐地点计算(\(i\) 从 \(1\) 到 \(N\)):
- 初始化 \(res = 0\)
- 遍历 \(j\) 从 \(1\) 到 \(D\):\(res += |A_{i,j} - T_i|\)
- \(ans = \max(ans, res)\)
- 输出 \(ans\)
-
时间/空间复杂度:
- 时间复杂度:\(O(N \cdot D)\),遍历整个 \(N \times D\) 矩阵
- 空间复杂度:\(O(N \cdot D)\),存储气温矩阵(可优化为 \(O(D)\) 逐行处理)
-
逐行统计的核心思想:
- 行列独立性:每个地点的误差计算仅依赖该地点的 \(T_i\) 和 \(A_{i,*}\),与其他地点无关,天然适合逐行并行处理
- 绝对值累加的几何意义:\(S_i\) 表示预测值 \(T_i\) 与实际值序列在 \(D\) 维空间中的曼哈顿距离,衡量整体偏离程度
- 在线最大值维护:不存储所有 \(S_i\),而是用单个变量 \(ans\) 实时更新,将空间从 \(O(N)\) 降为 \(O(1)\)
- 适用于"矩阵逐行聚合后求最值"的基础数据处理问题
【算法标签】
模拟
【代码详解】
#include <bits/stdc++.h>
using namespace std;
#define int long long // 将 int 定义为 long long,防止求和时溢出
const int N = 10005; // 最大数组维度(N和D的最大值)
int n, d, ans = -1; // n: 地点数量, d: 天数, ans: 所有地点中最大的总预报误差
int t[N]; // t[i]: 地点i的预测气温(每天相同)
int a[N][N]; // a[i][j]: 地点i在第j天的实际气温
signed main() // 使用 signed 替代 int,因为 #define int long long
{
cin >> n >> d; // 读入地点数量 N 和天数 D
// 读入每个地点的预测气温 T_i
for (int i = 1; i <= n; i++)
cin >> t[i];
// 读入每个地点每天的实际气温 A_{i,j}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= d; j++)
cin >> a[i][j];
// 遍历每个地点,计算总预报误差 S_i
for (int i = 1; i <= n; i++)
{
int res = 0; // res: 当前地点i的D天预报误差之和
for (int j = 1; j <= d; j++)
res += abs(a[i][j] - t[i]); // 累加每天预报误差的绝对值
ans = max(ans, res); // 更新最大总预报误差
}
cout << ans << endl; // 输出所有地点中最大的总预报误差
return 0;
}
【运行结果】
3 4
10 0 -5
12 8 10 15
0 -3 4 1
-10 -5 0 -4
11
浙公网安备 33010602011771号