Servers ---CodeForces
来源:互联网 发布:mysql client android 编辑:程序博客网 时间:2024/06/05 03:48
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 ti, ki 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.
Example
Input
4 31 3 22 2 13 4 3Output6-110Input3 23 2 35 1 2Output33Input8 61 3 204 2 16 5 510 1 115 3 621 8 8Output6930-11536
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.
思路:
直接模拟过程。
代码:
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <cstdlib>#include <cmath>using namespace std;int book[105];int people;int main(){ int N,Q; cin>>N>>Q; people = N; int T = 0; while(Q--){ int a,b,c; scanf("%d %d %d",&a,&b,&c); if(people<N){ int mid = a-T; for(int i=1 ; i<=N ; i++){ if(book[i] == 0)continue; book[i]-=mid; if(book[i]<0)book[i] = 0; if(book[i] == 0)people++; } } T = a; int flag = 0,sum = 0; if(people<b){ printf("-1\n"); continue; } for(int i=1 ; i<=N ; i++){ if(book[i] == 0){ flag++; sum += i; book[i] += c; people--; } if(flag == b)break; } printf("%d\n",sum); } return 0;}
阅读全文
0 0
- Servers ---CodeForces
- codeforces 747C Servers
- codeforces 747C Servers
- 【codeforces 747C】Servers
- 【50.00%】【codeforces 747C】Servers
- CodeForces 5A - Chat Servers Outgoing Traffic
- Codeforces Round #387 (Div. 2) C. Servers
- Codeforces Round #387 (Div. 2) Servers
- Codeforces Round #387 (Div. 2) C. Servers
- servers
- servers
- Servers
- Codeforces 5A Chat Servers Outgoing Traffic set的使用
- Codeforces Round #387(Div. 2)C. Servers【模拟】
- Codeforces Round #387 (Div. 2)C. Servers(模拟)
- Codeforces Round #387 (Div. 2)C. Servers(模拟)
- 【Codeforces 747 C Servers 】+ 思维 或 优先队列
- Codeforces Round #387 (Div. 2) 747C Servers
- linux iic驱动编程之二 向总线注册设备(1)
- RecyclerView 下拉刷新 上拉加载
- Java 语法
- 反射--动态代理机制
- 东南大学 崇志宏:面向研究生的深度学习快速入门
- Servers ---CodeForces
- 从httpd启动keystone
- Java作业-n阶方阵对角线元素之和
- 九度1149 字串计算
- Kafka 消息 Deliver guarantee
- k++与++k的总结
- UVa 297 Quadtrees
- 迈思德网关与百度最新版本物管理对接成功
- MATLAB学习过程(一)