zoukankan      html  css  js  c++  java
  • CROC 2016

    B. Processing Queries

    题目连接:

    http://www.codeforces.com/contest/644/problem/B

    Description

    In this problem you have to simulate the workflow of one-thread server. There are n queries to process, the i-th will be received at moment ti and needs to be processed for di units of time. All ti are guaranteed to be distinct.

    When a query appears server may react in three possible ways:

    If server is free and query queue is empty, then server immediately starts to process this query.
    If server is busy and there are less than b queries in the queue, then new query is added to the end of the queue.
    If server is busy and there are already b queries pending in the queue, then new query is just rejected and will never be processed.
    As soon as server finished to process some query, it picks new one from the queue (if it's not empty, of course). If a new query comes at some moment x, and the server finishes to process another query at exactly the same moment, we consider that first query is picked from the queue and only then new query appears.

    For each query find the moment when the server will finish to process it or print -1 if this query will be rejected.

    Input

    The first line of the input contains two integers n and b (1 ≤ n, b ≤ 200 000) — the number of queries and the maximum possible size of the query queue.

    Then follow n lines with queries descriptions (in chronological order). Each description consists of two integers ti and di (1 ≤ ti, di ≤ 109), where ti is the moment of time when the i-th query appears and di is the time server needs to process it. It is guaranteed that ti - 1 < ti for all i > 1.

    Output

    Print the sequence of n integers e1, e2, ..., en, where ei is the moment the server will finish to process the i-th query (queries are numbered in the order they appear in the input) or  - 1 if the corresponding query will be rejected.

    Sample Input

    5 1
    2 9
    4 8
    10 9
    15 2
    19 1

    Sample Output

    11 19 -1 21 22

    Hint

    题意

    有一个任务队列,你队列里面最多堆b个任务,如果新加入的任务的时候,任务队列已经满了的话,那这个任务就会被拒绝

    现在给你n个任务的加入时间和处理时间

    问你每个任务的完成情况是怎么样的。

    题解:

    模拟一下队列就好了

    用一个类似two point的思想去处理每一个任务。

    代码

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn = 1e6+7;
    
    long long a[maxn];
    int n,b;
    long long ans[maxn];
    int main()
    {
        scanf("%d%d",&n,&b);
        int st=0,ed=0;
        for(int i=1;i<=n;i++)
        {
            int x,y;scanf("%d%d",&x,&y);
            while(st<ed&&a[st]<=x)st++;
            if(st==ed)ans[i]=x+y,a[ed++]=ans[i];
            else if(ed-st<=b)ans[i]=a[ed-1]+y,a[ed++]=ans[i];
            else ans[i]=-1;
        }
        for(int i=1;i<=n;i++)
            cout<<ans[i]<<" ";
        cout<<endl;
    }
  • 相关阅读:
    JavaScript Array filter() 方法
    Object.assign方法的使用入门
    使用ES6新特性async await进行异步处理
    win10系统怎么设置软件开机启动
    【ES6学习笔记之】Object.assign()高级编程
    如何使用闭包形成计数器
    多次调用settimeout 如何使用单例模式
    在线表单设计器现在已经开源
    VisualStudio2017集成GitHub
    PHP使用curl替代file_get_contents
  • 原文地址:https://www.cnblogs.com/qscqesze/p/5297481.html
Copyright © 2011-2022 走看看