计蒜客暑假第一阶段第七场 d题

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

题目大意:

给你n个任务、每个任务执行开始的时间和每个服务器可以同时执行的任务数,让你计算最少需要多少台服务器,可以把这些任务执行完,执行一个任务需要1000秒。

思路:

我们算出每个任务开始后的1000秒内一共有多少个新的任务加入,然后找出新加入任务个数的峰值(最大值),然后除以每个服务器能够同时执行的任务个数,向上取整,就是所需要的服务器的个数。(注意,如果一个任务的开始时间是500,然后有一个任务的开始时间是1500,那么后者任务不属于前者任务开始后1000秒内新加入的任务)。

代码:

#include<bits/stdc++.h>
#define MAXN 100005
typedef long long ll;
using namespace std;
ll n,k;
ll root[MAXN];
int main()
{
    cin>>n>>k;
    for(int i=0;i<n;i++)
        cin>>root[i];
    sort(root,root+n);
    ll maxz=-1;
    ll i=0,j=0;
    for(i=0;i<n;i++)//从第一个任务开始遍历,找开始时间在1000秒之内任务的个数
    {
        ll minz=n-i;//这个地方是为了防止对于某个任务,剩余的所有任务都在该任务开始后的1000秒内
        for(;j<n;j++)//这个地方不对j重新设置初值,因为数组是排好序的
        {
            if((root[j]-root[i])>=1000)//因为是排好序的,所以只要找到一个开始时间不是在1000秒内的任务,就直接break就行
            {
                minz=j-i;
                break ;
            }
        }
        maxz=max(maxz,minz);
        if(j==n)//如果j等于n了,说明对于当前任务任务来说,剩余的所有任务的开始时间都在1000秒之内,所以之后的任务就不用遍历了,直接结束就行了
            break ;
    }
    if(maxz%k)
        cout<<(maxz/k)+1;//向上取整
    else
        cout<<maxz/k;
}

猜你喜欢

转载自blog.csdn.net/qq_40938077/article/details/81389731