题解:AtCoder AT_awc0098_a Error Analysis of Temperature Forecasts

题解:AtCoder AT_awc0098_a Error Analysis of Temperature Forecasts
本文分享的必刷题目是从蓝桥云课、洛谷、AcWing等知名刷题平台精心挑选而来并结合各平台提供的算法标签和难度等级进行了系统分类。题目涵盖了从基础到进阶的多种算法和数据结构旨在为不同阶段的编程学习者提供一条清晰、平稳的学习提升路径。欢迎大家订阅我的专栏算法题解C与Python实现附上汇总贴算法竞赛备考冲刺必刷题C | 汇总【题目来源】AtCoderA - Error Analysis of Temperature Forecasts【题目描述】Takahashi aims to become a weather forecaster and is practicing by forecasting the daily temperature atN NNlocations.Takahashi forecasted the temperature at each location overD DDdays and recorded the results. Since he has just started practicing, his forecasted temperature for locationi iiis the same valueT i T_iTi​degrees (Celsius) every day. On the other hand, the actual temperature at locationi iion dayj jjwasA i , j A_{i,j}Ai,j​degrees.The “forecast error” for locationi iion dayj jjis defined as the absolute difference between the forecasted temperature and the actual temperature,∣ A i , j − T i ∣ |A_{i,j} - T_i|∣Ai,j​−Ti​∣.To review the accuracy of his forecasts, Takahashi decided to find the sum of the forecast errors over theD DDdays for each location. Specifically, for each locationi ii(1 ≤ i ≤ N 1 \leq i \leq N1≤i≤N), he calculates the total forecast error:S i ∑ j 1 D ∣ A i , j − T i ∣ S_i \sum_{j1}^{D} |A_{i,j} - T_i|Si​∑j1D​∣Ai,j​−Ti​∣Find the maximum value amongS 1 , S 2 , … , S N S_1, S_2, \ldots, S_NS1​,S2​,…,SN​.高橋立志成为一名天气预报员正在通过预测N NN个地点的每日气温来练习。高橋预测了每个地点D DD天的气温并记录了结果。由于他刚开始练习他对地点i ii预测的气温每天都是相同的值T i T_iTi​度摄氏度。另一方面地点i ii在第j jj天的实际气温为A i , j A_{i,j}Ai,j​度。地点i ii在第j jj天的预报误差定义为预测气温与实际气温的绝对差值即∣ A i , j − T i ∣ |A_{i,j} - T_i|∣Ai,j​−Ti​∣。为了检查预报的准确性高橋决定计算每个地点D DD天的预报误差之和。具体地对于每个地点i ii1 ≤ i ≤ N 1 \le i \le N1≤i≤N他计算总预报误差S i ∑ j 1 D ∣ A i , j − T i ∣ S_i \sum_{j1}^{D} |A_{i,j} - T_i|Si​∑j1D​∣Ai,j​−Ti​∣求S 1 , S 2 , … , S N S_1, S_2, \ldots, S_NS1​,S2​,…,SN​中的最大值。【输入】N NND DDT 1 T_1T1​T 2 T_2T2​… \ldots…T N T_NTN​A 1 , 1 A_{1,1}A1,1​A 1 , 2 A_{1,2}A1,2​… \ldots…A 1 , D A_{1,D}A1,D​A 2 , 1 A_{2,1}A2,1​A 2 , 2 A_{2,2}A2,2​… \ldots…A 2 , D A_{2,D}A2,D​⋮ \vdots⋮A N , 1 A_{N,1}AN,1​A N , 2 A_{N,2}AN,2​… \ldots…A N , D A_{N,D}AN,D​The first line contains the number of locationsN NNand the number of daysD DD, separated by a space.The second line contains the forecasted temperatures for each location,T 1 , T 2 , … , T N T_1, T_2, \ldots, T_NT1​,T2​,…,TN​, separated by spaces.The third to( N 2 ) (N 2)(N2)-th lines contain the actual temperatures for each location.The( 2 i ) (2 i)(2i)-th line (1 ≤ i ≤ N 1 \leq i \leq N1≤i≤N) contains the actual temperatures at locationi iiover theD DDdays,A i , 1 , A i , 2 , … , A i , D A_{i,1}, A_{i,2}, \ldots, A_{i,D}Ai,1​,Ai,2​,…,Ai,D​, separated by spaces.【输出】Print the maximum value amongS 1 , S 2 , … , S N S_1, S_2, \ldots, S_NS1​,S2​,…,SN​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【算法标签】#模拟【代码详解】#includebits/stdc.husingnamespacestd;#defineintlonglong// 将 int 定义为 long long防止求和时溢出constintN10005;// 最大数组维度N和D的最大值intn,d,ans-1;// n: 地点数量, d: 天数, ans: 所有地点中最大的总预报误差intt[N];// t[i]: 地点i的预测气温每天相同inta[N][N];// a[i][j]: 地点i在第j天的实际气温signedmain()// 使用 signed 替代 int因为 #define int long long{cinnd;// 读入地点数量 N 和天数 D// 读入每个地点的预测气温 T_ifor(inti1;in;i)cint[i];// 读入每个地点每天的实际气温 A_{i,j}for(inti1;in;i)for(intj1;jd;j)cina[i][j];// 遍历每个地点计算总预报误差 S_ifor(inti1;in;i){intres0;// res: 当前地点i的D天预报误差之和for(intj1;jd;j)resabs(a[i][j]-t[i]);// 累加每天预报误差的绝对值ansmax(ans,res);// 更新最大总预报误差}coutansendl;// 输出所有地点中最大的总预报误差return0;}【运行结果】3 4 10 0 -5 12 8 10 15 0 -3 4 1 -10 -5 0 -4 11