计蒜客暑假第一阶段第七场 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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值