读书人

HDU1509(行列应用)

发布时间: 2012-08-02 11:35:26 作者: rapoo

HDU1509(队列应用)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1509

package D0725;import java.io.*;import java.util.Collections;import java.util.LinkedList;public class HDU1509 {public static void main(String[] args) throws IOException {StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));LinkedList<Msg> list = new LinkedList<Msg>();String order;int id = 1;while (st.nextToken() != StreamTokenizer.TT_EOF) {order = st.sval;if (order.equals("GET")) {if (list.isEmpty())System.out.println("EMPTY QUEUE!");else {Collections.sort(list);Msg msg = list.poll(); System.out.println(msg.name + " " + msg.arg);}} else {// putst.nextToken();String name = st.sval;st.nextToken();int arg = (int) st.nval;st.nextToken();int priority = (int) st.nval;Msg msg = new Msg(id++, name, arg, priority);list.add(msg);}}}}class Msg implements Comparable<Msg> {public int id;public String name;public int arg;public int priority;public Msg(int id, String name, int arg, int priority) {this.id = id;this.name = name;this.arg = arg;this.priority = priority;}@Overridepublic int compareTo(Msg o) {if (priority > o.priority)return 1;else if (priority < o.priority)return -1;else {if (id < o.id)return -1;elsereturn 1;}}}


读书人网 >编程

热点推荐