https://codeforces.com/problemset/problem/1547/E
题意:有长度为n的数组,还有k个空调,给出空调所在的位置跟温度,求n个位置所有位置的温度。如果某个位置没有温度,那么这个位置的温度是左边的空调或者右边的空调传过来的温度中较低的一个,其中传过来的温度的计算共识是a[j] + abs(i - j)(假设当前位置是i)。
思路:维护一个left和right数组,表示当前位置上左边传过来的最低温度和右边传过来的最低温度,按顺序递推一遍即可,然后再同时遍历一次left和right,取每个位置上的较小值即可。
总结:left的下标应该是比i晚一个,pref[i + 1] = a[i] + pref[i],right应该是suff[i] = a[i] + suff[i + 1],然后遍历左右就是ans[i] = pref[i + 1] + suff[i],或者ans[i] = pref[i] + suff[i - 1]
inline void solve() {int n, k;cin >> n >> k;vector<int> a(n);vector<int> pos(k);for (int i = 0; i < k; ++i) {cin >> pos[i];pos[i] --;}for (int i = 0; i < k; ++i) {int t;cin >> t;a[pos[i]] = t;}vector<int> left(n + 1, INF), right(n + 1, INF);for (int i = 0; i < n; ++i) {if (a[i]) {left[i + 1] = a[i];}left[i + 1] = min(left[i + 1], left[i] + 1);}for (int i = n - 1; i >= 0; --i) {if (a[i]) {right[i] = a[i];}right[i] = min(right[i + 1] + 1, right[i]);}vector<int> ans(n + 1);for (int i = 1; i <= n; ++i) {ans[i] = min(left[i], right[i - 1]);}for (int i = 1; i <= n; ++i) {cout << ans[i] << " \n"[i == n];}
}