codeforces 747 C. Servers 优先队列

C. Servers

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

There are n servers in a laboratory, each of them can perform tasks. Each server has a unique id — integer from 1 to n.

It is known that during the day q tasks will come, the i-th of them is characterized with three integers: ti — the moment in seconds in which the task will come, ki — the number of servers needed to perform it, and di — the time needed to perform this task in seconds. All ti are distinct.

To perform the i-th task you need ki servers which are unoccupied in the second ti. After the servers begin to perform the task, each of them will be busy over the next di seconds. Thus, they will be busy in seconds ti, ti + 1, ..., ti + di - 1. For performing the task, ki servers with the smallest ids will be chosen from all the unoccupied servers. If in the second ti there are not enough unoccupied servers, the task is ignored.

Write the program that determines which tasks will be performed and which will be ignored.

Input

The first line contains two positive integers n and q (1 ≤ n ≤ 100, 1 ≤ q ≤ 105) — the number of servers and the number of tasks.

Next q lines contains three integers each, the i-th line contains integers tiki and di (1 ≤ ti ≤ 106, 1 ≤ ki ≤ n, 1 ≤ di ≤ 1000) — the moment in seconds in which the i-th task will come, the number of servers needed to perform it, and the time needed to perform this task in seconds. The tasks are given in a chronological order and they will come in distinct seconds.

Output

Print q lines. If the i-th task will be performed by the servers, print in the i-th line the sum of servers' ids on which this task will be performed. Otherwise, print -1.

Examples

input

Copy

4 3
1 3 2
2 2 1
3 4 3

output

Copy

6
-1
10

input

Copy

3 2
3 2 3
5 1 2

output

Copy

3
3

input

Copy

8 6
1 3 20
4 2 1
6 5 5
10 1 1
15 3 6
21 8 8

output

Copy

6
9
30
-1
15
36

Note

In the first example in the second 1 the first task will come, it will be performed on the servers with ids 1, 2 and 3 (the sum of the ids equals 6) during two seconds. In the second 2 the second task will come, it will be ignored, because only the server 4 will be unoccupied at that second. In the second 3 the third task will come. By this time, servers with the ids 1, 2 and 3 will be unoccupied again, so the third task will be done on all the servers with the ids 1, 2, 3 and 4 (the sum of the ids is 10).

In the second example in the second 3 the first task will come, it will be performed on the servers with ids 1 and 2 (the sum of the ids is 3) during three seconds. In the second 5 the second task will come, it will be performed on the server 3, because the first two servers will be busy performing the first task.

 

题意:

你有q个任务和n个服务器,每个任务都有三个属性 ti ki di,ti是到达时间,ki是需要的服务器数目,di是服务时间

如果任务到达时没有足够的服务器为它服务,那么这个任务就被忽视

如果有足够的服务器,就从编号最小的服务器开始为它工作 任务结束时释放服务器

最后输出每个任务的服务器编号的总和

 

思路:

因为题目的输入时按照到达时间从小到大输入的,所以就每次输入一个任务时判断能否工作,如果可以工作就插入到优先队列中(按照结束时间从小到大排序),判断能否工作之前要先判断是否有工作结束,然后更改一下目前的服务器数。

TAT我觉得我写的很乱 暑假最后一次挣扎了 加油嘿!

 

#include <cstdio>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <iostream>
#include <queue>
#include <map>
#include <vector>
using namespace std;
const int N=111111;
struct node
{
    int en;
    int a[111];
    int flag;
    int sum;
    int ans;
    bool friend operator <(node A,node B) {
        return A.en>B.en;
    }
}s[N],head;
int vis[N];
int main()
{
    int i,j,n,q,st,cnt,time,cntt,qqq;
    scanf("%d%d",&n,&q);
    qqq=n;
    priority_queue<node> que;
    for(i=1;i<=q;i++)
    {

        scanf("%d%d%d",&st,&cnt,&time);
        if(!que.empty())
        {
            while(que.top().en<=st &&!que.empty())
            {
                head=que.top();
                for(j=1;j<=head.sum;j++)
                {
                    vis[head.a[j]]=0;
                }
                qqq+=head.sum;
                que.pop();
            }
        }
        if(cnt>qqq) s[i].flag=1;
        else
        {
            s[i].flag=2;
            s[i].ans=0;
            cntt=0;
            for(j=1;j<=n;j++)
            {
                if(cntt==cnt) break;
                if(!vis[j])
                {
                    vis[j]=1;
                    s[i].a[++cntt]=j;
                    s[i].ans+=j;
                }
            }
            s[i].sum=cntt;
            s[i].en=st+time;
            que.push(s[i]);
            qqq-=cntt;
        }
    }
    for(i=1;i<=q;i++)
    {
        if(s[i].flag==1) printf("-1\n");
        else printf("%d\n",s[i].ans);
    }
    return 0;
}



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值