D. Range = √Sum
D. Range = √Sum
You are given an integer $n$. Find a sequence of $n$ distinct integers $a_1, a_2, \dots, a_n$ such that $1 \leq a_i \leq 10^9$ for all $i$ and $$\max(a_1, a_2, \dots, a_n) - \min(a_1, a_2, \dots, a_n)= \sqrt{a_1 + a_2 + \dots + a_n}.$$
It can be proven that there exists a sequence of distinct integers that satisfies all the conditions above.
Input
The first line of input contains $t$ ($1 \leq t \leq 10^4$) — the number of test cases.
The first and only line of each test case contains one integer $n$ ($2 \leq n \leq 3 \cdot 10^5$) — the length of the sequence you have to find.
The sum of $n$ over all test cases does not exceed $3 \cdot 10^5$.
Output
For each test case, output $n$ space-separated distinct integers $a_1, a_2, \dots, a_n$ satisfying the conditions in the statement.
If there are several possible answers, you can output any of them. Please remember that your integers must be distinct!
Example
input
3 2 5 4
output
3 1 20 29 18 26 28 25 21 23 31
Note
In the first test case, the maximum is $3$, the minimum is $1$, the sum is $4$, and $3 - 1 = \sqrt{4}$.
In the second test case, the maximum is $29$, the minimum is $18$, the sum is $121$, and $29-18 = \sqrt{121}$.
For each test case, the integers are all distinct.
解题思路
构造题感觉更多是看直觉和灵感的,这里就直接按照题解的思路来写了。
对于$n$是奇数的情况,我们可以考虑以$n$为中心往两边构造出序列$$\left\{ {n - \left\lfloor {\frac{n}{2}} \right\rfloor,\, n - \left\lfloor {\frac{n}{2}} \right\rfloor - 1,\, \ldots \,,\, n-1,\, n,\, n+1,\, \ldots \,,\, n + \left\lfloor {\frac{n}{2}} \right\rfloor - 1,\, n + \left\lfloor {\frac{n}{2}} \right\rfloor} \right\}$$
此时序列的$\max - \min = n - 1$,总和为(两端两两相加)$\left\lfloor {\frac{n}{2}} \right\rfloor \times 2n + n = \frac{n-1}{2} \times 2n + n = n^2$。此时如果给序列中的每个数都加上$2$,那么$\max - \min = n - 1$不变,总和变成了${\frac{n-1}{2}} \times 2(n+2) + (n+2) = n^2 + 2n$。现在我们对序列中的最小值减去$1$,最大值加上$1$,此时$\max - \min = n + 1$,总和依然是$n^2 + 2n$。
为了使得$(n+1)^2 = n^2 + 2n + 1$,我们再对序列中第二大的数加上$1$就可以了(由于上一步已经让最大数加$1$了,因此对第二大数加$1$得到的结果是合法的)。
例如当$n = 5$:
- $[3, 4, 5, 6, 7]$(以$5$为中心构造)
- $[5, 6, 7, 8, 9]$(所有元素加$2$)
- $[4, 6, 7, 8, 10]$(最小值减$1$,最大值加$1$)
- $[4, 6, 7, 9, 10]$(第二大值加$1$)
对于$n$是偶数的情况,同样考虑以$n$为中心(不包含$n$)往两边构造出序列$$\left\{ {n - \frac{n}{2},\, n - \frac{n}{2} - 1,\, \ldots \,,\, n-1,\, n+1,\, \ldots \,,\, n + {\frac{n}{2}} - 1,\, n + {\frac{n}{2}} } \right\}$$
此时序列的$\max - \min = n$,总和为$\frac{n}{2} \times 2n = n^2$。
AC代码如下:
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 const int N = 3e5 + 10; 5 6 int a[N]; 7 8 void solve() { 9 int n; 10 scanf("%d", &n); 11 if (n & 1) { 12 for (int i = 0, j = n - n / 2; i < n; i++) { 13 a[i] = j++ + 2; 14 } 15 a[0]--, a[n - 1]++, a[n - 2]++; 16 } 17 else { 18 for (int i = 0, j = n - n / 2; i < n; i++) { 19 a[i] = j++; 20 if (j == n) j++; 21 } 22 } 23 for (int i = 0; i < n; i++) { 24 printf("%d ", a[i]); 25 } 26 printf("\n"); 27 } 28 29 int main() { 30 int t; 31 scanf("%d", &t); 32 while (t--) { 33 solve(); 34 } 35 36 return 0; 37 }
cf的构造题还是经常无从下手,寒假需要多做这样的题进行训练。现在也准备期末了,没时间打cf和写题解了,寒假再开始吧(我真的好菜)。・゚゚*(>д<)*゚゚・。
参考资料
Codeforces Round #836 (Div. 2) Editorial:https://codeforces.com/blog/entry/109438
本文来自博客园,作者:onlyblues,转载请注明原文链接:https://www.cnblogs.com/onlyblues/p/16933593.html