平衡二分堆的使用

本文介绍了java.util.Timer类用于调度后台任务,TimerTask为任务。重点阐述了其内部任务队列TaskQueue使用的平衡二分堆算法,包括添加、获取最小任务、移除最小任务等操作,还提及防止内存泄漏的处理,以及维护堆不变性的方法。

java.util.Timer是一个用来调度后台任务的类,TimerTask就是一个任务。
里面用了一个平衡二分堆的算法:

/**
 * This class represents a timer task queue: a priority queue of TimerTasks,
 * ordered on nextExecutionTime.  Each Timer object has one of these, which it
 * shares with its TimerThread.  Internally this class uses a heap, which
 * offers log(n) performance for the add, removeMin and rescheduleMin
 * operations, and constant time performance for the the getMin operation.
 */
class TaskQueue {
    /**
     * Priority queue represented as a balanced binary heap: the two children
     * of queue[n] are queue[2*n] and queue[2*n+1].  The priority queue is
     * ordered on the nextExecutionTime field: The TimerTask with the lowest
     * nextExecutionTime is in queue[1] (assuming the queue is nonempty).  For
     * each node n in the heap, and each descendant of n, d,
     * n.nextExecutionTime <= d.nextExecutionTime.
     */
    private TimerTask[] queue = new TimerTask[128];

    /**
     * The number of tasks in the priority queue.  (The tasks are stored in
     * queue[1] up to queue[size]).
     */
    private int size = 0;

    /**
     * Adds a new task to the priority queue.
     */
    void add(TimerTask task) {
        // Grow backing store if necessary
        if (++size == queue.length) {
            TimerTask[] newQueue = new TimerTask[2*queue.length];
            System.arraycopy(queue, 0, newQueue, 0, size);
            queue = newQueue;
        }

        queue[size] = task;
        fixUp(size);
    }

    /**
     * Return the "head task" of the priority queue.  (The head task is an
     * task with the lowest nextExecutionTime.)
     */
    TimerTask getMin() {
        return queue[1];
    }

    /**
     * Remove the head task from the priority queue.
     */
    void removeMin() {
        queue[1] = queue[size];
        queue[size--] = null;  // Drop extra reference to prevent memory leak
        fixDown(1);
    }

    /**
     * Sets the nextExecutionTime associated with the head task to the
     * specified value, and adjusts priority queue accordingly.
     */
    void rescheduleMin(long newTime) {
        queue[1].nextExecutionTime = newTime;
        fixDown(1);
    }

    /**
     * Returns true if the priority queue contains no elements.
     */
    boolean isEmpty() {
        return size==0;
    }

    /**
     * Removes all elements from the priority queue.
     */
    void clear() {
        // Null out task references to prevent memory leak
        for (int i=1; i<=size; i++)
            queue[i] = null;

        size = 0;
    }

    /**
     * Establishes the heap invariant (described above) assuming the heap
     * satisfies the invariant except possibly for the leaf-node indexed by k
     * (which may have a nextExecutionTime less than its parent's).
     *
     * This method functions by "promoting" queue[k] up the hierarchy
     * (by swapping it with its parent) repeatedly until queue[k]'s
     * nextExecutionTime is greater than or equal to that of its parent.
     */
    private void fixUp(int k) {
        while (k > 1) {
            int j = k >> 1;
            if (queue[j].nextExecutionTime <= queue[k].nextExecutionTime)
                break;
            TimerTask tmp = queue[j];  queue[j] = queue[k]; queue[k] = tmp;
            k = j;
        }
    }

    /**
     * Establishes the heap invariant (described above) in the subtree
     * rooted at k, which is assumed to satisfy the heap invariant except
     * possibly for node k itself (which may have a nextExecutionTime greater
     * than its children's).
     *
     * This method functions by "demoting" queue[k] down the hierarchy
     * (by swapping it with its smaller child) repeatedly until queue[k]'s
     * nextExecutionTime is less than or equal to those of its children.
     */
    private void fixDown(int k) {
        int j;
        while ((j = k << 1) <= size) {
            if (j < size &&
                queue[j].nextExecutionTime > queue[j+1].nextExecutionTime)
                j++; // j indexes smallest kid
            if (queue[k].nextExecutionTime <= queue[j].nextExecutionTime)
                break;
            TimerTask tmp = queue[j];  queue[j] = queue[k]; queue[k] = tmp;
            k = j;
        }
    }
}

