zoukankan      html  css  js  c++  java
  • Codeforces Round #387 (Div. 2) C. Servers

    C. Servers
    time limit per test2 seconds
    memory limit per test256 megabytes
    inputstandard input
    outputstandard 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 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.

    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 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.

    题意:有n个服务器,q个任务,每个任务有开始时间为ti,需要的服务器数量为ki,执行该任务需要di时间。输出q行,每行为执行该任务的服务器数量和,如果不能执行该任务,输出-1。
    题解:贪心,标记每个服务器被使用的情况。

    #include<stdio.h>
    #include<string.h>
    struct task
    {
        int t,k,d,sum;
    } a[100008];
    int main()
    {
        int n,q,i,j;
        int vis[108];
        bool check[108];
        while(scanf("%d%d",&n,&q)!=EOF)
        {
            memset(vis,0,sizeof(vis));
            memset(check,false,sizeof(check));
            for(i=0; i<q; i++)
            {
                scanf("%d%d%d",&a[i].t,&a[i].k,&a[i].d);
            }
            for(i=0; i<q; i++)
            {
                int sum=0,flag=0;
                for(j=1; j<=n; j++)
                {
                    if(a[i].t>vis[j])
                    {
                        vis[j]=0;
                    }
                    if(!vis[j])
                    {
                        vis[j]+=a[i].t+a[i].d-1;
                        sum+=j;
                        flag++;
                        check[j]=true;
                    }
                    if(flag==a[i].k)break;
                }
    //            for(int k=1; k<=n; k++)
    //            {
    //                printf("%d%c",vis[k],k==n?'
    ':'=');
    //            }
    
                if(sum==0||flag<a[i].k)
                {
                    printf("-1
    ");
                    for(j=1; j<=n; j++)
                    {
                        if(check[j])
                        {
                            vis[j]=0;
                        }
                    }
                    memset(check,false,sizeof(check));
                }
                else
                {
                    printf("%d
    ",sum);
                    memset(check,false,sizeof(check));
                }
            }
        }
    }
    
  • 相关阅读:
    服务控制器
    .Net Web项目安装包制作 (一)
    C#制作Windows service服务系列一:制作一个可安装、可启动、可停止、可卸载的Windows
    用Visual C#创建Windows服务程序
    插件的“动态替换”
    .Net Web项目安装包制作(三)补充说明
    .net打包/C#WinFrom程序打包
    C#制作Windows service服务系列三制作可控制界面的Windows服务(windows service)
    C#制作Windows service服务系列二演示一个定期执行的windows服务及调试
    .Net Web项目安装包制作 (二)数据库安装、其他组件的安装
  • 原文地址:https://www.cnblogs.com/kuronekonano/p/11794325.html
Copyright © 2011-2022 走看看