Cho N trạm trên đường thẳng, mỗi trạm có công suất bức xạ R. Trạm i tăng bức xạ cho trạm lân cận: (i-1) tăng R-1, (i-2) tăng R-2, ...; (i+1) tăng R-1, (i+2) tăng R-2, ... cho đến khi hiệu ứng ≤ 0. Ví dụ: [1,2,3] → [3,4,4].
Duyệt từng trạm, lan truyền hiệu ứng đến hai bên cho đến khi giảm về 0.
Ví dụ
#include <iostream>
#include <vector>
using namespace std;
vector<int> finalRadiation(vector<int> stations) {
int n = stations.size();
vector<int> result = stations;
for (int i = 0; i < n; i++) {
int R = stations[i];
for (int d = 1; d < R; d++) {
if (i - d >= 0) result[i - d] += R - d;
if (i + d < n) result[i + d] += R - d;
}
}
return result;
}
int main() {
vector<int> stations = {1, 2, 3};
vector<int> res = finalRadiation(stations);
for (int v : res) cout << v << " ";
}
Kết quả
3 4 4