Farmer John's cows have decided to offer a programming contest for the cows on Farmer Nhoj's farm. In order to make the problems as fun as possible, they have spent considerable time coming up with challenging input cases. For one problem in particular, "Haybales", the cows need your help devising challenging inputs. This involve solving the following somewhat intriguing problem:
There is an array of sorted integers?x1≤x2≤?≤xNx1≤x2≤?≤xN?(1≤N≤1051≤N≤105), and an integer?KK. You don't know the array or?KK, but you do know for each index?ii, the largest index?jiji?such that?xji≤xi+Kxji≤xi+K. It is guaranteed that?i≤jii≤ji?and?j1≤j2≤?≤jN≤Nj1≤j2≤?≤jN≤N.
Given this information, Farmer John's cows need to construct any array along with some integer?KK?that matches that information. The construction needs to satisfy?0≤xi≤10180≤xi≤1018?for all?ii?and?1≤K≤10181≤K≤1018.
It can be proven that this is always possible. Help Farmer John's cows solve this problem!
The first line of input contains?NN. The next line contains?j1,j2,…,jNj1,j2,…,jN.
Print?KK, then?x1,…,xNx1,…,xN?on separate lines. Any valid output will be accepted.
6 2 2 4 5 6 6
6 1 6 17 22 27 32
The sample output is the array?a=[1,6,17,22,27,32]a=[1,6,17,22,27,32]?with?K=6K=6.?j1=2j1=2?is satisfied because?a2=6≤1+6=a1+Ka2=6≤1+6=a1+K?but?a3=17>1+6=a1+Ka3=17>1+6=a1+K, so?a2a2?is the largest element that is at most?a1a1. Similarly,
This is not the only possible correct output for the sample input. For example, you could instead output the array?[1,2,4,5,6,7][1,2,4,5,6,7]?with?K=1K=1.
Problem credits: Danny Mittal
[/hide]
(Analysis by Danny Mittal)
Consider creating a tree on the indexes of the array as follows. For an index?ii, let its parent be?ji+1ji+1. Note that this means that we'll also need a node?N+1N+1, since?jN+1=N+1jN+1=N+1; this node will be the root of the tree.
Let's define the height of a node as the depth of the deepest node minus its own depth. This tree construction is illustrated below for the sample with heights on the left.
3 7
/|
2 5 6
| |
1 3 4
/|
0 1 2
Notice that as we go down the array from index?NN?to index?11, the heights of the corresponding nodes decrease. This means that the array consists of first the nodes at height?00, then the nodes at height?11, etc. Additionally, since each node?ii's parent is?ji+1ji+1, which is the first index whose element?aji+1aji+1?is greater than?ai+Kai+K, and a node's parent necessarily has height?11?higher than the height of that node, the nodes at a certain height need to have values that are approximately?KK?higher than the values of the nodes at the immediately lower height.
This suggests that we assign the values of the nodes as?ai=hiK+xiai=hiK+xi, where?hihi?is the height of node?ii?and?xixi?is some yet to be determined value between?00?and?K?1K?1. Using these values means that?ai+K=(hi+1)K+xiai+K=(hi+1)K+xi, which means that the point at which values go from being less than?ai+Kai+K?to more than?ai+Kai+K?occurs at height?hi+1hi+1, which is exactly what we want since?ii's parent,?ji+1ji+1, is at height?hi+1hi+1.
All that remains is to choose values of?xixi?appropriately so that that point occurs at exactly the right place inside height?hi+1hi+1. This means choosing the?xx?values so that?xji+1>xixji+1>xi, but any smaller indexes at height?hi+1hi+1?have an?xx?value less than or equal to?xixi.
We can do this using DFS. We start by assigning the root node?N+1N+1?to have?xN+1=K?1xN+1=K?1, the largest?xx?value possible. Then, it DFSs through its children starting from the largest index and going down. Whenever we reach a node, we assign it the next lower?xx?value, then DFS through its children in decreasing order. This guarantees that every node?ii?has an?xx?value larger than?ii's children, but all smaller indexes at the same height as?ii?get an?xx?value smaller than?ii's children, since the DFS reaches them only after searching through all of?ii's descendants.
Since each node will get a unique?xx?value, we need to set?K=N+1K=N+1?in order to ensure that all?xx?values are between?00?and?K?1K?1. The?xx?values for the sample are illustrated below.
7
/---/
5 6
/ /
3 4
/-/
1 2
x = 0 1 2 3 4 5 6
As an example of how this construction works, consider index?44, which has?j4=5j4=5. Its height is?11?and its?xx?value is?x4=4x4=4, which means that its array value will be?a4=(1)K+4=(1)(7)+4a4=(1)K+4=(1)(7)+4, and?a4+Ka4+K?will be?(2)(7)+4(2)(7)+4.
Index?55?is at height?22?but has the smaller?xx?value?x5=3x5=3, so its array value will be?a5=(2)(7)+3a5=(2)(7)+3, which is smaller than?a4+K=(2)(7)+4a4+K=(2)(7)+4. Similarly, index?66?is at height?22?and has the larger?xx?value?x6=5x6=5, so its array value will be?a6=(2)(7)+5a6=(2)(7)+5, which is larger than?a4+K=(2)(7)+4a4+K=(2)(7)+4. Therefore index?55?is the largest index?jj?satisfying?aj≤a4+Kaj≤a4+K, and so the requirement?j4=5j4=5?is satisfied.
Using these?xx?values, we can calculate our array?aa?along with the choice of?K=N+1K=N+1?to produce a valid construction. Our algorithm simply consists of constructing the tree then performing a DFS to compute the?xx?values as well as the heights. This runs in?O(N)O(N)?time.
The array values for all indexes in the sample are illustrated below.
(3)(7) + 6
/----------------------/
(2)(7) + 3 (2)(7) + 5
/ /
(1)(7) + 2 (1)(7) + 4
/------------/
(0)(7) + 0 (0)(7) + 1
Code:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class TestsForHaybales {
static List<Integer>[] children;
static long[] depths;
static long[] xs;
static long x;
static void dfs(int a) {
xs[a] = x;
x--;
Collections.reverse(children[a]);
for (int b : children[a]) {
depths[b] = depths[a] + 1L;
dfs(b);
}
}
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(in.readLine());
children = new List[n + 2];
for (int a = 1; a <= n + 1; a++) {
children[a] = new ArrayList<>();
}
StringTokenizer tokenizer = new StringTokenizer(in.readLine());
for (int a = 1; a <= n; a++) {
children[Integer.parseInt(tokenizer.nextToken()) + 1].add(a);
}
depths = new long[n + 2];
xs = new long[n + 2];
x = n;
dfs(n + 1);
long k = n + 1;
StringJoiner joiner = new StringJoiner("\n");
for (int a = 1; a <= n; a++) {
long height = depths[1] - depths[a];
long value = (height * k) + xs[a];
joiner.add("" + value);
}
System.out.println(k);
System.out.println(joiner);
}
}
[/hide]

? 2025. All Rights Reserved. 滬ICP備2023009024號-1