event simulation

本文介绍了一个基本事件模拟器的实现方案,程序读取事件ID及周期,使用优先队列和二分搜索来高效处理事件,确保按正确顺序输出前n个发生的事件。

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

}

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值