zoukankan      html  css  js  c++  java
  • coderforces #387 Servers(模拟)

    Servers

    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard 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.

     【分析】可能有更简单的方法吧,我这个方法比较笨,直接循环模拟时间(天数)。

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <algorithm>
    #include <cmath>
    #include <string>
    #include <map>
    #include <stack>
    #include <queue>
    #include <vector>
    #define inf 10000000000000
    #define met(a,b) memset(a,b,sizeof a)
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    typedef long long ll;
    using namespace std;
    const int N = 1e6+5;
    const int M = 4e5+5;
    int n,m,k,tot=0;
    struct man{
        int k,d;
    }a[N];
    int main()
    {
        //int vis[1000000];
        int mark[105];
        met(mark,0);
        int s,t,t1,t2,p,dir;
        scanf("%d%d",&n,&m);
        while(m--){
            scanf("%d%d%d",&s,&t,&t1);
    
            a[s].k=t;a[s].d=t1;
        }
        tot=n;
        for(int i=1;i<=1000000;i++){
            //if(i==15)printf("!!!%d
    ",a[15].k);
            //printf("i=%d
    ",i);
            if(!a[i].k){
                tot=0;
                for(int j=1;j<=n;j++){
                    if(mark[j])mark[j]--;
                    if(mark[j]<1)tot++;
                    //printf("#%d
    ",j,mark[j]);
                }
                continue;
            }
            //printf("!!!%d %d
    ",i,tot);
            if(tot<a[i].k){
                printf("-1
    ");
                tot=0;
                for(int j=1;j<=n;j++){
                    if(mark[j])mark[j]--;
                    if(mark[j]<1)tot++;
                    //printf("#%d
    ",j,mark[j]);
                }
                continue;
            }
            else {
                tot=0;
                p=a[i].k;
                int sum=0;
                for(int j=1;j<=n;j++){
    
                    if(mark[j]==0&&p!=0){
                        mark[j]=a[i].d;
                        sum+=j;
                        p--;
                    }
                    if(mark[j])mark[j]--;
                    if(mark[j]<1)tot++;
                }
                printf("%d
    ",sum);
            }
        }
        return 0;
    }
  • 相关阅读:
    MSSQLSERVER数据库 C#里调用存储过程,多参数查询,个人记录
    ASP.NET GridView和Repeater合并单元格
    C# XPath教程
    MSSQLSERVER数据库 导入文本文件
    MSSQLSERVER数据库 递归查询例子
    C# TreeView右键弹出菜单
    tomcat 下War包部署方法
    JAVA自定义标签教程及实例代码
    JAVA tag学习
    Java Quartz 自动调度
  • 原文地址:https://www.cnblogs.com/jianrenfang/p/6197369.html
Copyright © 2011-2022 走看看