0%

HDU 6235 Permutation 题解

HDU 6235 Permutation 题解

题目大意

输出一个 \(n\) 的排列,要求 \(p_i\equiv 0\,(\text{mod } |p_i-p_{i-2}|)\),对 \(i=3,4,\cdots\) 成立。

题解

我们发现,如果 \(|p_i - p_{i-2}|\) 如果可以为 \(1\),即隔一个的位置差的绝对值为 \(1\),则一定满足条件。我们从 \(1\) 开始依次逐个递增一个向后放,从 \(n\) 开始递减向后放,就可以满足上面的要求了。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// https://acm.hdu.edu.cn/showproblem.php?pid=6235
#include <cstdio>
#include <cctype>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cassert>
#include <cmath>
using namespace std;
typedef long long ll;

template <typename _Tp>
void read(_Tp &a, char c = 0, int f = 1) {
for(c = getchar(); !isdigit(c); c = getchar()) if(c == '-') f = -1;
for(a = 0; isdigit(c); a = a * 10 + c - '0', c = getchar()); a *= f;
}

template <typename _Tp>
void write(_Tp a) {
if(a < 0) putchar('-'), a = -a;
if(a > 9) write(a / 10); putchar(a % 10 + '0');
}

const int N = 1e5 + 5;

int a[N];

int main() {
// freopen("0722_2.in", "r", stdin);
int T;
read(T);
while(T--) {
int n;
read(n);
int p = 1, q = n;
for(int i = 1; i <= n; i += 2) a[i] = p++;
for(int i = 2; i <= n; i += 2) a[i] = q--;
for(int i = 1; i <= n; i++) write(a[i]), putchar(' ');
putchar('\n');
}
return 0;
}