解题思路
-
为
的排列
,要维持不等式,则
变化多少,
也相反变化
- 所以选取一段连续的
两两满足不等式,则
也选连续的一段区间
- 所以
被切分成若干区间
- 要求划分的区间数尽可能少
- 因为
,所以区间最长为
- 考虑如何赋值,使满足不等式
,
- 所以
连续,
为最大最小值
- 则
在
上的赋值从区间中间开始,在从尾部折回
- 对于模
的余数,单独划分为一个长度为余数的区间
import java.io.*;
import java.math.BigInteger;
import java.util.*;
//implements Runnable
public class Main {
static long md=(long)998244353;
static long Linf=Long.MAX_VALUE/2;
static int inf=Integer.MAX_VALUE/2;
static int N=200010;
static int n=0;
static int m=0;
static void solve() throws Exception{
AReader input=new AReader();
// String fileName="C:\\Users\\Lenovo\\Downloads\\055.txt";
// Scanner input=new Scanner(new FileReader(fileName));
// BufferedReader input = new BufferedReader(new FileReader(fileName));
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
String al="abcdefghijklmnopqrstuvwxyz";
char[] ac=al.toCharArray();
int T=input.nextInt();
while(T>0){
T--;
n=input.nextInt();
int k=input.nextInt();
int p=n/k;
int t=1;
int[] b=new int[n+1];
int[] a=new int[n+1];
for(int o=0;o<p;++o){
int l=o*k;
for(int i=l+(k+1)/2+1;i<=l+k;++i){
a[i]=t;
b[i]=o+1;
t++;
}
for(int i=l+1;i<=l+(k+1)/2;++i){
a[i]=t;
b[i]=o+1;
t++;
}
}
int ans=p;
if(n%k!=0){
ans=p+1;
int o=n%k;
int l=p*k;
for(int i=l+(o+1)/2+1;i<=l+o;++i){
a[i]=t;
b[i]=p+1;
t++;
}
for(int i=l+1;i<=l+(o+1)/2;++i){
a[i]=t;
b[i]=p+1;
t++;
}
}
for(int i=1;i<=n;++i)out.print(a[i]+" ");
out.println();
out.println(ans);
for(int i=1;i<=n;++i)out.print(b[i]+" ");
out.println();
}
out.flush();
out.close();
}
public static void main(String[] args) throws Exception{
solve();
}
// public static final void main(String[] args) throws Exception {
// new Thread(null, new Tx2(), "线程名字", 1 << 27).start();
// }
// @Override
// public void run() {
// try {
// //原本main函数的内容
// solve();
//
// } catch (Exception e) {
// }
// }
static
class AReader{
BufferedReader bf;
StringTokenizer st;
BufferedWriter bw;
public AReader(){
bf=new BufferedReader(new InputStreamReader(System.in));
st=new StringTokenizer("");
bw=new BufferedWriter(new OutputStreamWriter(System.out));
}
public String nextLine() throws IOException{
return bf.readLine();
}
public String next() throws IOException{
while(!st.hasMoreTokens()){
st=new StringTokenizer(bf.readLine());
}
return st.nextToken();
}
public char nextChar() throws IOException{
//确定下一个token只有一个字符的时候再用
return next().charAt(0);
}
public int nextInt() throws IOException{
return Integer.parseInt(next());
}
public long nextLong() throws IOException{
return Long.parseLong(next());
}
public double nextDouble() throws IOException{
return Double.parseDouble(next());
}
public float nextFloat() throws IOException{
return Float.parseFloat(next());
}
public byte nextByte() throws IOException{
return Byte.parseByte(next());
}
public short nextShort() throws IOException{
return Short.parseShort(next());
}
public BigInteger nextBigInteger() throws IOException{
return new BigInteger(next());
}
public void println() throws IOException {
bw.newLine();
}
public void println(int[] arr) throws IOException{
for (int value : arr) {
bw.write(value + " ");
}
println();
}
public void println(int l, int r, int[] arr) throws IOException{
for (int i = l; i <= r; i ++) {
bw.write(arr[i] + " ");
}
println();
}
public void println(int a) throws IOException{
bw.write(String.valueOf(a));
bw.newLine();
}
public void print(int a) throws IOException{
bw.write(String.valueOf(a));
}
public void println(String a) throws IOException{
bw.write(a);
bw.newLine();
}
public void print(String a) throws IOException{
bw.write(a);
}
public void println(long a) throws IOException{
bw.write(String.valueOf(a));
bw.newLine();
}
public void print(long a) throws IOException{
bw.write(String.valueOf(a));
}
public void println(double a) throws IOException{
bw.write(String.valueOf(a));
bw.newLine();
}
public void print(double a) throws IOException{
bw.write(String.valueOf(a));
}
public void print(char a) throws IOException{
bw.write(String.valueOf(a));
}
public void println(char a) throws IOException{
bw.write(String.valueOf(a));
bw.newLine();
}
}
}