当前位置: 首页 > news >正文

Codeforces Round 812 (Div. 2) ---- 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());}}
}

 

http://www.lryc.cn/news/286719.html

相关文章:

  • Matlab 将工作区变量保存到文件中(save)
  • 源码实现简介
  • 我每天如何使用 ChatGPT
  • MySQL修炼手册14:用户权限管理:安全保障与数据隔离
  • 动态规划解决马尔可夫决策过程
  • ubuntu1604安装及问题解决
  • Leetcode—24. 两两交换链表中的节点【中等】
  • USRP相关报错解决办法
  • 【剑指offer】重建二叉树
  • 中仕教育:事业编招考全流程介绍
  • 149. 直线上最多的点数
  • 不合格机器人工程讲师再读《悉达多》-2024-
  • 【STM32CubeMX串口通信详解】USART2 -- DMA发送 + DMA空闲中断 接收不定长数据
  • Webpack5入门到原理19:React 脚手架搭建
  • 苹果眼镜(Vision Pro)的开发者指南(6)-实战应用场景开发 - 游戏、协作、空间音频、WebXR
  • flutter底层架构初探
  • 初识SQL注入
  • React初探:从环境搭建到Hooks应用全解析
  • 设计模式——1_6 代理(Proxy)
  • 性能优化(CPU优化技术)-NEON 介绍
  • Kafka-服务端-KafkaController
  • ffmpeg使用手册
  • 操作系统导论-课后作业-ch15
  • 宝塔面板SRS音视频TRC服务器启动失败
  • 04-Seata修改通信端口
  • 活动回顾丨云原生技术实践营上海站「云原生 AI 大数据」专场(附 PPT)
  • 【数据结构与算法】4.自主实现单链表的增删查改
  • Linux系统常用命令行指令
  • java SSM园林绿化管理系统myeclipse开发mysql数据库springMVC模式java编程计算机网页设计
  • 【issue-halcon例程学习】edges_color.hdev