首页 > 其他分享 >E. Klever Permutation

E. Klever Permutation

时间:2024-02-10 12:11:15浏览次数:26  
标签:10 le level Klever Permutation length permutation test

E. Klever Permutation

You are given two integers $n$ and $k$ ($k \le n$), where $k$ is even.

A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in any order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation (as $2$ appears twice in the array) and $[0,1,2]$ is also not a permutation (as $n=3$, but $3$ is not present in the array).

Your task is to construct a $k$-level permutation of length $n$.

A permutation is called $k$-level if, among all the sums of continuous segments of length $k$ (of which there are exactly $n - k + 1$), any two sums differ by no more than $1$.

More formally, to determine if the permutation $p$ is $k$-level, first construct an array $s$ of length $n - k + 1$, where $s_i=\sum_{j=i}^{i+k-1} p_j$, i.e., the $i$-th element is equal to the sum of $p_i, p_{i+1}, \dots, p_{i+k-1}$.

A permutation is called $k$-level if $\max(s) - \min(s) \le 1$.

Find any $k$-level permutation of length $n$.

Input

The first line of the input contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. This is followed by the description of the test cases.

The first and only line of each test case contains two integers $n$ and $k$ ($2 \le k \le n \le 2 \cdot 10^5$, $k$ is even), where $n$ is the length of the desired permutation.

It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$.

Output

For each test case, output any $k$-level permutation of length $n$.

It is guaranteed that such a permutation always exists given the constraints.

Example

input

5
2 2
3 2
10 4
13 4
7 4

output

2 1
1 3 2
1 8 4 10 2 7 5 9 3 6
4 10 1 13 5 9 2 12 6 8 3 11 7
1 6 3 7 2 5 4

Note

In the second test case of the example:

$p_1 + p_2 = 3 + 1 = 4$;

$p_2 + p_3 = 1 + 2 = 3$.

The maximum among the sums is $4$, and the minimum is $3$.

 

解题思路

  构造题直接投降,卡到比赛结束都没想出来,鉴定为弱智.jpg。

  首先相邻的 $s_i$ 和 $s_{i+1}$ 必然不同,这是因为 $s_i - s_{i+1} = a_{i} - a_{i+k}$,由于 $a$ 是排列因此必然有 $a_{i} \ne a_{i+k}$,所以 $s_i$ 和 $s_{i+1}$ 至少相差 $1$。由于还要保证所有的 $s_i$ 中最大值与最小值的差不超过 $1$,假设 $s_1 = x$,那么 $s$ 的形式必然是 $[x,x+1,x,x+1,x,\ldots]$ 或者 $[x,x-1,x,x-1,x,\ldots]$。

  假设构造的 $s$ 是这样的 $[x,x+1,x,x+1,x,\ldots]$,那么根据 $s_1 - s_2 = -1$,有 $a_1 = a_{1+k} + 1$。同理可得 $a_2 = a_{2+k} - 1$,$a_3 = a_{3+k} + 1$,$a_4 = a_{4+k} - 1$,以此类推。发现如果是奇数下标,那么就有 $a_i = a_{i+k}+1$,否则是偶数下标就有 $a_i = a_{i+k}+1$。

  构造 $a$ 的方法是把 $a$ 分成 $k$ 组,由于题目保证 $k$ 是偶数,因此同一组中元素的下标 $i, \, i+k, \, i+2k, \, \ldots$ 的奇偶性是相同的。所以可以开两个变量 $l=1$,$r=n$,对于奇数的组从左到右遍历组中每个元素,赋值为 $l$,然后令 $l \gets l+1$。同理对于偶数的组从左到右遍历组中每个元素,赋值为 $r$,然后令 $r \gets r-1$。

  AC 代码如下,时间复杂度为 $O(n)$:

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

const int N = 2e5 + 10;

int ans[N];

void solve() {
    int n, m;
    scanf("%d %d", &n, &m);
    for (int i = 1, l = 1, r = n; i <= m; i++) {
        for (int j = i; j <= n; j += m) {
            if (i & 1) ans[j] = l++;
            else ans[j] = r--;
        }
    }
    for (int i = 1; i <= n; i++) {
        printf("%d ", ans[i]);
    }
    printf("\n");
}

int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        solve();
    }
    
    return 0;
}

 

参考资料

  Codeforces Round 923 (Div. 3) Editorial:https://codeforces.com/blog/entry/125597

标签:10,le,level,Klever,Permutation,length,permutation,test
From: https://www.cnblogs.com/onlyblues/p/18012779

