华为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;
}
全部评论
相关推荐
06-04 10:32
安徽大学 单片机 实习僧和BOSS直聘都投了几十家,硬件开发,硬件测试,嵌入式都投了,全是已读不回……我现在考虑想在秋招前速成一个Linux项目,其实现在完全不知道自己要找什么方向的,只能海投了,求大佬们给点意见😭😭😭
西工程小巴:数电课设+数字信号处理课设+微机原理课设?

点赞 评论 收藏
分享
06-17 11:08
天津理工大学 Java 点赞 评论 收藏
分享