E. Klever Permutation

E. Klever Permutation

You are given two integers n and k (kn), 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 nk+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 nk+1, where si=j=ii+k1pj, i.e., the i-th element is equal to the sum of pi,pi+1,,pi+k1.

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

Find any k-level permutation of length n.

Input

The first line of the input contains a single integer t (1t104) — 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 (2kn2105, 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 2105.

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:

p1+p2=3+1=4;

p2+p3=1+2=3.

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

 

解题思路

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

  首先相邻的 sisi+1 必然不同,这是因为 sisi+1=aiai+k,由于 a 是排列因此必然有 aiai+k,所以 sisi+1 至少相差 1。由于还要保证所有的 si 中最大值与最小值的差不超过 1,假设 s1=x,那么 s 的形式必然是 [x,x+1,x,x+1,x,] 或者 [x,x1,x,x1,x,]

  假设构造的 s 是这样的 [x,x+1,x,x+1,x,],那么根据 s1s2=1,有 a1=a1+k+1。同理可得 a2=a2+k1a3=a3+k+1a4=a4+k1,以此类推。发现如果是奇数下标,那么就有 ai=ai+k+1,否则是偶数下标就有 ai=ai+k+1

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

  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

posted @   onlyblues  阅读(60)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
历史上的今天:
2023-02-10 E. Sum Over Zero
2022-02-10 蹄球
Web Analytics
点击右上角即可分享
微信分享提示