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

186 篇文章 0 订阅

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

设定一个数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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值