当前位置:网站首页>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
边栏推荐
- Solving for the number of mines
- Prometeus 2.31.0 新特性
- 【Grpc】简介
- Mysql database ALTER basic operations
- 使用 apxs 构建和安装 Apache 扩展共享对象模块
- Fedora 36 dnf 安装ModSecurity和 OWASP 核心规则集
- hint: Updates were rejected because the tip of your current branch is behind hint: its remote counte
- 手把手教你编写性能测试用例
- sql实战积累
- C language pointer practice questions
猜你喜欢
基于设计稿识别的可视化低代码系统实践
Unity vertex animation
Mysql database ALTER basic operations
为什么字符串一旦创建就不可以改变?
[LeetCode] Find the sum of the numbers from the root node to the leaf node
【CAS:41994-02-9 |Biotinyl tyramide】Biotinyl tyramide price
type-C 边充电边听歌(OTG) PD芯片方案,LDR6028 PD充电加OTG方案
XSS详解及复现gallerycms字符长度限制短域名绕过
GBJ1510-ASEMI机器人电源整流桥GBJ1510
ITK编译remote库
随机推荐
Minimum number of steps to get out of the maze 2
【ROS2原理10】Interface数据的规定
GBJ1510-ASEMI机器人电源整流桥GBJ1510
el-input保留一位小数点
彩色袜子题
-Pickling peanuts-
将string类对象中的内容格式化到字符串Buffer中时遇到的异常崩溃分析
Solidity最强对手:MOVE语言及新公链崛起
mstsc/Mstsc (Microsoft terminal services client)远程桌面连接
防勒索病毒现状分析
删除表空间数据文件
小程序实现搜索功能续
对修饰器的实验支持功能在将来的版本中可能更改。在 “tsconfig“ 或 “jsconfig“ 中设置 “experimentalDecorators“ 选项以删除此警告
PEG derivative Biotin-PEG1-OH (cas: 95611-10-2, 2-biotinaminoethanol) advantage description
嵌入式Qt-实现两个窗口的切换
即时通讯开发如何撸一个WebSocket服务器
手把手教你编写性能测试用例
Unity顶点动画
墨西哥大众VW Mexico常见的几种label
Chip Information|Semiconductor revenue growth expected to slow to 7%, Bluetooth chip demand still growing steadily