路标设置
原题链接
解题思路
二分答案模板题,二分右边界可以设置为相邻两个路标的最大值
猜一个最小距离:如果这个距离是满足题目要求的,那么大于这个距离(路标剩余数量大于等于当前距离)肯定也满足要求;如果这个距离不满足要求,那么小于这个距离的肯定也不满足要求(路标数量不够)。
check 函数中用pre记录上一个路标的位置,初始值为起点 0,如果当前路标到上一个路标距离小于等于 k,则这两个路标之间不需要再增加新的路标;否则添加一个路标,然后更新当前路标的上一个路标的位置,再循环判断一下当前的路标(i–)。
参考代码
import java.io.*;
public class Main {
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
static int N = (int)1e5 + 10, l, n, k;
static int[] a = new int[N];
static int left = 0, right = Integer.MIN_VALUE;
public static boolean check(int x) {
int cnt = k, pre = 0;
for (int i = 1; i <= n; i++) {
if (cnt < 0) return false;
if (a[i] - pre <= x) {
pre = a[i];
continue;
}
pre += x;
i--;
cnt--;
}
return cnt >= 0;
}
public static void main(String[] args) throws Exception {
// input...
String[] lnk = in.readLine().split(" ");
l = Integer.parseInt(lnk[0]);
n = Integer.parseInt(lnk[1]);
k = Integer.parseInt(lnk[2]);
String[] s = in.readLine().split(" ");
for (int i = 1; i <= n; i++) {
a[i] = Integer.parseInt(s[i - 1]);
right = Math.max(right, a[i] - a[i - 1]);
}
while (left < right) {
int mid = left + ((right - left) >> 1);
if (check(mid)) {
right = mid;
} else {
left = mid + 1;
}
}
out.println(left);
out.flush();
in.close();
}
}
通往奥格瑞玛的道路
原题链接
解题思路
todo:这里是解题思路
参考代码
todo:这里是参考代码
求和
原题链接
解题思路
todo:这里是解题思路
参考代码
todo:这里是参考代码
大师
原题链接
解题思路
todo:这里是解题思路
参考代码
todo:这里是参考代码