Codeforces 622D Optimal Number Permutation【贪心+构造】

来源:互联网 发布:虫虫群发软件 编辑:程序博客网 时间:2024/06/07 01:15

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 from1 to n twice. You can arbitrary permute any numbers ina.

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 numberi. 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每个数各两次。

设定一个数i出现的两个位子为xi,yi(xi<yi),那么对应di=yi-xi.

现在希望.尽可能的小,让你构造出一个序列。


思路:


1、现在我们希望整体最优,因为每个部分都是独立不相互影响的,那么我们考虑局部最优可以导致整体最优。

那么对于第i个数来讲,(n-i)是不必考虑的,因为其值是固定的,我们考虑|di+i-n|尽可能的小,我们又知道,绝对值的最小值就是0.那么我们希望di=n-i..

而又有当i==n的时候(n-i)已经是0.那么我们问题转换变成:

对于1~n这些数来讲,我们希望数字i两个位子的差距为n-i那么长。


2、接下来问题就变成了构造,手写两组数组我们不难想到:

将数组分成两个长度为N的部分,i%2==1的部分放在左侧,i%2==0的部分放在右侧,然后贪心的将位子放置好即可。

具体细节参考代码。


Ac代码:

#include<stdio.h>#include<string.h>#include<queue>using namespace std;int ans[1080000];int main(){    int n;    while(~scanf("%d",&n))    {        memset(ans,-1,sizeof(ans));        int nex=-1;        int cnt=1;        for(int i=1;i<=n-1;i++)        {            int want=n-i;            if(i%2==1)            {                ans[cnt]=i;                ans[cnt+want]=i;            }            else            {                ans[n+cnt]=i;                ans[n+cnt+want]=i;                cnt++;            }        }        for(int i=1;i<=2*n;i++)        {            if(ans[i]==-1)printf("%d ",n);            else printf("%d ",ans[i]);        }        printf("\n");    }}










0 0