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 <= n <= 100 0001≤n≤100000 and 1 <= k <=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 <= t[i] \<= 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.

 

题目大意是说现有一台服务器,最多同时处理k条指令,每个指令耗时1000ms,有K条输入指令,在第t[i]时刻输入第i条指令,问至少需要多少台服务器,才能不超载。

解题思路:只需要知道最忙碌时需要同时处理几条指令,除以k向上取整即可。(注意n=1的时候是特例)

#include<stdio.h>
#include<math.h>
main()
{
    long n,k,a[150000],count,max=0,now,i;
    while(scanf("%ld %ld",&n,&k)!=EOF)
    {
        max=0;
        for(i=0;i<n;i++)
        {
            scanf("%ld",&a[i]);
        }
            if(n==1)
        {
            printf("1\n");
            goto out1;
        }
        count=1,now=0;
        for(i=1;i<n;i++)
        {
            while(a[i]-a[now]>=1000)  //先出
            {
                count--;
                now++;
            }
            count++;           //后入
            if(max<count) max=count; //记录最大值
        }
        printf("%ld\n",(int)(ceil((double)max/k))); //向上取整
        out1:;
    }
}
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值