(UVA) Argus - Solution
Link to the problem: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=633&page=show_problem&problem=3644
The solution below used a Priority Queue to solve this problem.
import java.io.*;
import java.util.*;
class Main {
public void process() throws NumberFormatException, IOException {
Scanner sc = new Scanner(System.in);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
Queue<Data> queue = new PriorityQueue<Data>();
String command = sc.next();
while (!command.equals("#")) {
int id = sc.nextInt();
int period = sc.nextInt();
queue.add(new Data(id, period, period));
command = sc.next();
}
int numQueries = sc.nextInt();
for (int i = 0; i < numQueries; i++) {
Data d = queue.poll();
bw.write(d.id+"\n");
queue.add(new Data(d.id, d.period+d.firstPeriod, d.firstPeriod));
}
bw.flush();
bw.close();
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
class Data implements Comparable<Data> {
int id;
int period;
int firstPeriod;
public Data(int id, int period, int firstPeriod) {
this.id = id;
this.period = period;
this.firstPeriod = firstPeriod;
}
public int compareTo(Data d) {
if (this.period-d.period == 0) {
return this.id-d.id;
}
return this.period-d.period;
}
}
The solution below used a Priority Queue to solve this problem.
import java.io.*;
import java.util.*;
class Main {
public void process() throws NumberFormatException, IOException {
Scanner sc = new Scanner(System.in);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
Queue<Data> queue = new PriorityQueue<Data>();
String command = sc.next();
while (!command.equals("#")) {
int id = sc.nextInt();
int period = sc.nextInt();
queue.add(new Data(id, period, period));
command = sc.next();
}
int numQueries = sc.nextInt();
for (int i = 0; i < numQueries; i++) {
Data d = queue.poll();
bw.write(d.id+"\n");
queue.add(new Data(d.id, d.period+d.firstPeriod, d.firstPeriod));
}
bw.flush();
bw.close();
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
class Data implements Comparable<Data> {
int id;
int period;
int firstPeriod;
public Data(int id, int period, int firstPeriod) {
this.id = id;
this.period = period;
this.firstPeriod = firstPeriod;
}
public int compareTo(Data d) {
if (this.period-d.period == 0) {
return this.id-d.id;
}
return this.period-d.period;
}
}
Comments
Post a Comment