D. Range = √Sum
D. Range = √Sum
You are given an integer . Find a sequence of distinct integers such that for all and
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 () — the number of test cases.
The first and only line of each test case contains one integer () — the length of the sequence you have to find.
The sum of over all test cases does not exceed .
Output
For each test case, output space-separated distinct integers 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 , the minimum is , the sum is , and .
In the second test case, the maximum is , the minimum is , the sum is , and .
For each test case, the integers are all distinct.
解题思路
构造题感觉更多是看直觉和灵感的,这里就直接按照题解的思路来写了。
对于是奇数的情况,我们可以考虑以为中心往两边构造出序列
此时序列的,总和为(两端两两相加)。此时如果给序列中的每个数都加上,那么不变,总和变成了。现在我们对序列中的最小值减去,最大值加上,此时,总和依然是。
为了使得,我们再对序列中第二大的数加上就可以了(由于上一步已经让最大数加了,因此对第二大数加得到的结果是合法的)。
例如当:
- (以为中心构造)
- (所有元素加)
- (最小值减,最大值加)
- (第二大值加)
对于是偶数的情况,同样考虑以为中心(不包含)往两边构造出序列
此时序列的,总和为。
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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效