NCPC 2015
Problem D
Disastrous Downtime
Problem ID: downtime
Claus Rebler, cc-by-sa
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 oppor-
tunity 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 n 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
1000 milliseconds to process, and it must be processed right away.
Each server can work on at most k requests simultaneously. Given this limitation, can you
calculate the minimum number of servers needed to prevent another system breakdown?
Input
The first line contains two integers 1 ≤ n ≤ 100 000 and 1 ≤ k ≤ 100 000 , the number of
upcoming requests and the maximum number of requests per second that each server can handle.
Then follow n lines with one integer 0 ≤ t i ≤ 100 000 each, specifying that the i th request
will happen t i 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
Output a single integer on a single line: the minimum number of servers required to process all
the incoming requests, without another system breakdown.
Sample Input 1 Sample Output 1
2 1
0
1000
1
Sample Input 2 Sample Output 2
3 2
1000
1010
1999
2
NCPC 2015 Problem D: Disastrous Downtime

题意:n条请求,一个机器每秒内能处理k个,每个请求需要处理一秒。接下来是n个请求的时间,问至少要多少个机器才能保证不会有请求没被处理。

题解:我的想法是两个指针ij,i小j大,对于每个i来说找最小的j使得data[j]-data[i]>1000,则j-i为这一秒内需要处理的请求数,求出其中的最大值。一个机器能处理k个,算出需要多少个机器就行了。

还用一种做法是用数组存,遍历一次,也是相当于两个指针。

我的代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int data[];
int main()
{
int n,k,t;
while(scanf("%d%d",&n,&k)!=EOF)
{
int sum=;
memset(data,,sizeof(data));
for(int i=;i<n;i++)
scanf("%d",&data[i]);
sort(data,data+n);
int i,j;
for(i=,j=;i<n&&j<n;)
{
while(((data[j]-data[i])<)&&j<n) j++;
if(j==n) break;
sum=max(sum,j-i);
i++;
}
sum=max(sum,j-i);
//cout<<i<<j<<endl;
//cout<<sum<<" *****"<<endl;
int ans=sum/k; sum%=k;
if(sum%k) ans++;
printf("%d\n",ans);
}
return ;
}

另一种做法:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
int data[]; int main() {
int n,k,s;
scanf("%d%d",&n,&k);
memset(data,,sizeof(data));
for(int i=;i<n;i++) {
scanf("%d",&s);
data[s]++;
data[s+]--;
}
int sum=,ans=;
for(int i=;i<;i++) {
sum+=data[i];
ans = max(sum,ans);
}
if(ans%k==) printf("%d\n",ans/k);
else printf("%d\n",ans/k+);
}

NCPC 2015 October 10, 2015 Problem D的更多相关文章

  1. ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 D. Delay Time

    Problem D. Delay Time Input file: standard input Output file: standard output Time limit: 1 second M ...

  2. ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 G. Garden Gathering

    Problem G. Garden Gathering Input file: standard input Output file: standard output Time limit: 3 se ...

  3. ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 I. Illegal or Not?

    I. Illegal or Not? time limit per test 1 second memory limit per test 512 megabytes input standard i ...

  4. 2015年10个最佳Web开发JavaScript库

    2015年10个最佳Web开发JavaScript库 现在的互联网可谓是无所不有,有大量的JavaScript项目开发工具充斥于网络中.我们可以参考网上的指导来获取构建代码项目的各种必要信息.如果你是 ...

  5. http://browniefed.com/blog/2015/09/10/the-shapes-of-react-native/

    http://browniefed.com/blog/2015/09/10/the-shapes-of-react-native/

  6. 2015.12.29~2015.12.30真题回顾!-- HTML5学堂

    2015.12.29~2015.12.30真题回顾!-- HTML5学堂 吃饭,能够解决饥饿,提供身体运作机能.练习就像吃饭,强壮自己,提升编程技能,寻求编程技巧的最佳捷径!吃饭不能停,练习同样不能停 ...

  7. 2015.12.21~2015.12.24真题回顾!-- HTML5学堂

    2015.12.21~2015.12.24真题回顾!-- HTML5学堂 山不在高,有仙则名!水不在深,有龙则灵!千里冰封,非一日之寒!IT之路,须厚积薄发!一日一小练,功成不是梦!小小技巧,尽在HT ...

  8. Visual Studio 2015和.Net 2015 预览版在线安装和ISO镜像安装光盘下载

    微软刚刚宣布了 Visual Studio 2015和.Net 2015 预览版,并同时提供了下载. 微软在纽约正进行中的#Connect# 全球开发者在线大会上宣布了Visual Studio 20 ...

  9. #VSTS日志# 2015/12/10 – 终于可以删除工作项了

    最近的更新不少,废话少说,直接上干货 定制工作项字段 本周的更新后,所有的用户都可以在vsts上直接给工作项添加字段了,具体内容包括– 添加新字段(日期,字符串,整形,数字)– 字段显示位置配置– 过 ...

随机推荐

  1. 遍历一个类的属性--并转换为Dictionary类型

    参考地址...http://www.cnblogs.com/xwgli/p/3306297.html 记录点滴...以前很少用泛型...HaHa... /// <summary> /// ...

  2. linux配置java环境变量

    linux配置java环境变量(详细) 一. 解压安装jdk 在shell终端下进入jdk-6u14-linux-i586.bin文件所在目录, 执行命令 ./jdk-6u14-linux-i586. ...

  3. redis-string1

    package com.ztest.redis.string; import com.sun.istack.internal.logging.Logger;import com.ztest.redis ...

  4. Redis学习笔记三:多机数据库的实现

    1.复制 执行slaveof命令或者设置slaveof选项,让一个服务器去复制另外一个服务器. 旧版复制功能的实现(Redis 2.8 之前的版本) 复制功能分为同步和命令传播两个操作. 同步(syn ...

  5. HDU1102--最小生成树

    Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  6. HDU 4793 Collision(2013长沙区域赛现场赛C题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4793 解题报告:在一个平面上有一个圆形medal,半径为Rm,圆心为(0,0),同时有一个圆形范围圆心 ...

  7. Gunicorn 问题

    Does Gunicorn suffer from the thundering herd problem? The thundering herd problem occurs when many ...

  8. iOS项目目录结构

    一. 目前最为流行的目录结构是: <先根据模块后根据功能> 的文件目录结构 优点: 模块分明, 并且开发和维护时方便查阅各个功能 缺点: 可能会出现模块内随意建立文件夹, 导致局部逻辑紊乱 ...

  9. Android列表控件ListView详解

    ListView绝对可以称得上是Android中最常用的控件之一,几乎所有应用程序都会用到它. 由于手机屏幕空间都比较有限,能够一次性在屏幕上显示的内容并不多,当我们的程序中有大量的数据需要展示的时候 ...

  10. 2015安徽省赛 H.数7

    http://xcacm.hfut.edu.cn/problem.php?id=1212 模拟大发 #include<iostream> #include<cstdio> #i ...