uva1203 - Argus (排序、优先级队列)

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.Arrays;
import java.util.PriorityQueue;

class Main
{
	public static final boolean DEBUG = false;	
	public static int N = 1010;
	public static Register[] instruction = new Register[N];
	public static int n;
	
	static class Register implements Comparable<Register>
	{
		int num;
		int period;
		int cnt;
		
		public int compareTo(Register other)
		{
			if (period != other.period) return period - other.period;
			
			return num - other.num;
		}
	}
	
	static class Node implements Comparable<Node>
	{
		int num;
		int v;
		
		public int compareTo(Node other)
		{
			if (v != other.v) return v - other.v;
			
			return num - other.num;
		}
	}
	
	public static void main(String[] args) throws IOException
	{
		BufferedReader cin;
		String s;
		int k;
		
		if (DEBUG) {
			cin = new BufferedReader(new FileReader("d:\\OJ\\uva_in.txt"));
		} else {
			cin = new BufferedReader(new InputStreamReader(System.in));
		}
		
		n = 0;
		while ((s = cin.readLine()) != null) {
			if ("#".equals(s)) break;
			
			StringTokenizer st = new StringTokenizer(s);
			int cnt = 0;
			
			instruction[n] = new Register();
			instruction[n].cnt = 0;
			
			while (st.hasMoreTokens()) {
				String tmp = st.nextToken();
				if (cnt == 1) {
					instruction[n].num = Integer.parseInt(tmp);
				} else if (cnt == 2) {
					instruction[n].period = Integer.parseInt(tmp);
				}
				
				cnt++;
			}
			n++;
		}
		
		s = cin.readLine();
		k = Integer.parseInt(s);
		
		Arrays.sort(instruction, 0, n);
		
		int outputcnt = k;
		int i = 0;
		
		while (i < k) {
			int start = instruction[0].period * instruction[0].cnt;
			int end = start + instruction[0].period;
			
			PriorityQueue<Node> pq = new PriorityQueue<Node>();
			
			for (int j = 0; j < n; j++) {
				int cur = instruction[j].period * (instruction[j].cnt + 1);
				
				if (cur >= start && cur <= end) {
					i++;
					Node tmp = new Node();
					tmp.num = instruction[j].num;
					tmp.v = cur;
					instruction[j].cnt++;
					pq.add(tmp);
				}
			}
			
			while (!pq.isEmpty() && outputcnt > 0) {
				System.out.println(pq.poll().num);
				outputcnt--;
			}
		}
	}
}

你可能感兴趣的:(uva1203 - Argus (排序、优先级队列))