美团831笔试开发第3题
在尝试用 TreeSet 实现,但是还是连着 wa 了两次,过了 0 个用例,心态有点爆炸,就提前 5 分钟交卷,结果尿了泡尿,好像检查出自己的问题在 rightEdge 和 leftEdge 没有和 l,r 取并集上,心态更炸裂,这次的题目感觉很简单,前两道题 15 分钟就做完了,第三题看着也不难,不知道大家都咋样?
这道题我想的思路是用 treeset 保存所有 max 值的下标,然后判断 l,r 区间内有没有 max 值,有几个 max 值,l, r 区间外有没有 max 值来实现
import java.util.Scanner;
import java.util.Arrays;
import java.util.HashMap;
import java.util.TreeSet;
import java.util.HashSet;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
int n = in.nextInt(), q = in.nextInt();
int[] arr = new int[n];
TreeSet set = new TreeSet<>();
int max_val = 0;
for (int i = 0; i < n; i++) {
arr[i] = in.nextInt();
if (arr[i] > max_val) {
max_val = arr[i];
set.clear();
set.add(i + 1);
}
}
while (q-- > 0) {
int l = in.nextInt();
int r = in.nextInt();
if (l == 1 && r == n) {
if (set.size() == 1) {
System.out.println("lose");
System.out.println(r == l ? 2 : r - l + 1);
} else {
System.out.println("draw");
System.out.println(r == l ? 2 : r - l + 1);
}
continue;
}
if (set.ceiling(l) == null || set.floor(r) == null || set.ceiling(l) > r || set.floor(r) < l) {
System.out.println("win");
long right_edge = set.ceiling(l) == null ? Integer.MAX_VALUE : set.ceiling(l);
long left_edge = set.floor(r) == null ? Integer.MIN_VALUE : set.floor(r);
long min_size = Math.min(right_edge - l + 1, r - left_edge + 1);
System.out.println(min_size == 1 ? 2 : min_size);
continue;
}
// pointing to the same biggest
if (set.ceiling(l) != set.floor(r)) {
// has additional biggest besides [l, r]
if (set.floor(l - 1) != null || set.ceiling(r + 1) != null) {
long right_edge = set.ceiling(r + 1) == null ? Integer.MAX_VALUE : set.ceiling(r + 1);
long left_edge = set.floor(l - 1) == null ? Integer.MIN_VALUE : set.floor(l - 1);
System.out.println("draw");
long min_size = Math.min(Math.max(right_edge, r) - l + 1, r - Math.min(l, left_edge) + 1);
System.out.println(min_size == 1 ? 2 : min_size);
continue;
} else {
System.out.println("lose");
System.out.println(r == l ? 2 : r - l + 1);
continue;
}
} else {
if (set.floor(l - 1) != null || set.ceiling(r + 1) != null) {
long right_edge = set.ceiling(r + 1) == null ? Integer.MAX_VALUE : set.ceiling(r + 1);
long left_edge = set.floor(l - 1) == null ? Integer.MIN_VALUE : set.floor(l - 1);
System.out.println("draw");
long min_size = Math.min(Math.max(r, right_edge) - l + 1, r - Math.min(l, left_edge) + 1);
System.out.println(min_size == 1 ? 2 : min_size);
continue;
} else {
System.out.println("lose");
System.out.println(r == l ? 2 : r - l + 1);
continue;
}
}
}
}
}
这道题我想的思路是用 treeset 保存所有 max 值的下标,然后判断 l,r 区间内有没有 max 值,有几个 max 值,l, r 区间外有没有 max 值来实现
import java.util.Scanner;
import java.util.Arrays;
import java.util.HashMap;
import java.util.TreeSet;
import java.util.HashSet;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
int n = in.nextInt(), q = in.nextInt();
int[] arr = new int[n];
TreeSet
int max_val = 0;
for (int i = 0; i < n; i++) {
arr[i] = in.nextInt();
if (arr[i] > max_val) {
max_val = arr[i];
set.clear();
set.add(i + 1);
}
}
while (q-- > 0) {
int l = in.nextInt();
int r = in.nextInt();
if (l == 1 && r == n) {
if (set.size() == 1) {
System.out.println("lose");
System.out.println(r == l ? 2 : r - l + 1);
} else {
System.out.println("draw");
System.out.println(r == l ? 2 : r - l + 1);
}
continue;
}
if (set.ceiling(l) == null || set.floor(r) == null || set.ceiling(l) > r || set.floor(r) < l) {
System.out.println("win");
long right_edge = set.ceiling(l) == null ? Integer.MAX_VALUE : set.ceiling(l);
long left_edge = set.floor(r) == null ? Integer.MIN_VALUE : set.floor(r);
long min_size = Math.min(right_edge - l + 1, r - left_edge + 1);
System.out.println(min_size == 1 ? 2 : min_size);
continue;
}
// pointing to the same biggest
if (set.ceiling(l) != set.floor(r)) {
// has additional biggest besides [l, r]
if (set.floor(l - 1) != null || set.ceiling(r + 1) != null) {
long right_edge = set.ceiling(r + 1) == null ? Integer.MAX_VALUE : set.ceiling(r + 1);
long left_edge = set.floor(l - 1) == null ? Integer.MIN_VALUE : set.floor(l - 1);
System.out.println("draw");
long min_size = Math.min(Math.max(right_edge, r) - l + 1, r - Math.min(l, left_edge) + 1);
System.out.println(min_size == 1 ? 2 : min_size);
continue;
} else {
System.out.println("lose");
System.out.println(r == l ? 2 : r - l + 1);
continue;
}
} else {
if (set.floor(l - 1) != null || set.ceiling(r + 1) != null) {
long right_edge = set.ceiling(r + 1) == null ? Integer.MAX_VALUE : set.ceiling(r + 1);
long left_edge = set.floor(l - 1) == null ? Integer.MIN_VALUE : set.floor(l - 1);
System.out.println("draw");
long min_size = Math.min(Math.max(r, right_edge) - l + 1, r - Math.min(l, left_edge) + 1);
System.out.println(min_size == 1 ? 2 : min_size);
continue;
} else {
System.out.println("lose");
System.out.println(r == l ? 2 : r - l + 1);
continue;
}
}
}
}
}
全部评论
我思路是线段树+L的前更大值,r的后的更大值(用单调栈存一个数组)
相关推荐
11-19 12:10
湖南财政经济学院 Java 点赞 评论 收藏
分享