git reimport
This commit is contained in:
45
archive/2018.02/2018.02.25 - unsorted/B.java
Normal file
45
archive/2018.02/2018.02.25 - unsorted/B.java
Normal file
@@ -0,0 +1,45 @@
|
||||
package chelper;
|
||||
|
||||
import io.InputReader;
|
||||
import io.OutputWriter;
|
||||
import misc.SimpleSavingChelperSolution;
|
||||
|
||||
|
||||
public class B extends SimpleSavingChelperSolution {
|
||||
|
||||
public void solve(int testNumber, InputReader in, OutputWriter out) {
|
||||
wrapSolve(testNumber, in, out);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void solve(int testNumber) {
|
||||
int n = in.nextInt();
|
||||
int k = in.nextInt();
|
||||
|
||||
int[] link = new int[n];
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
link[i] = in.nextInt() - 1;
|
||||
}
|
||||
|
||||
int[] dp = new int[n];
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
int l = Math.max(0, i - k);
|
||||
int r = Math.min(i + k, n - 1);
|
||||
|
||||
dp[i] = r - l + 1;
|
||||
|
||||
if (link[i] != -1) {
|
||||
int ll = Math.max(0, link[i] - k);
|
||||
int rr = Math.min(link[i] + k, n - 1);
|
||||
|
||||
dp[i] += dp[link[i]] - Math.max(0, rr - l + 1);
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
out.print(dp[i] + " ");
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user