codeforces747C Servers(思维)

本文介绍了一种用于确定哪些任务能够被服务器执行的任务调度算法。该算法通过跟踪每台服务器的使用情况来决定是否接受新的任务请求,并计算所选服务器ID之和作为执行任务的标识。适用于有多台服务器并行处理不同任务需求的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.


思路:

用一个数组vis记录每一台机器上次在哪次任务使用的,然后每次任务扫一遍多少个机器可用,如果小于这次任务的要求数, 就输出-1.

否则就从小到大找出这次任务需要的机器数,相加。

代码:

#include <cstdio>
#include <cmath>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <cctype>
#include <sstream>
#define INF 0x3f3f3f3f
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
typedef long long LL;
using namespace std;
const int maxn=100000+5;

struct Node
{
    int b,e,num;
}a[maxn];
int main()
{
    //freopen("in.txt", "r", stdin);
    int n,q,vis[maxn]={0};
    scanf ("%d%d",&n,&q);
    int t,k,d;
    for (int i=1;i<=q;i++) {
        scanf ("%d%d%d",&t,&k,&d);
        a[i].b=t;a[i].e=t+d;a[i].num=k;
        int x=0;
        for (int j=1;j<=n;j++){
            if (!vis[j] || a[vis[j]].e<=a[i].b) x++;//!vis[j]是指从未使用过的
        }
        if (x<a[i].num) printf ("-1\n");
        else {
            int ans=0;
            for (int j=1,m=0;j<=n&&m<a[i].num;j++){
                if (!vis[j] || a[vis[j]].e<=a[i].b) {
                    m++;
                    vis[j]=i;
                    ans+=j;
                }
            }
            printf ("%d\n",ans);
        }
    }
    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值