1754B.Kevin and Permutation

原题地址

For his birthday, Kevin received the set of pairwise distinct numbers 1,2,3,…,n as a gift.

He is going to arrange these numbers in a way such that the minimum absolute difference between two consecutive numbers be maximum possible. More formally, if he arranges numbers in order p1,p2,…,pn, he wants to maximize the value
mini=1n−1|pi+1−pi|,
where |x| denotes the absolute value of x.

Help Kevin to do that.

Input
Each test consists of multiple test cases. The first line contains a single integer t (1≤t≤100) — the number of test cases. Description of the test cases follows.

The only line of each test case contains an integer n (2≤n≤1000) — the size of the set.

Output
For each test case print a single line containing n distinct integers p1,p2,…,pn (1≤pi≤n) describing the arrangement that maximizes the minimum absolute difference of consecutive elements.

Formally, you have to print a permutation p which maximizes the value mini=1n−1|pi+1−pi|.

If there are multiple optimal solutions, print any of them.

Example
inputCopy
2
4
3
outputCopy
2 4 1 3
1 2 3
Note
In the first test case the minimum absolute difference of consecutive elements equals min{|4−2|,|1−4|,|3−1|}=min{2,3,2}=2. It’s easy to prove that this answer is optimal.

In the second test case each permutation of numbers 1,2,3 is an optimal answer. The minimum absolute difference of consecutive elements equals to 1.

#include<bits/stdc++.h>
using namespace std;
int main(void) {int t;cin >> t;while (t--) {int n;cin >> n;int a[1005] = { 0 };int j = 0, cnt = 1, flag = 0;if (n % 2 != 0) n--, flag = 1;while (j < n) {int i;for (i = cnt + n / 2; i >= 1; i -= n / 2) {a[j] = i;j++;}cnt++;if (i < 1)i += n / 2;}for (int i = 0; i < j; i++) {cout << a[i] << ' ';}if (flag)cout << n + 1;cout << endl;}
}
//code by yxisme;
//code by 01100_10111;

Codeforces 1754B.Kevin and Permutation相关推荐

  1. codeforces 7.22 E Permutation Shift

    codeforces 7.22 E Permutation Shift 给出一个1到n的排列,每次可以交换两个数,问在交换最多m次(m <= n/3)之后能不能得到由1 2 3 - n循环右移所 ...

  2. CodeForce1754——B. Kevin and Permutation

    CodeForce1754--B. Kevin and Permutation 题目描述 For his birthday, Kevin received the set of pairwise di ...

  3. 【CodeForces - 124C】Prime Permutation(数学,思维,小结论)

    题干: You are given a string s, consisting of small Latin letters. Let's denote the length of the stri ...

  4. 【CodeForces - 483C】Diverse Permutation(思维构造)

    题干: Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of ndistinct posit ...

  5. B. Kevin and Permutation codeforces1754B

    Problem - B - Codeforces 题目大意:求一个n的排列使得差分的最小值最小 1<=n<=1000 思路:因为n个数每两个相邻的数都要求差,所以差分的最小值就是n/2,那 ...

  6. CodeForces 361B Levko and Permutation

    题意:有n个数,这些数的范围是[1,n],并且每个数都是不相同的.你需要构造一个排列,使得这个排列上的数与它所在位置的序号的最大公约数满足 > 1,并且这些数的个数恰好满足k个,输出这样的一个排 ...

  7. CodeForces - 351E Jeff and Permutation(贪心)

    题目链接:点击查看 题目大意:给出一个长度为 n 的序列,可以选择某些位置使得 a[ i ] =  - a[ i ],问逆序对最少可以是多少 题目分析:需要看出的一个关系就是,在某一对关系 ( i , ...

  8. CodeForces - 1343F Restore the Permutation by Sorted Segments(思维)

    题目链接:点击查看 题目大意:现在有一个长度为 n 的排列 p ,但排列 p 暂时对我们保密,每个样例将会给出 n - 1 个排好序后的子段,换句话说,对于 r ∈ [ 2 , n ] ,存在一个 l ...

  9. CodeForces - 618B Guess the Permutation(思维+构造)

    题目链接:点击查看 题目大意:先给出一个长度为n的序列ai,这个序列是1~n全排列中的其中一种,再给出一个n*n的矩阵,maze[i][j]=val代表min(ai,aj)=val,要求我们构造出原始 ...

最新文章

  1. MATLAB【二】————图像做减法,批量文本处理,子图显示
  2. Python编程基础:第四节 类型转换Type Cast
  3. Dota2 Pro Circuit 贪心-模拟
  4. 【若依(ruoyi)】Swagger 上传接口
  5. 程序员的进化(目前还看不懂)
  6. git命令:修改已经commit的注释
  7. 再延期!曝华为P50六月发布
  8. spark学习-32-SparkEnv的构造步骤
  9. Linux Kernel 5.13 稳定版发布:初步支持 M1 芯片
  10. sessionFactory.getCurrentSession()的引出
  11. SVN 错误中文对照
  12. Linux ALSA声卡驱动
  13. 分屏 投影显示 PPT
  14. 638-字符串模式匹配-KMP算法
  15. win10电脑外接音响没声音怎么回事?win10电脑外接音响没声音的修复方法
  16. ARM汇编 BIC和ORR指令
  17. 德州大学达拉斯分校计算机专业博士,德克萨斯大学达拉斯分校计算机科学理科硕士入学条件及实习就业...
  18. 文件已经删除,但是空间没有释放的异常
  19. led灯什么牌子的质量好又耐用?2022性价比高的led护眼灯品牌推荐
  20. linux学习课程从入门到精通:Centos8-系统进程管理

热门文章

  1. 智能翻译机python实现详细解释
  2. 对C#中Thread.IsBackground属性的理解
  3. 差分进化算法和遗传算法的区别
  4. 消费新品周报 | 老板推出新款消毒柜;A.O.史密斯橱下冷热即饮净水机上市
  5. 05-20210301在WIN10下通过串口给华为海思Hi3516DV300刷机(鸿蒙系统)
  6. Coursera 的视频打不开
  7. ListView控件的使用方法,以及数据的绑定(关于会员等级)
  8. CSS中常用的clearfix方法
  9. 2013年搜索引擎盘点,爱恨交加
  10. Python应用 | Python实现汉译英