#P9547. [湖北省选模拟 2023] 路环群山 / mountain
[湖北省选模拟 2023] 路环群山 / mountain
Problem Description
In a 2D world there is a mountainous terrain. The mountain can be described by a function , meaning that at horizontal coordinate the altitude is . In this world there are sheep: goats and sheep. We know that the horizontal coordinate of the -th goat is , and the horizontal coordinate of the -th sheep is , but we do not know their altitudes. However, we know that the goats’ positions are concentrated in a higher altitude range, while the sheep’s positions are concentrated in a lower altitude range. You need to guess the mountain shape from the distributions of goats and sheep, making both the variance of the goats’ altitudes and the variance of the sheep’s altitudes as small as possible, and at the same time making the goats’ altitudes as high as possible compared with the sheep’s altitudes.
Formally, let
be the average altitude of goats and sheep respectively. Your goal is to construct a function to minimize the cost
$$\operatorname{cost}(f)=\frac{1}{\bar{u}-\bar{v}}\sqrt{\left[\sum_{i=1}^n (f(p_i)-\bar{u})^2\right]+\left[\sum_{j=1}^m (f(q_j)-\bar{v})^2\right]}$$Of course, you also need to ensure .
For convenience, you must describe using a Fourier series. That is, given , you need to find the optimal function of the form , and output as the answer. Please ensure . The testdata guarantees that there exists an optimal solution satisfying the above limits.
This problem uses Special Judge. Given tolerance . Your answer is considered correct when the function you output and the function in the official answer satisfy $\operatorname{cost}(f)<\max(\epsilon+\operatorname{cost}(f^*),(1+\epsilon)\operatorname{cost}(f^*))$.
Constraints
For all testdata, it is guaranteed that , , , .
It is guaranteed that in each testdata, under the condition that and are within the data range of this test point and the problem is solvable, they are generated uniformly at random.

Input Format
The input has three lines.
The first line contains four integers .
The second line contains integers, where the -th number is .
The third line contains integers, where the -th number is .
Output Format
Output lines, each containing two floating-point numbers .
3 2 1 0
‐10838702 0 10838702
‐1 1
1 0
4 4 2 0
1 3 5 7
2 4 6 8
0.6648289523 ‐0.1433645347
0.6172866488 1.3647253547
见选手目录下的 mountain/mountain3.in 与 mountain/mountain3.ans。
见选手目录下的 mountain/mountain3.in 与 mountain/mountain3.ans。
Hint
Explanation for Sample 1
Observe that , and . So when , almost all goats are at the same altitude, almost all sheep are at the same altitude, and the goats’ positions are higher than the sheep’s positions. In this case , achieving the optimal solution.
Note that for any non-zero number , the function can also be considered an optimal solution.
Explanation for Sample 2
One of the optimal functions is approximately $f(x)=0.6648289523\cos(x)-0.1433645347\sin(x)+0.6172866488\cos(2x)+1.3647253547\sin(2x)$, and its cost is approximately .
Subtasks
For all testdata, it is guaranteed that , , , .
It is guaranteed that in each testdata, under the condition that and are within the data range of this test point and the problem is solvable, they are generated uniformly at random.
Translated by ChatGPT 5