Optimal Number Permutation CodeForces - 622D (找规律)

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.

Example
Input
2
Output
1 1 2 2
Input
1
Output
1 1

找规律;
1:1 1
2 : 1 1 2 2
3 : 1 3 1 2 2 3
4 : 1 3 3 1 2 4 2 4
….

#include

int a[1000100];

int main()
{
    int n;
    scanf("%d",&n);
    a[n*2]=n;

    int end = n*2;
    int tmp = n/2;
    int i,j;

    a[1]=1;
    for(i=2;i<=tmp;i++)
        a[i] = a[i-1]+2;

    if(n%2 && n!=1) a[i] = a[i-1]+2;

    j=tmp+1+n%2;
    if(n%2==0)
        {
            a[j]=a[j-1];
            for(j++; j<=n; j++)
                a[j]=a[j-1]-2;
        }
    else 
        {
            for( j=tmp+1+n%2; j<=n; j++)
                a[j]=a[j-1]-2;
        }


    a[n+1]=2;
    for(i=n+2;i<=n+tmp;i++)
        a[i] = a[i-1]+2;

    j=n+tmp+1;
    if(n%2==0)
        {
            for( ;j<=end-1;j++)
                a[j]=a[j-1]-2;
        }
    else 
        {
            a[j]=a[j-1];
            for(j+=1;j<=end-1;j++)
                a[j]=a[j-1]-2;
        }




    for(i=1;i<=end;i++)
        printf("%d%c",a[i],i==end?'\n':' ');

    return 0;
}

你可能感兴趣的:(思维,技巧)