编写代码 当地动物园获得了一个大型开放式花园,动物可以像在自然栖息地一样自由活动,并用它们惯常的恶作剧来招待游客。 最受欢迎的动物是猴子。凭借他们的攀爬、跳跃和其他技能,他们让老游客和年轻游客都很开心。 有一种猴子擅长爬树和摘椰子。另一个物种专门砸开它们。 第一类猴子有N只(编号1至N),第二类猴子有M只(编号1至M)。 第一种类型的猴子k需要Ak秒在树上找到一个好的位置,然后摘下它的第一个椰子。之后,猴子每Bk秒摘一个新椰子。 第二种类型的猴子k花了Ck秒找到一个打开椰子的好工具,然后打开第一个椰子。之后,猴子每Dk秒摘一个椰子。 不幸的是,第二种猴子非常好斗,所以这两种猴子可能不会同时出现在花园里。因此,动物园管理员会在第一类猴子摘下所有椰子后立即将它们赶走。同样,如果同一类型的猴子在打开所有椰子后停留太久,就会发生争斗。因此,动物园管理员会在他们打开所有的椰子后立即将他们送走。 动物园管理员首先在摘完所有的椰子后立即赶到,然后在猴子们把椰子全部打开后立即赶走。猴子进出花园所需的时间也可以忽略不计。 托米斯拉夫特别喜欢第二种猴子,但他永远猜不到什么时候能看到它们。他知道猴子在花园里待的总时间,但不知道花园里有多少椰子,请帮助他计算第二类猴子到达的时间。 输入格式: 第一行包含整数T(1≤T≤1000),猴子在花园里的总时间,以秒为单位。 下一行包含整数N(1≤N≤100),第一类猴子的数量。 以下N行中的每一行都包含两个整数Ak和Bk(1≤Ak,Bk≤1000000000)表示第一种的猴子k的速度。 下一行包含整数M(1≤M≤100),第二类猴子的数量。 以下M行中的每一行都包含两个整数Ck和Dk(1≤Ck,Dk≤1000000000)表示第二种的猴子k的速度。 输出格式: 输出第一只猴子到达和第二只猴子到达中间间隔的秒数。 输入样例1: 12 1 3 1 1 5 1 输出样例1: 5 输入样例2: 20 2 3 2 1 3 3 3 1 4 1 5 1 输出样例2: 13 在第一个例子中,花园里有三个椰子: •第一种猴子在花园开放3秒钟后摘下第一颗椰子。 •花园开放4秒后,猴子摘下第二个椰子。 •花园开放5秒后,猴子摘下第三个椰子。 •动物园管理员进来护送猴子出去。第二种猴子来了。输出为5,因为这是Tomislav想要到达的时间。 •第二种猴子在花园开放10秒后打开第一个椰子。 •花园开放11秒后,猴子打开第二个椰子。 •花园开放12秒后,猴子打开第三个椰子。 •动物园管理员进来护送猴子出去。
06-14
由于所给引用内容未涉及背钻板厚分的相关信息,以下是结合专业知识对背钻板厚分的介绍。 背钻是一种用于印制电路板(PCB)制造的技术,主要用于消除多层板中不需要的通孔部分,以减少信号传输过程中的反射和损耗,提高信号完整性。背钻板厚分是在背钻工艺中对不同厚度的PCB进行分组的操作,其目的是为了提高生产效率、保证产品质量和优化工艺参数。 ### 分依据 - **板厚规格**:将具有相同或相近板厚的PCB归为同一。常见的PCB板厚规格有0.2mm、0.4mm、0.6mm、0.8mm、1.0mm、1.2mm、1.6mm、2.0mm等。不同的板厚在背钻过程中需要不同的钻孔深度和参数设置,按照板厚分可以确保每一的PCB在背钻时使用相对统一的工艺参数。 - **生产批量**:根据生产订单的批量大小进行分。对于大批量相同板厚的PCB,可以单独分为一进行连续生产,提高生产效率。而小批量不同板厚的PCB可以根据实际情况进行适当合并,但要注意工艺参数的调整。 - **板厚公差**:即使是同一规格的板厚,实际生产中也会存在一定的公差。将板厚公差相近的PCB分在同一,可以减少背钻过程中因板厚差异导致的钻孔深度误差。 ### 分方法 - **指定数量的分**:根据生产计划或设备的生产能力,指定每的PCB数量。例如,每固定放置50块板厚为1.6mm的PCB。这种方法适用于生产批量较大且稳定的情况,可以方便生产管理和工艺控制。 - **未指定数量的分**:根据板厚的分布情况进行分类,然后将相同板厚的PCB自由组合成。例如,将所有板厚为1.0mm的PCB归为一类,然后根据实际生产情况将它们分成若干。这种方法灵活性较高,适用于小批量、多品种的生产模式。 - **指定元素的分**:如果在分时,有特殊要求的PCB,如某些板厚需要特殊的背钻工艺或有特定的生产顺序要求,则先安排这些特殊要求的PCB,再将其他普通的PCB进行分。例如,对于一些有高精度信号要求的PCB,需要单独分并采用更严格的工艺参数进行背钻。 ### 分的重要性 - **提高生产效率**:通过合理的分,可以减少背钻设备在不同板厚之间切换工艺参数的时间,提高设备的利用率和生产效率。 - **保证产品质量**:同一的PCB板厚相近,背钻时可以使用相对统一的工艺参数,减少因板厚差异导致的钻孔深度误差,从而保证背钻质量的一致性。 - **优化工艺参数**:根据不同的板厚特点,可以针对性地优化背钻工艺参数,如钻孔速度、进给量、钻头直径等,提高背钻的精度和可靠性。 ### 代码示例(Python模拟指定数量的分) ```python # 假设这是所有PCB板厚的列表 pcb_thicknesses = [1.6, 1.6, 1.0, 1.0, 1.6, 1.2, 1.2, 1.0] # 指定每的数量 pile_size = 3 # 分函数 def divide_into_piles(thicknesses, size): piles = [] current_pile = [] for thickness in thicknesses: current_pile.append(thickness) if len(current_pile) == size: piles.append(current_pile) current_pile = [] if current_pile: piles.append(current_pile) return piles # 执行分 result_piles = divide_into_piles(pcb_thicknesses, pile_size) for i, pile in enumerate(result_piles): print(f"第 {i + 1} 的板厚: {pile}") ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值