优化代码:rest方法
This commit is contained in:
@@ -4,8 +4,7 @@ import com.dota.domain.Doctor;
|
|||||||
import com.dota.domain.Week;
|
import com.dota.domain.Week;
|
||||||
import com.dota.domain.WorkEnum;
|
import com.dota.domain.WorkEnum;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.Arrays;
|
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
static Doctor gl;
|
static Doctor gl;
|
||||||
@@ -104,26 +103,35 @@ public class Main {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
record Rest(int day, int cnt) {
|
static class Rest {
|
||||||
|
int day;
|
||||||
|
int cnt;
|
||||||
|
public Rest(int day, int cnt){
|
||||||
|
this.day = day;
|
||||||
|
this.cnt = cnt;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 给有调休的安排休息
|
// 给有调休的安排休息
|
||||||
// 保证同一组必须有一个在
|
// 保证同一组必须有一个在
|
||||||
static void rest(Doctor[] doctors) {
|
static void rest(Doctor[] doctors) {
|
||||||
var list = new ArrayList<Rest>();
|
var list = new Rest[5];
|
||||||
for (int i = 0; i < 5; i++) {
|
for (int i = 0; i < 5; i++) {
|
||||||
list.add(new Rest(i, 0));
|
list[i] = new Rest(i, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (Doctor doctor : doctors) {
|
for (Doctor doctor : doctors) {
|
||||||
for (int i = 0; i < 5; i++) {
|
for (int i = 0; i < 5; i++) {
|
||||||
if (doctor.works[i] == WorkEnum.DUTY || doctor.works[i] == WorkEnum.WORK) {
|
if (doctor.works[i] == WorkEnum.DUTY || doctor.works[i] == WorkEnum.WORK) {
|
||||||
list.set(i, new Rest(i, list.get(i).cnt + 1));
|
list[i].cnt++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
list.sort((a, b) -> b.cnt - a.cnt);
|
var queue = new PriorityQueue<Rest>((a,b)->b.cnt-a.cnt);
|
||||||
|
for (Rest rest : list) {
|
||||||
|
queue.offer(rest);
|
||||||
|
}
|
||||||
|
|
||||||
for (Doctor doctor : doctors) {
|
for (Doctor doctor : doctors) {
|
||||||
if (doctor.restCount == 0) {
|
if (doctor.restCount == 0) {
|
||||||
@@ -131,18 +139,16 @@ public class Main {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
int j = 0;
|
var temp = new ArrayList<Rest>();
|
||||||
while (i < doctor.restCount) {
|
while (i < doctor.restCount) {
|
||||||
Rest r = list.get(j++);
|
Rest r = queue.poll();
|
||||||
if (checkRest(r.day, doctor, doctors)) {
|
if (checkRest(r.day, doctor, doctors)) {
|
||||||
j = 0;
|
|
||||||
doctor.works[r.day] = WorkEnum.REST;
|
doctor.works[r.day] = WorkEnum.REST;
|
||||||
i++;
|
i++;
|
||||||
list.add(new Rest(r.day, r.cnt - 1));
|
|
||||||
list.remove(r);
|
|
||||||
list.sort((a, b) -> b.cnt - a.cnt);
|
|
||||||
}
|
}
|
||||||
|
temp.add(r);
|
||||||
}
|
}
|
||||||
|
queue.addAll(temp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user