A. 题目链接
算法标签:模拟。
public static void solve() throws IOException {
String s = in.nextLine();
String[] a = s.split(" ");
int ans = 0;
for (String x : a) {
if (Character.isUpperCase(x.charAt(0))) {
ans++;
}
}
out.println(ans);
}
B. 题目链接
算法标签:二分、二分答案。
思路:假设区间为最短区间长度为m可以种k棵树,那么m + 1、m + 2、m + 3… 都可以种k棵树,具有单调性。因此可以二分答案,如果m比较大,就缩小R,否则增大L。check函数的逻辑主要判断区间长度为m能否种k棵树就行了。
// check函数
static boolean check(int mid, int[] a, int k) {
int cnt = mid;
int pre = a[0] + mid - 1;
for (int i = 1; i < a.length; i++) {
int cur = a[i] + mid - 1;
if (pre >= a[i]) {
cnt += cur - pre;
} else {
cnt += mid;
}
pre = cur;
}
return cnt >= k;
}
public static void solve() throws IOException {
int n = in.nextInt();
int k = in.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = in.nextInt();
int l = 1;
int r = 200001;
while (l < r) {
int mid = (l + r) >> 1;
if (check(mid, a, k)) {
r = mid;
} else {
l = mid + 1;
}
}
out.println(l);
}