Bestcoder #3 -- task schedule

Source: Internet
Author: User
Task Schedule
Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 0 accepted submission (s): 0


Problem description
There is a machine and a worksheet for this machine. There are n tasks on the worksheet. The machine executes the I task at the Ti time. One task can be completed in one second.
There are m inquiries, and each query has a number Q, indicating that if there is a task request beyond the worksheet in the Q time, calculate when the task can be executed.
The machine always runs according to the worksheet. when the machine is idle, it immediately executes the task requests outside the worksheet.
Input
The first line of the input contains an integer T, indicating a total of T groups of test data.

For each group of test data:
The first row contains two numbers, N and M, indicating that there are n tasks and M inquiries in the worksheet;
The second row contains n different numbers T1, T2, T3.... TN, indicating that the machine executes the I-th task at the Ti time.
In the next m row, each row has a number Q, indicating that there is a task request out of the worksheet at Q time.

Note: m queries are irrelevant.

[Technical Specification]
1. T <= 50
2. 1 <= n, m <= 10 ^ 5
3. 1 <= Ti <= 2*10 ^ 5, 1 <= I <= N
4. 1 <= q <= 2*10 ^ 5
Output
For each query, calculate and output the time when the task can be executed. Each query outputs a row.
Sample Input
15 51 2 3 5 612345
Sample output
44447

 

AC: The simulation + priority queue is laborious and memory-consuming ).

//author: svtter//#include <iostream>#include <stdio.h>#include <string.h>#include <vector>#include <map>#include <algorithm>#include <queue>#include <cmath>#define INF 0xffffff#define lln long longusing namespace std;struct item{    int n;    int i;    bool operator < (const item &a)const    {        return n > a.n;    }};int m, n;int work[100001];int extra[100001];priority_queue <item> q;int main(){    int i, cur;    int t;    int t1;    int a;    item it, b;    freopen("test2", "r", stdin);    scanf("%d", &t);    while(t--)    {        scanf("%d%d", &n, &m);        for(i = 0; i < n; i++)            scanf("%d", &work[i]);        for(i = 0; i < m; i++)        {            scanf("%d", &it.n);            it.i = i;            q.push(it);        }        sort(work, work+n);        t1 = 0;        cur = 0;        while(!q.empty())        {            if(t1 != n)                a = work[t1];            else            {                while(!q.empty())                {                    b = q.top();                    if(b.n <= cur)                        extra[b.i] = cur;                    else                    {                        extra[b.i] = b.n;                    }                    q.pop();                }                break;            }            if(!q.empty())                b = q.top();            else                break;            if(a > b.n && a != cur)            {                if(b.i <= cur)                    extra[b.i] = cur;                else                    extra[b.i] = b.n;                q.pop();            }            else            {                cur = work[t1]+1;                t1++;            }        }        for(i = 0; i < m; i++)            printf("%d\n", extra[i]);        printf("\n");    }    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.