当前位置:网站首页>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
边栏推荐
- 【无标题】
- sql实战积累
- Mysql database ALTER basic operations
- Web性能测试模型小结
- hint: Updates were rejected because the tip of your current branch is behind hint: its remote counte
- assert利用蚁剑登录
- 使用 GoogleTest 框架对 C 代码进行单元测试
- 宽带由20M换为100M
- Unity reports Unsafe code may only appear if compiling with /unsafe. Enable “Allow ‘unsafe’ code” in Pla
- 【软考软件评测师】软件测试基础知识
猜你喜欢
Teach you how to write performance test cases
【LeetCode】求根节点到叶节点数字之和
多线程之自定义线程池
改变社交与工作状态的即时通讯是什么呢?
02| operator
【论文笔记】基于深度学习的机器人抓取虚拟仿真实验教学系统
跳房子游戏
Penetration Testing and Offensive and Defense Confrontation - Vulnerability Scanning & Logic Vulnerability (Part1)
Unity vertex animation
Solidity最强对手:MOVE语言及新公链崛起
随机推荐
Solidity最强对手:MOVE语言及新公链崛起
OpenSSF的开源软件风险评估工具:Scorecards
DALL·E-2是如何工作的以及部署自己的DALL·E模型
人际关系不仅要“存”,更要“激活”!
浏览器中location详解
unity 报错 Unsafe code may only appear if compiling with /unsafe. Enable “Allow ‘unsafe‘ code“ in Pla
头脑风暴:单词拆分
UI遍历的初步尝试
Solve the problem of sed replacement text containing special characters such as "/" and "#"
高校就业管理系统设计与实现
为什么字符串一旦创建就不可以改变?
不是吧,连公司里的卷王写代码都复制粘贴,这合理?
Unity image is blurry after using long image
【Swoole系列3.5】进程池与进程管理器
OSS-访问oss生成的url无法访问,直接下载问题
你有对象类,我有结构体,Go lang1.18入门精炼教程,由白丁入鸿儒,go lang结构体(struct)的使用EP06
Qt的pro文件递归搜寻添加文件
DHCP——动态主机配置协议
ABAP 里文件操作涉及到中文字符集的问题和解决方案
Quick responsiveness intelligent/smart responsiveness of polyethylene glycol type nano/reduction response hydrogels research and preparation