Codeforces edu 7 D. Optimal Number Permutation 构造

题目

题目链接:http://codeforces.com/contest/622/problem/D

题目来源:Educational Codeforces Round 7

简要题意:令一个和式取到最小。

题解

首先可以暴力一下,可以发现都能达到 0 <script type="math/tex" id="MathJax-Element-1">0</script>为结果。

然后可以发现这样每个数所需的距离是固定的。

将数按照奇数和偶数放到两段中,就能构造出来,其中最后一个不放入,最后塞到空位中。

很容易发现这样一定能够构造出一个解来,挺不错的构造想法题。

代码

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>

#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
LL powmod(LL a,LL b, LL MOD) {LL res=1;a%=MOD;for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;}
// head
const int N = 5E5+5;

int a[N*2];
int main()
{
    int n;
    while (scanf("%d", &n) == 1) {
        int l = 1, r = n;
        for (int i = 1; i < n; i += 2) {
            a[l++] = a[r--] = i;
        }
        l = n+1, r = 2*n-1;
        for (int i = 2; i < n; i += 2) {
            a[l++] = a[r--] = i;
        }
        int lim = 2*n;
        for (int i = 1; i <= lim; i++) {
            if (!a[i]) a[i] = n;
            printf("%d%c", a[i], i==lim ? '\n' : ' ');
        }
        memset(a, 0, sizeof a);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值