阿里国际笔试 0902笔试
第二题Pokemon的题,想知道以下做法为什么不对,我没有超时,显示的答案错误,过了30%多
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
int t = in.nextInt();
int len, n, k, thing, time;
for (int i = 0; i < t; i++) {
len = in.nextInt();
n = in.nextInt();
k = in.nextInt();
Map<Integer, Integer> map = new HashMap<>();
List<Integer> things = new ArrayList<>();
for (int j = 0; j < n; j++) {
thing = in.nextInt();
time = in.nextInt();
if (thing > k) {
things.add(thing);
map.put(thing, time);
}
}
things.sort((o1, o2) -> o1 - o2);
solve(k, map, things);
}
}
public static void solve(int k, Map<Integer, Integer> map, List<Integer> things) {
boolean flag = true;
long robot = 1;
long pokemon = k;
for (int next : things) {
long distance = next - pokemon;
pokemon += distance;
robot += distance;
int time = map.get(next);
robot = robot + time;
if (robot >= pokemon) {
flag = false;
break;
}
}
if (flag) {
System.out.println("YES");
} else {
System.out.println("NO");
}
}
}
#笔试##阿里##阿里国际##24秋招#

