Codeforces Round 812 (Div. 2) ---- C. Build Permutation --- 题解

目录

[C. Build Permutation](#C. Build Permutation)

题目描述:

[​编辑 思路解析:](#编辑 思路解析:)

代码实现:


C. Build Permutation

题目描述:

思路解析:

先证明在任何情况下答案均存在。

假设我们所求的为 m m+1 m+2.....n 的排列,我们称不小于n的最小平方数为h,不大于n的最大平方数为w。那么h和w之间的差值为根号w+根号h一定小于n,则 h <= 2 * n,那么 h-n <= n.

因此pi = h-i,我们可以将它填充为h h-k<=i<=k,利用这种方法可以递归地把k还原为-1

代码实现:

复制代码
import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class Main {
    static int[] arr;
    public static void main(String[] args) throws IOException {
        int t = input.nextInt();
        for (int o = 0; o < t; o++) {
            int n = input.nextInt();
            arr = new int[n];
            recurse(n - 1);
            for (int i = 0; i < n; i++) {
                pw.print(arr[i] + " ");
            }
            pw.println();
        }
        pw.flush();
        pw.close();
        br.close();
    }
    public static void recurse(int r){
        if (r < 0) return;
        int t = (int) Math.sqrt(2 * r);
        t *= t;
        int l = t - r;
        recurse(l - 1);
        for (; l <= r; l++, r--) {
            arr[l] =r;
            arr[r] = l;
        }
    }

    static PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));
    static Input input = new Input(System.in);
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    static class Input {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public Input(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public String nextLine() {
            String str = null;
            try {
                str = reader.readLine();
            } catch (IOException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }
            return str;
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public Double nextDouble() {
            return Double.parseDouble(next());
        }

        public BigInteger nextBigInteger() {
            return new BigInteger(next());
        }
    }
}
相关推荐
BenChuat23 分钟前
Java常见排序算法实现
java·算法·排序算法
元亓亓亓44 分钟前
LeetCode热题100--105. 从前序与中序遍历序列构造二叉树--中等
算法·leetcode·职场和发展
纪元A梦1 小时前
贪心算法在SDN流表优化中的应用
算法·贪心算法
JCBP_1 小时前
QT(4)
开发语言·汇编·c++·qt·算法
码熔burning1 小时前
JVM 垃圾收集算法详解!
jvm·算法
小柴狗2 小时前
C语言关键字详解:static、const、volatile
算法
仙俊红4 小时前
LeetCode每日一题,20250914
算法·leetcode·职场和发展
风中的微尘10 小时前
39.网络流入门
开发语言·网络·c++·算法
西红柿维生素11 小时前
JVM相关总结
java·jvm·算法
ChillJavaGuy13 小时前
常见限流算法详解与对比
java·算法·限流算法