题目传送门:点击打开链接
There are n servers in a laboratory, each of them can perform tasks. Each server has a unique id — integer from1 to n.
It is known that during the day q tasks will come, thei-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, anddi — the time needed to perform this task in seconds. Allti are distinct.
To perform the i-th task you need ki servers which are unoccupied in the secondti. After the servers begin to perform the task, each of them will be busy over the nextdi seconds. Thus, they will be busy in secondsti,?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 secondti 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.
The first line contains two positive integers n andq (1?≤?n?≤?100,1?≤?q?≤?105) — the number of servers and the number of tasks.
Next q lines contains three integers each, thei-th line contains integers ti, ki and di (1?≤?ti?≤?106,1?≤?ki?≤?n,1?≤?di?≤?1000) — the moment in seconds in which thei-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.
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.
4 3 1 3 2 2 2 1 3 4 3
6 -1 10
3 2 3 2 3 5 1 2
3 3
8 6 1 3 20 4 2 1 6 5 5 10 1 1 15 3 6 21 8 8
6 9 30 -1 15 36
In the first example in the second 1 the first task will come, it will be performed on the servers with ids1, 2 and 3 (the sum of the ids equals 6) during two seconds. In the second2 the second task will come, it will be ignored, because only the server4 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 ids1, 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 ids1 and 2 (the sum of the ids is3) 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.
题意:一个实验室里有n个实验员,他们编号从1-n。他们每个人都能参与实验项目。
现在已知今天实验室迎来了q项任务。
每个任务都有三个属性:ti,ki,di。
ti代表这个任务开始的时间
ki代表这个任务需要的人数
di代表这个任务持续的时间
一个员工在工作时间内只能从事一项实验项目
输出:输出每个任务对应的人员的id的和。比如一个任务由编号为1,2,3的人来完成。那么这个任务的id和为1+2+3=6
思路:这题我用了最简单的模拟方法。我设置了一个数组来记录每个工作人员的忙碌情况:比如vis[1]==3 代表id为3的员工还要工作3个单位时间。 当vis[id]==0 代表编号为id的员工处于闲置状态。
入果一个工作需要的工作人员数目大于当前闲置的人数,则这个任务无法完成。输出-1
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define m(a) memset(a,0,sizeof(a))
#define ss(a,b) ((b-a+1)*(a+b))/2
struct ssr
{int ti,ki,di;
} task[100005];///结构体储存数据int vis[105];///记录工作情况int main()
{int n,q;while(~scanf("%d %d",&n,&q)){m(task);m(vis);for(int i=1; i<=q; i++){scanf("%d %d %d",&task[i].ti,&task[i].ki,&task[i].di);}int sum=0;///记录id的和int sum2;int temp=0;///记录当前空闲人员的数量for(int i=1; i<=q; i++){sum=0;sum2=0;if(i!=1){for(int j=1; j<=n; j++){if(vis[j]){vis[j]-=(task[i].ti-task[i-1].ti);///确定每个工作人员的数目if(vis[j]<0){vis[j]=0;}}}}temp=0;for(int j=1; j<=n; j++){if(vis[j]==0){temp++;}}if(temp<task[i].ki){printf("-1\n");continue;}for(int j=1; j<=n; j++){if(vis[j]==0){sum+=j;sum2++;///sum2记录已经决定调用的空闲人员的数量vis[j]+=task[i].di;if(sum2==task[i].ki)///当调用的人的数量等于需要的人的数量时,就可以停止本次实验的调用了{break;}}}printf("%d\n",sum);}}return 0;
}