相关文章

  • E. Klever Permutation
    题解假设a1a2a3...ak ak+1ak+2...an是符合要求的数组,那么我们可以推断出:a(k+1)=a(1)+1;a(k+2)=a(2)-1;...a(2k+1)=a(k+1)+1;...因此我们知晓奇数位的数要比较小,偶数的位置要比较大;又题目说明一定有解,所以我们假定a1=1,a2=n再递推出其余各项。Code#include<b......
  • CF1861E Non-Intersecting Subpermutations 题解
    简要题意一个长度为\(n\)的元素在\([1,k]\)的整数序列\(a\)的价值定义如下。初始\(i=1\),如果\(a_{i\simi+k-1}\)包含了\(1\simk\)的所有整数,则价值加\(1\),然后令\(i=i+k\)。如果没有包含\(1\simk\)的所有整数则\(i=i+1\),直到\(i\geqn-k+2\)时结束。......
  • P9663 Permutation on Tree 题解
    考虑枚举一个\(x\in[1,n)\),将\(\leqx\)的看作\(0\),\(>x\)的看作\(1\),那么一个排列的贡献实际上就是\(\sum_{x=1}^{n-1}\sum[[p_i\leqx]+[p_{i+1}>x]=1]\)。那么问题转变为一个给定一棵树,每一个点有权值\(0\)或\(1\),求所有排布方案的贡献之和。设\(f_x\)表示......
  • CF1918G Permutation of Given 题解
    总体思路本题通过每次在已知序列中加入\(2\)个元素的方法,可以构造出满足条件的序列\(A\),这里提供一种新的构造方法。性质因为序列\(A\)中所有元素构成的可重集与序列\(B\)中所有元素构成的可重集完全相等,所以\(A\)中所有元素之和与\(B\)中所有元素之和相等。\[\s......
  • 加权排列熵Weighted Permutation Entropy及多尺度系列(Matlab版)
    学者们开发了各种复杂性度量来比较时间序列并区分规则(例如,周期),混沌和随机行为。提出了加权排列熵概念,其是一个定义简单的复杂性度量,可以很容易地计算任何类型的时间序列,无论是规则的,混沌的,嘈杂的,还是基于现实的时间序列。(matlab代码获取:https://mbd.pub/o/bread/mbd-ZZmbm5pv)参......
  • P10033 「Cfz Round 3」Sum of Permutation
    原题链接基础赛唯一写了的题,因为我喜欢构造!事实上的确有点麻烦了,应该会有更好的做法。但是自我感觉这个思维很连贯,因为这就是我做题时思路的写照。记\(p_{pos1}=1,p_{posn}=n\)。首先可以构造\(a_i\getsp_i+1\)这样一定满足第二个限制,但是当\(p_i=n\)时不满足第一个限......
  • ARC167D Good Permutation 题解
    ARC167D看到排列并且有\(i\getsa_i\),就可以直接建出图来,显然是若干个不相干的环。如果不求字典序最小,就可以直接不在同一个环中的\(i,j\)直接交换就可以了,因为它要求了最小化操作数。如果求字典序最小,直接从前往后扫一遍,可以用set维护不在这个环中且\(j>i\)的最小值,如果......
  • CodeForces 1909F2 Small Permutation Problem (Hard Version)
    洛谷传送门CF传送门感觉这个题还是挺不错的。考虑F1。考察\(a_i\)差分后的意义,发现\(a_i-a_{i-1}\)就是\((\sum\limits_{j=1}^{i-1}[p_j=i])+p_i\lei\)。考虑将其转化为棋盘问题。在\((i,p_i)\)放一个车,那么\(a_i-a_{i-1}\)就是\((1,i)\sim......
  • CF213E Two Permutation 题解
    CF213ETwoPermutations题解题意:给出两个排列$a,b$,长度分别为\(n,m\),你需要计算有多少个$x$,使得\(a_1+x,a_2+x,...a_n+x\)是\(b\)的子序列。\(n\leqm\leq2\times10^5\)分析:一个很自然的思路是直接枚举\(x\),然后只保留\(b\)中值域在\([x+1,x+n]\)......
  • [ARC141C] Bracket and Permutation
    考虑假设已知括号序列\(s\),如何求出\(p,q\)。对于求\(p\),考虑从\(s_1\)到\(s_n\)逐个往里放,如果能放就直接放,肯定不劣,否则就从后面抽最近的左括号放过来,然后继续放。不难证明不存在更优方案,对于\(q\)同理。接下来我们发现,如果\(p\)中存在\(p_i<p_{i-1}\),\(s_{p_{i......