codeforces622D Optimal Number Permutation

来源:互联网 发布:网络与生活作文2000字 编辑:程序博客网 时间:2024/05/24 06:46

原题链接:http://codeforces.com/problemset/problem/622/D



D. Optimal Number Permutation
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You have array a that contains all integers from 1 to n twice. You can arbitrary permute any numbers in a.

Let number i be in positions xi, yi (xi < yi) in the permuted array a. Let's define the value di = yi - xi — the distance between the positions of the number i. Permute the numbers in array a to minimize the value of the sum .

Input

The only line contains integer n (1 ≤ n ≤ 5·105).

Output

Print 2n integers — the permuted array a that minimizes the value of the sum s.

Examples
input
2
output
1 1 2 2
input
1
output
1 1



题意:

长度为2*N的序列,包含1~N的数字各两个,要求找到一个排列,使得最小。。


思路:

只有di可变,所以使di+i-n的绝对值最小(di表示相同两数之间的距离),那么di=n-i;

所以1之间需要隔n-2个数字,2之间需要n-3个数字,3之间需要n-4个数字;

那么当1放完之后,其之间的n-2个空间(首尾相隔n-4)可以放3,以此类推;

所以我们可以把奇数和偶数分开放。



代码:

#include <cstdio>#include <cstring>int a[500005*2];int main(){int i, j, x, y,n;while (~scanf("%d", &n)){memset(a, 0, sizeof(a));if (n == 1){printf("1 1\n");continue;}x = 1;y = n;a[x] = a[y] = 1;i = 3;x++;y--;while (x < y){a[x] = a[y] = i;x++;y--;i += 2;}x = n+1;y = 2 * n - 1;a[x] = a[y] = 2;i = 4;x++;y--;while (x < y){a[x] = a[y] = i;x++;y--;i += 2;}for (i = 1; i <= 2 * n; i++){if (a[i] == 0)printf(i == 1 ? "%d" : " %d", n);elseprintf(i == 1 ? "%d" : " %d", a[i]);}printf("\n");}return 0;}


原创粉丝点击