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

本篇博客介绍了一个使用Java实现的程序设计案例,该案例通过读取输入数据并进行排序等操作来解决特定问题。程序中运用了优先队列、字符串解析、文件读取等多种Java编程技术。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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--;
			}
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值