Ha, 10 years passed. But here is my code which gets WA. Can anyone Accepted have a look and say what's wrong?
`/*
* 212. Use of Hospital Facilities
* TOPIC: ad hoc, simulation
* status: in progress
*/
/**
* Created by sj on 17/01/17.
*/
import sun.reflect.annotation.ExceptionProxy;
import javax.transaction.TransactionRequiredException;
import java.awt.geom.PathIterator;
import java.io.*;
import java.sql.Time;
import java.util.*;
import java.util.regex.Pattern;
public class Main {
enum ET {
ROOM_READY, BED_READY, PATIENT_RIGHT_AFTER_SURGERY, PATIENT_AFTER_RECOVERY;
}
final private static int MAXP = 0x80, MAXROOMS = 10, MAXBEDS = 30, oo = (1<<29);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int m,n,pat; // # of operating rooms, beds, patients, resp
int op2rec, prepOp, prepRec;
int start = +oo, end = -oo, completed, total;
public static String time2string( int t ) {
int h = t/60, m = t%60;
return String.format("%d:%02d",h,m);
}
public static void main(String... args) throws Exception {
new Main().go();
}
interface Marker {};
class Patient implements Comparable<Patient>, Marker {
String name;
final int id, minutesForSurgery, minutesForRecovery;
int operationStart, recoveryStart, whichBed, whichRoom;
Patient( int id ) throws Exception {
this.id = id;
name = (new Scanner(br.readLine())).next();
Scanner scan = new Scanner(br.readLine());
minutesForSurgery = scan.nextInt();
minutesForRecovery = scan.nextInt();
}
@Override
public int compareTo( Patient other ) {
return this.id-other.id;
}
@Override
public String toString() {
return String.format("%3d %-8s %2d %5s %5s %2d %5s %5s",id+1,name,whichRoom+1,time2string(operationStart),time2string(operationStart+minutesForSurgery),whichBed+1,time2string(recoveryStart),time2string(recoveryStart+minutesForRecovery));
}
}
abstract class Facility implements Marker {
int timeLoad = 0, wasReadyAt;
public void admit( Patient p, int curTime ) {
timeLoad += (this instanceof OperatingRoom)?p.minutesForSurgery:p.minutesForRecovery;
if ( this instanceof OperatingRoom ) {
start = Math.min(curTime,start);
p.operationStart = curTime;
p.whichRoom = ((OperatingRoom)this).id;
pq.add(new Event(p, curTime + p.minutesForSurgery+op2rec, ET.PATIENT_RIGHT_AFTER_SURGERY));
getReady(curTime+p.minutesForSurgery);
}
else {
p.recoveryStart = curTime;
p.whichBed = ((Bed)this).id;
pq.add(new Event(p, curTime + p.minutesForRecovery, ET.PATIENT_AFTER_RECOVERY));
getReady(curTime+p.minutesForRecovery);
}
}
private void getReady( int operationEnded ) {
if ( this instanceof OperatingRoom )
pq.add(new Event(this,wasReadyAt = operationEnded+prepOp,ET.ROOM_READY));
else {
end = Math.max(end,operationEnded);
pq.add(new Event(this, wasReadyAt = operationEnded+prepRec,ET.BED_READY));
}
}
}
class OperatingRoom extends Facility implements Comparable<OperatingRoom> {
final int id;
@Override
public int compareTo( OperatingRoom other ) {
return this.id-other.id;
}
OperatingRoom( int id ) {
this.id = id;
}
@Override
public String toString() {
return String.format("%-4s %2d %7d %6.2f","Room",id+1,timeLoad,timeLoad/(end-start+0.00)*100);
}
@Override
public boolean equals( Object obj ) {
if ( !(obj instanceof OperatingRoom) )
return false ;
return this.id == ((OperatingRoom)obj).id;
}
}
class Bed extends Facility implements Comparable<Bed> {
final int id;
Bed( int id ) {
this.id = id;
}
@Override
public int compareTo( Bed other ) {
return this.id-other.id;
}
@Override
public String toString() {
return String.format("%-4s %2d %7d %6.2f","Bed",id+1,timeLoad,timeLoad/(end-start+0.00)*100);
}
@Override
public boolean equals( Object obj ) {
if ( !(obj instanceof Bed) )
return false ;
return this.id == ((Bed)obj).id;
}
}
class Event implements Comparable<Event> {
Marker obj;
int time;
ET type;
@Override
public int compareTo( Event other ) {
return time-other.time;
}
Event( Marker obj, int time, ET type ) {
this.obj = obj;
this.time = time;
this.type = type;
}
}
class FancyComp implements Comparator<Patient> {
@Override
public int compare( Patient a, Patient b ) {
if ( a.operationStart+a.minutesForSurgery == b.operationStart+b.minutesForSurgery ) {
if ( a.operationStart == b.operationStart )
return a.id-b.id;
return a.whichRoom-b.whichRoom;
}
assert false;
return a.operationStart+a.minutesForSurgery < b.operationStart+b.minutesForSurgery?-1:1;
}
}
Patient []p;
Bed []b;
OperatingRoom []r;
PriorityQueue<Event> pq = new PriorityQueue<>();
TreeSet<OperatingRoom> roomPool = new TreeSet<>();
TreeSet<Bed> bedPool = new TreeSet<>();
TreeSet<Patient> pool = new TreeSet<>();
TreeSet<Patient> immediateAttention = new TreeSet<>(new FancyComp());
void simulate() {
int curTime = -1;
Event e;
assert bedPool.isEmpty();
assert roomPool.isEmpty();
for ( total = pat, completed = 0; completed < total; ) {
assert !pq.isEmpty();
assert curTime < pq.peek().time;
for ( curTime = pq.peek().time; !pq.isEmpty() && curTime == pq.peek().time && (e = pq.poll()) != null; )
if ( e.type == ET.ROOM_READY )
roomPool.add((OperatingRoom)e.obj);
else if ( e.type == ET.BED_READY )
bedPool.add((Bed)e.obj);
else {
if ( e.type == ET.PATIENT_AFTER_RECOVERY )
++completed;
else if ( e.type == ET.PATIENT_RIGHT_AFTER_SURGERY )
immediateAttention.add((Patient) e.obj);
}
if ( completed == total ) break ;
assert immediateAttention.size() <= bedPool.size();
// for( ;!immediateAttention.isEmpty(); bedPool.pollFirst().admit(immediateAttention.pollFirst(),curTime+op2rec) );
while ( !immediateAttention.isEmpty() ) {
Patient pp = immediateAttention.pollFirst();
Bed pb = null;
for ( Bed bb: bedPool )
if ( bb.wasReadyAt <= pp.operationStart+pp.minutesForSurgery ) {
bb.admit(pp,curTime);
pb = bb;
break ;
}
assert pb != null;
bedPool.remove(pb);
}
for( ;!pool.isEmpty() && !roomPool.isEmpty(); roomPool.pollFirst().admit(pool.pollFirst(),curTime) );
}
}
void writeSoln() throws Exception {
bw.write(" Patient Operating Room Recovery Room\n");
bw.write(" # Name Room# Begin End Bed# Begin End\n");
bw.write(" ------------------------------------------------------\n");
for ( int i = 0; i < pat; bw.write(p[i++].toString()+"\n") );
bw.write("\nFacility Utilization\n");
bw.write("Type # Minutes % Used\n");
bw.write("-------------------------\n");
for ( int i = 0; i < m; bw.write(r[i++]+"\n") );
for ( int i = 0; i < n; bw.write(b[i++]+"\n") );
bw.flush();
}
void go() throws Exception {
int i,j,k;
Scanner scan = new Scanner(br.readLine());
m = scan.nextInt();
r = new OperatingRoom[m];
for ( i = 0; i < m; ++i ) r[i] = new OperatingRoom(i);
n = scan.nextInt();
b = new Bed[n];
for ( i = 0; i < n; ++i ) b[i] = new Bed(i);
start = scan.nextInt()*60;
op2rec = scan.nextInt();
prepOp = scan.nextInt();
prepRec = scan.nextInt();
pat = scan.nextInt();
p = new Patient[pat];
for ( j = 0; j < pat; ++j )
p[j] = new Patient(j);
for ( i = 0; i < m; ++i )
pq.add(new Event(r[i],start,ET.ROOM_READY));
for ( j = 0; j < n; ++j )
pq.add(new Event(b[j],start,ET.BED_READY));
for ( k = 0; k < pat; pool.add(p[k++]) );
simulate();
writeSoln();
}
}
`