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());
        }
    }
}
相关推荐
feifeigo12330 分钟前
MATLAB实现两组点云ICP配准
开发语言·算法·matlab
fengfuyao98532 分钟前
粒子群算法(PSO)求解标准VRP问题的MATLAB实现
开发语言·算法·matlab
Ayanami_Reii1 小时前
进阶数据结构应用-SPOJ 3267 D-query
数据结构·算法·线段树·主席树·持久化线段树
guygg881 小时前
基于全变差的压缩感知视频图像重构算法
算法·重构·音视频
VT LI2 小时前
SDF在实时图形渲染中的核心原理与架构创新
算法·sdf·有号距离场
想七想八不如114082 小时前
408操作系统 PV专题
开发语言·算法
天一生水water2 小时前
储层认知→技术落地→产量优化
人工智能·算法·机器学习
明洞日记2 小时前
【VTK手册019】 深入理解 vtkProperty:从几何表达到 PBR 物理渲染
c++·图像处理·算法·vtk·图形渲染
Genevieve_xiao2 小时前
【数据结构与算法】【xjtuse】面向考纲学习(下)
java·数据结构·学习·算法
修炼地2 小时前
代码随想录算法训练营第二十七天 | 56. 合并区间、738.单调递增的数字、968.监控二叉树
c++·算法