HDU1873(队列应用)(一)

2014-11-24 08:56:37 · 作者: · 浏览: 0

[java]
package D0725;

import java.util.*;

public class HDU1873 {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n;
LinkedList list1;
LinkedList list2;
LinkedList list3;
while (sc.hasNext()) {
n = sc.nextInt();
list1 = new LinkedList();
list2 = new LinkedList();
list3 = new LinkedList();
int id = 1;
while (n-- > 0) {
String str = sc.next();
// in
if (str.charAt(0) == 'I') {
int docid = sc.nextInt();
int priority = sc.nextInt();
Patient p = new Patient(id++, docid, priority);
if (docid == 1) {
list1.add(p);
Collections.sort(list1);
} else if (docid == 2) {
list2.add(p);
Collections.sort(list2);
} else {
list3.add(p);
Collections.sort(list3);
}

} else {//out
int docid = sc.nextInt();
if (docid == 1) {
if (list1.isEmpty())
System.out.println("EMPTY");
else
System.out.println(list1.poll().id);
} else if (docid == 2) {
if (list2.isEmpty())
System.out.println("EMPTY");
else
System.out.println(list2.poll().id);
} else {
if (list3.isEmpty())
System.out.println("EMPTY");
else
System.out.println(list3.poll().id);
}

}

}
}
}

}

class Patient implements Comparable {
public int docId;
public int priority;
public int id;

public Patient(int id, int docId, int priority) {
this.id = id;
this.docId = docId;
this.priority = priority;
}

@Override
public int compareTo(Patient o) {
if (this.priority >= o.priority)
return -1;
return 1;
}

} [java]
package D0725;

import java.util.*;

public class HDU1873 {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n;
LinkedList list1;
LinkedList list2;
LinkedList list3;
while (sc.hasNext()) {
n = sc.nextInt();
list1 = new LinkedList();
list2 = new LinkedList();
list3 = new LinkedList();
int id = 1;
while (n-- > 0) {
String str = sc.next();
// in
if (str.charAt(0) == 'I') {
int docid = sc.nextInt();
int priority = sc.nextInt();
Patient p = new Patient(id++, docid, priority);
if (docid == 1) {
list1.add(p);
Collections.sort(list1);