UVA1422-Processor (Binary + priority queue)

Source: Internet
Author: User

Question Link


There are n tasks, and each task must execute W of workload within [R, D] (the three variables are integers ). The processing speed of the processor can change. When the speed is S, the time required for executing a task with a workload of W is w/s. In addition, it does not have to be executed consecutively and can be divided into several blocks. The minimum value of the maximum speed of the processor during execution. The processor speed is any integer.


Idea: in fact, it is similar to minimizing the maximum value, that is, finding the minimum speed when each task is completed within a given time range. We can first sort the start time from small to large, and then enumerate the time. the start time is smaller than the current enumeration time, and the earlier the task is completed, the higher the priority. Then decide which task or task the time processor processes. Note that when the end time of the first element in the queue is less than the current enumerated time, this task is not completed within the specified time, so the speed is not enough.


#include <iostream>#include <cstdio>#include <cstring>#include <queue>#include <algorithm>using namespace std;const int N = 10000000;const int MAXN = 10005;struct Work{    int r, d, w;    friend bool operator < (Work a, Work b) {        return a.d > b.d;    }}work[MAXN];int n;int cmp(Work a, Work b) {    return a.r < b.r;}int judge(int mid) {    priority_queue<Work> q;    Work state;    int cnt = 0;    for (int i = 1; i <= 20000; i++) {        if (!q.empty()) {            state = q.top();            if (state.d < i) {                return false;              }        }        while (cnt < n && work[cnt].r + 1 <= i) {            q.push(work[cnt++]);         }        int sum = mid;        while (sum && !q.empty()) {            state = q.top();            q.pop();            if (sum < state.w) {                state.w -= sum;                sum = 0;                q.push(state);            }             else                 sum -= state.w;             if (cnt == n && q.empty())                 return true;        }      }    return false;}int main() {    int cas;    scanf("%d", &cas);    while (cas--) {        scanf("%d", &n);        for (int i = 0; i < n; i++)            scanf("%d%d%d", &work[i].r, &work[i].d, &work[i].w);        sort(work, work + n, cmp);        int L = 0, R = N, mid;        while (L < R) {            mid = L + (R - L) / 2;             if (judge(mid))                 R = mid;            else                L = mid + 1;         }           printf("%d\n", L);    }    return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.