题解: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|\),然后求所有 \(S_i\) 中的最大值。本质是二维数据的逐行统计与聚合问题。
-
算法选择:
- 直接模拟:逐行读取实际气温,逐元素计算绝对差并累加,最后取最大值
-
关键步骤:
- 读取输入:\(N\)(地点数)、\(D\)(天数)、预测气温数组 \(T[1..N]\)
- 读取实际气温矩阵:\(A[1..N][1..D]\)
- 逐地点计算(\(i\) 从 \(1\) 到 \(N\)):
- 初始化 \(res = 0\)
- 对 \(j\) 从 \(1\) 到 \(D\):\(res \leftarrow res + |A_{i,j} - T_i|\)
- \(ans = \max(ans, res)\)
- 输出答案:\(ans\)
-
时间/空间复杂度:
- 时间复杂度:\(O(N \cdot D)\),遍历整个矩阵计算绝对差之和
- 空间复杂度:\(O(N \cdot D)\),存储预测气温和实际气温矩阵
-
直接模拟的核心思想:
- 问题分解:将"求所有地点的最大误差和"分解为"先求每个地点的误差和,再求最大值",避免复杂的联合优化
- 绝对值的几何意义:\(|A_{i,j} - T_i|\) 表示预测值与实际值在数轴上的距离,累加后得到总偏离程度
- 行列独立性:每个地点的计算相互独立,无跨地点依赖,因此可以逐行处理,无需存储完整中间结果
- 溢出问题处理:使用
long long防止 \(N \cdot D \cdot \max|A - T|\) 的累加结果超出int范围 - 适用于矩阵遍历、逐行统计、简单聚合类基础数据处理问题
【算法标签】
模拟
【代码详解】
#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号