Codeforces Round #387 (Div. 2) Servers

115 篇文章 0 订阅
60 篇文章 1 订阅
Servers

原题链接:http://codeforces.com/problemset/problem/747/C

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 ≤ 1001 ≤ 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 ≤ 1061 ≤ ki ≤ n1 ≤ 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
4 3
1 3 2
2 2 1
3 4 3
output
6
-1
10
input
3 2
3 2 3
5 1 2
output
3
3
input
8 6
1 3 20
4 2 1
6 5 5
10 1 1
15 3 6
21 8 8
output
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 12 and 3 (the sum of the ids equals6) 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 12 and 3 will be unoccupied again, so the third task will be done on all the servers with the ids 123 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.

代码:

#include<stdio.h>
using namespace std;
int a[110];
int main()
{
    int n,m,i,j;
    scanf("%d%d",&n,&m);
    for(i=1; i<=m; i++)
    {
        int x,y,z;
        scanf("%d%d%d",&x,&y,&z);
        int k=0;
        for(j=1; j<=n; j++)
            if(x>=a[j])
                k++;
        if(k>=y)
        {
            k=0;
            int sum=0;
            for(j=1; j<=n; j++)
            {
                if(x>=a[j])
                {
                    sum+=j;
                    k++;
                    a[j]=x+z;
                }
                if(k==y)
                    break;
            }
            printf("%d\n",sum);
        }
        else
            printf("-1\n");
    }
    return 0;
}
ps:把下标当做服务器的编号,数组记录当前服务器服务结束的时间即处于空闲状态的开始时间,对于每次询问,先判断能不能执行任务,能的话再更新服务器的服务结束时间(每次遍历从一开始,使得结果取得最小的sum)
       ---------------------------------------------主要还是思想啊。。



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值