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());
        }
    }
}
相关推荐
凌肖战1 小时前
力扣网编程55题:跳跃游戏之逆向思维
算法·leetcode
88号技师2 小时前
2025年6月一区-田忌赛马优化算法Tianji’s horse racing optimization-附Matlab免费代码
开发语言·算法·matlab·优化算法
ゞ 正在缓冲99%…2 小时前
leetcode918.环形子数组的最大和
数据结构·算法·leetcode·动态规划
Kaltistss3 小时前
98.验证二叉搜索树
算法·leetcode·职场和发展
知己如祭3 小时前
图论基础(DFS、BFS、拓扑排序)
算法
mit6.8244 小时前
[Cyclone] 哈希算法 | SIMD优化哈希计算 | 大数运算 (Int类)
算法·哈希算法
c++bug4 小时前
动态规划VS记忆化搜索(2)
算法·动态规划
哪 吒4 小时前
2025B卷 - 华为OD机试七日集训第5期 - 按算法分类,由易到难,循序渐进,玩转OD(Python/JS/C/C++)
python·算法·华为od·华为od机试·2025b卷
军训猫猫头4 小时前
1.如何对多个控件进行高效的绑定 C#例子 WPF例子
开发语言·算法·c#·.net
success4 小时前
【爆刷力扣-数组】二分查找 及 衍生题型
算法