当前位置:网站首页>hopscotch game
hopscotch game
2022-08-10 02:15:00 【Chengqiuming】
A problem description
Second algorithm design
1 If the number of stones removed is equal to the total number of stones (M=N), enter L directly.
2 Add the start (0) and end (N+1) stones, and the distances to the start node are 0 and L, respectively.
3 Sort all stones according to the distance from the starting node from small to large.
4 Let left=0, right=L, if right-left>1, then mid=(right+left)/2, determine whether it is satisfied that after removing M stones, any distance is less than mid.If it is satisfied, it means that the distance can be larger, let left = mid; otherwise, let right = mid, and continue the binary search.
5 After the search, left is the maximum value of the shortest distance that the cow must jump.
Three codes
package com.platform.modules.alg.alglib.poj3258;import java.util.Arrays;public class Poj3258 {public String output = "";private final int maxn = 50050;private Integer L;private Integer n;private Integer m;private Integer dis[] = new Integer[maxn];public Poj3258() {for (int i = 0; i < dis.length; i++) {dis[i] = 0xFFFFFFF;}}public String cal(String input) {String[] line = input.split("\n");String[] line1 = line[0].split(" ");L = Integer.parseInt(line1[0]);n = Integer.parseInt(line1[1]);m = Integer.parseInt(line1[2]);if (n == m) {return L.toString();}for (int i = 1; i <= n; i++) {dis[i] = Integer.parseInt(line[i]);}dis[0] = 0; // Increment start pointdis[n + 1] = L; // add end pointArrays.sort(dis, 0, n + 1);//sort(dis, dis + n + 2);Integer left = 0;Integer right = L;while (right - left > 1) {int mid = (right + left) / 2;if (judge(mid))left = mid;//If it can be released, it means that x can be largerelseright = mid;}return left.toString();}boolean judge(int x) { //After removing m stones, any distance is not less than xint num = n - m; //Remove m stones, place num stones, loop num timesint last = 0; //Record the index of the previous placed stonefor (int i = 0; i < num; i++) { //For these stones, make any spacing not less than xint cur = last + 1;while (cur <= n && dis[cur] - dis[last] < x) //Place the first position with a distance >=x from lastcur++; //accumulate position by curif (cur > n)return false; //If it is greater than n during this process, it means that it cannot be releasedlast = cur; //Update the last position}return true;}}
Four tests
边栏推荐
- Unity image使用长图后 图片很糊
- 高校就业管理系统设计与实现
- DP 优化方法合集
- -red and black-
- 【论文笔记】基于深度学习的机器人抓取虚拟仿真实验教学系统
- Shader Graph学习各种特效案例
- UI遍历的初步尝试
- R语言使用cox函数构建生存分析回归模型、使用subgroupAnalysis进行亚组分析并可视化森林图
- Experimental support for decorators may change in future releases.Set the "experimentalDecorators" option in "tsconfig" or "jsconfig" to remove this warning
- 小程序中计算距离信息
猜你喜欢
In the 2022 gold, nine, silver and ten work tide, how can I successfully change jobs and get a high salary?
Unity image使用长图后 图片很糊
基于设计稿识别的可视化低代码系统实践
开发IM即时通讯容易吗?需要什么技术
Biotin-Cy2 Conjugate, Biotin-Cy2 Conjugate_Cy2 Biotin Conjugate
【论文笔记】基于深度学习的机器人抓取虚拟仿真实验教学系统
芯片资讯|半导体收入增长预计将放缓至 7%,蓝牙芯片需求依然稳步增长
商业模式及其 SubDAO 深入研究
Data storage - the C language
[Turn] Typora_Markdown_ picture title (caption)
随机推荐
R语言使用glm函数构建logistic回归模型,使用forestmodel包的forest_model函数可视化逻辑回归模型对应的森林图
Web性能测试模型小结
初步认识对象
GB28181 sip和RTSP(Real-Time Streaming Protocol)实时流控制协议
02| operator
OSS-访问oss生成的url无法访问,直接下载问题
基于FTP协议实现文件上传与下载
什么是 PWA
【软考软件评测师】软件测试基础知识
【UNR #6 B】机器人表演(DP)
Fedora 36 dnf 安装ModSecurity和 OWASP 核心规则集
Solidity最强对手:MOVE语言及新公链崛起
R语言使用glm函数构建逻辑回归模型(logistic)、使用subgroupAnalysis函数进行亚组分析并可视化森林图
Data storage - the C language
Penetration Testing and Offensive and Defense Confrontation - Vulnerability Scanning & Logic Vulnerability (Part1)
将string类对象中的内容格式化到字符串Buffer中时遇到的异常崩溃分析
RedHat红帽RHEL7安装与使用,VMware Workstation16 Pro虚拟机的安装与使用
2022金九银十工作潮,怎么样才能成功跳槽面试拿到高薪呢?
JVM :运行时数据区-虚拟机栈
芯片资讯|半导体收入增长预计将放缓至 7%,蓝牙芯片需求依然稳步增长