/*
For this lab you will be building the start of a very basic event simulation. Your program will read in lines of the form
event id period
for all of the events that your system is to track. The id of an event is a unique non-negative integer for identifying that event. The period is a positive integer for the number of ticks (the time unit of the simulation) between occurrences of the event.
Your program will also be given one command line argument n. Your program should print the identifiers (one per line) for the first n occurrences of events. If multiple events would occur simultaneously the event with the smallest id should be printed first.
*/
/*算是一个final version, 虽然并没有要求时间测试或者数据测试,但还是增加一个二分搜查,跑的更快,去找id所对应的period; 本来以为一个pq就能搞定,但还是要加一个数组。
/
import java.util.;
public class Main {
public static int BinarySearch(int[][] arr,int key) {
int low = 0;
int high = arr.length - 1;
int middle = 0;
if (key < arr[low][0] || key > arr[high][0] || low > high) {
return -1;
}
while (low <= high) {
middle = (low + high) / 2;
if (arr[middle][0] > key) {
high = middle - 1;
} else if (arr[middle][0] < key) {
low = middle + 1;
} else {
return middle;
}
}
return -1;
}
public static void main(String[] args) {
final int n = Integer.parseInt(args[0]);
Scanner scnr = new Scanner(System.in);
int[] answer = new int[n];
int n1 = 0;
ArrayList<Event> list = new ArrayList<>();
PriorityQueue<Event> pq = new PriorityQueue<>();
while (scnr.hasNextLine()&& scnr.hasNext()) {
int[] cao = new int[2];
scnr.next();
for (int k = 0; k < 2; k++) {
cao[k] = scnr.nextInt();
}
pq.add(new Event(cao[0], cao[1]));
list.add(new Event(cao[0], cao[1]));
}
int[][] Store = new int[pq.size()][2];
for (int var1 = 0;var1<pq.size();var1++) {
Store[var1][0]=list.get(var1).id;
Store[var1][1]=list.get(var1).period;
}
Arrays.sort(Store, Comparator.comparingInt(o -> o[0]));
for(int var3 = 0; var3<answer.length;var3++){
Event e1 = pq.poll();
answer[var3] = e1.id;
// System.out.println("Check the indexOut id: "+e1.id);
int k =BinarySearch(Store,e1.id);
//System.out.println("Check the indexOut : "+k);
int var2 = Store[k][1];
pq.add(new Event(e1.id,e1.period+var2));
}
for(int id: answer){
System.out.println(id);
}
}
}
class Event implements Comparable{
public int id;
public int period;
public Event(int id, int period){
this.id = id;
this.period = period;
}
@Override
public int compareTo(Event o) {
if(this.period<o.period){
return -1;
}
if(this.period==o.period){
if(this.id<o.id){
return -1;
}
else
return 1;
}
else
return 1;
}
}