欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Nordic Collegiate Programming Contest 2015​--Disastrous Downtime

程序员文章站 2022-03-24 10:45:06
...

Nordic Collegiate Programming Contest 2015​--Disastrous Downtime

You're investigating what happened when one of your computer systems recently broke down. So far you've concluded that the system was overloaded; it looks like it couldn't handle the hailstorm of incoming requests. Since the incident, you have had ample opportunity to add more servers to your system, which would make it capable of handling more concurrent requests. However, you've simply been too lazy to do it—until now. Indeed, you shall add all the necessary servers . . . very soon!

To predict future requests to your system, you've reached out to the customers of your service, asking them for details on how they will use it in the near future. The response has been pretty impressive; your customers have sent you a list of the exact timestamp of every request they will ever make!

You have produced a list of all the nn upcoming requests specified in milliseconds. Whenever a request comes in, it will immediately be sent to one of your servers. A request will take exactly 10001000 milliseconds to process, and it must be processed right away.

Each server can work on at most kk requests simultaneously. Given this limitation, can you calculate the minimum number of servers needed to prevent another system breakdown?

Input Format

The first line contains two integers 1 \le n \le 100 0001≤n≤100000 and 1 \le k \le 100 0001≤k≤100000, the number of upcoming requests and the maximum number of requests per second that each server can handle.

Then follow nn lines with one integer 0 \le t_i \le 100 0000≤ti​≤100000 each, specifying that the ii-th request will happen t_iti​ milliseconds from the exact moment you notified your customers. The timestamps are sorted in chronological order. It is possible that several requests come in at the same time.

Output Format

Output a single integer on a single line: the minimum number of servers required to process all the incoming requests, without another system breakdown.

样例输入1

2 1
0
1000

样例输出1

1

样例输入2

3 2
1000
1010
1999

样例输出2

2

题目来源

Nordic Collegiate Programming Contest 2015​

题意:有 n 条指令,在同一时间内,一台机器只能同时执行 k 条指令,而且 每条指令需要执行 1000ms=1s 的时间才能执行完毕,            问总共最少需要多少台机器才能使每条指令都执行完。

题解:求出某时刻需要执行的最多指令条数,然后 进行 k 等分。

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a[110005]={0};
int main(){
    int n,k,t;
    scanf("%d %d",&n,&k);
    for(int i=0;i<n;i++){
        scanf("%d",&t);
        a[t]++;    
        a[t+1000]--;  // 每条指令执行完就除去
    }
    int sum=0,ans=0;
    for(int i=0;i<110000;i++){
        sum+=a[i];
        ans=max(sum,ans);  // 某时刻最多的 执行条数
    }
    printf("%d\n",ans%k==0?(ans/k):(ans/k+1));
    return 0;
}

 

相关标签: 思维