华为OD机试真题 - 执行时长
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int GPU = scanner.nextInt();
int runableCount = scanner.nextInt();
PriorityQueue priorityQueue = new PriorityQueue(new Comparator() {
@Override
public int compare(Integer o1, Integer o2) {
return o2 - o1;
}
});
for (int i = 0; i < runableCount; i++) {
priorityQueue.add(scanner.nextInt());
}
System.out.println(getMinTime(GPU, priorityQueue));
}
public static int getMinTime(int GPUCount, PriorityQueue runables) {
GPUPoll poll = new GPUPoll(GPUCount);
int i = 0;
while (!runables.isEmpty()) {
while (poll.hasBanancePoll() != -1) {
if (runables.peek() != null)
poll.poll[poll.hasBanancePoll()] = runables.poll();
else break;
}
poll.execut();
i++;
}
i += poll.getMaxTime();
return i;
}
static class GPUPoll {
int[] poll;
public GPUPoll(int count) {
this.poll = new int[count];
}
public void execut() {
for (int i = 0; i < poll.length; i++) {
poll[i] -= 1;
}
}
public int hasBanancePoll() {
for (int i = 0; i < poll.length; i++) {
if (poll[i] == 0)
return i;
}
return -1;
}
public int getMaxTime() {
int max = Integer.MIN_VALUE;
for (int i = 0; i < poll.length; i++) {
if (poll[i] > 0) {
max = Math.max(max,poll[i]);
}
}
return max;
}
}
Scanner scanner = new Scanner(System.in);
int GPU = scanner.nextInt();
int runableCount = scanner.nextInt();
PriorityQueue priorityQueue = new PriorityQueue
@Override
public int compare(Integer o1, Integer o2) {
return o2 - o1;
}
});
for (int i = 0; i < runableCount; i++) {
priorityQueue.add(scanner.nextInt());
}
System.out.println(getMinTime(GPU, priorityQueue));
}
public static int getMinTime(int GPUCount, PriorityQueue
GPUPoll poll = new GPUPoll(GPUCount);
int i = 0;
while (!runables.isEmpty()) {
while (poll.hasBanancePoll() != -1) {
if (runables.peek() != null)
poll.poll[poll.hasBanancePoll()] = runables.poll();
else break;
}
poll.execut();
i++;
}
i += poll.getMaxTime();
return i;
}
static class GPUPoll {
int[] poll;
public GPUPoll(int count) {
this.poll = new int[count];
}
public void execut() {
for (int i = 0; i < poll.length; i++) {
poll[i] -= 1;
}
}
public int hasBanancePoll() {
for (int i = 0; i < poll.length; i++) {
if (poll[i] == 0)
return i;
}
return -1;
}
public int getMaxTime() {
int max = Integer.MIN_VALUE;
for (int i = 0; i < poll.length; i++) {
if (poll[i] > 0) {
max = Math.max(max,poll[i]);
}
}
return max;
}
}
全部评论
相关推荐
慢热的马里奥不想上班:楼主很努力,继续加油吧,咱只能管好自己不作弊,其他人爱咋就咋吧。
点赞 评论 收藏
分享