华为OD机试真题 - 执行任务赚积分
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int count = in.nextInt();
int time = in.nextInt();
LinkedList<int[]> runables = new LinkedList<>();
for (int i = 0; i < count; i++) {
int[] runable = new int[2];
runable[0] = in.nextInt();
runable[1] = in.nextInt();
runables.add(runable);
}
System.out.println(getMaxScore(runables, time));
}
public static int getMaxScore(LinkedList<int[]> runables, int time) {
Collections.sort(runables, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
if (o1[0] == o2[0])
return o2[1] - o1[1];
return o1[0] - o2[0];
}
});
int score = 0;
for (int i = 1; i < time; i++) {
int[] runable = runables.pollFirst();
if (runable[0] >= i) {
score += runable[1];
while (runables.peekFirst() != null && runables.peekFirst()[0] == runable[0]) {
runables.pollFirst();
}
}
}
return score;
}
Scanner in = new Scanner(System.in);
int count = in.nextInt();
int time = in.nextInt();
LinkedList<int[]> runables = new LinkedList<>();
for (int i = 0; i < count; i++) {
int[] runable = new int[2];
runable[0] = in.nextInt();
runable[1] = in.nextInt();
runables.add(runable);
}
System.out.println(getMaxScore(runables, time));
}
public static int getMaxScore(LinkedList<int[]> runables, int time) {
Collections.sort(runables, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
if (o1[0] == o2[0])
return o2[1] - o1[1];
return o1[0] - o2[0];
}
});
int score = 0;
for (int i = 1; i < time; i++) {
int[] runable = runables.pollFirst();
if (runable[0] >= i) {
score += runable[1];
while (runables.peekFirst() != null && runables.peekFirst()[0] == runable[0]) {
runables.pollFirst();
}
}
}
return score;
}
全部评论
相关推荐
点赞 评论 收藏
分享