E. Clique Partition

Here

 

解题思路

  •  a1\rightarrow n的排列
  • \left | i-j \right |+\left | a_i-a_j \right |\leq k,要维持不等式,则\left | i-j \right |变化多少,\left | a_i-a_j \right |也相反变化
  • 所以选取一段连续的i两两满足不等式,则a_i也选连续的一段区间
  • 所以1\rightarrow n被切分成若干区间
  • 要求划分的区间数尽可能少
  • 因为a_i\neq a_j,所以区间最长为k
  • 考虑如何赋值,使满足不等式
  • [l,r]a_l-a_r=1,r-l=k-1
  • u=l+(k+1)/2,v=u+1,v-u=1,a_u-a_v=k-1
  • 所以a_l,a_r连续,a_u,a_v为最大最小值
  • a[l,r]上的赋值从区间中间开始,在从尾部折回
  • 对于模k的余数,单独划分为一个长度为余数的区间



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();
        }
    }
}



 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值