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. 一篇文章告诉你标准化和归一化的区别?
  2. tomcat限速_WEB服务的下载限速(二)(限速模块安装与配置)
  3. php 去掉img,php怎样去掉img标签
  4. 2016 KidCraft冬令营 一起来玩Scratch + MineCraft
  5. python 中文识别 不用tesseract_Python——验证码识别 Pillow + tesseract-ocr
  6. 学以致用十-----centos7.2+python3.6+vim8.1+YouCompleteMe
  7. 开源个性化内容推荐引擎_您是哪种开源个性类型?
  8. Git命令行介绍和使用说明(持续更新)
  9. 2021-06-07java继承
  10. Atitit.实现继承的原理and方法java javascript .net c# php ...
  11. 人脸图像数据库(完整版)
  12. 解决OneNote同步出错
  13. 前端js获取文件名、后缀,截取文件名后缀
  14. 史上最全无线通信频率分配表
  15. win10截图软件工具
  16. matlab点集配准,matlab练习程序(对应点集配准的四元数法)
  17. 官宣!等你来赛,2022 第二届 STM32 Hackathon 挑战赛海选开放报名!
  18. 相位解包裹(五)枝切法(Goldstein’s branch cut algorithm)
  19. Yii框架中setFlash和getFlash函数的用法
  20. 研制过程评审活动(二)方案阶段

热门文章

  1. BERT融合知识图谱之模型及代码浅析
  2. git克隆代码报错remote: HTTP Basic: Access denied fatal: Authentication failed for的解决方案之一
  3. iTools(pro)下载
  4. Python 弹跳小球
  5. 模糊算法的工作原理,模糊算法和神经网络
  6. android 开发实例 底部导航栏(1)
  7. 齐向东:数字贸易没有“数字海关” 经营者要“自证清白”
  8. HTML5里的placeholder属性
  9. python程序代码是什么_python代码用什么写
  10. 苹果公司正在开发一种专用的无损音频格式,使苹果 AirPods 可以播放这种无损音频