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());
        }
    }
}
相关推荐
大闲在人5 分钟前
7. 供应链与制造过程术语:“周期时间”
算法·供应链管理·智能制造·工业工程
小熳芋8 分钟前
443. 压缩字符串-python-双指针
算法
Charlie_lll18 分钟前
力扣解题-移动零
后端·算法·leetcode
chaser&upper19 分钟前
矩阵革命:在 AtomGit 解码 CANN ops-nn 如何构建 AIGC 的“线性基石”
程序人生·算法
weixin_4997715527 分钟前
C++中的组合模式
开发语言·c++·算法
iAkuya1 小时前
(leetcode)力扣100 62N皇后问题 (普通回溯(使用set存储),位运算回溯)
算法·leetcode·职场和发展
近津薪荼1 小时前
dfs专题5——(二叉搜索树中第 K 小的元素)
c++·学习·算法·深度优先
xiaoye-duck1 小时前
吃透 C++ STL list:从基础使用到特性对比,解锁链表容器高效用法
c++·算法·stl
松☆1 小时前
CANN与大模型推理:在边缘端高效运行7B参数语言模型的实践指南
人工智能·算法·语言模型
java干货1 小时前
为什么 “File 10“ 排在 “File 2“ 前面?解决文件名排序的终极算法:自然排序
开发语言·python·算法