NCPC 2015 October 10, 2015 Problem D
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的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 2015年10个最佳Web开发JavaScript库
2015年10个最佳Web开发JavaScript库 现在的互联网可谓是无所不有,有大量的JavaScript项目开发工具充斥于网络中.我们可以参考网上的指导来获取构建代码项目的各种必要信息.如果你是 ...
- http://browniefed.com/blog/2015/09/10/the-shapes-of-react-native/
http://browniefed.com/blog/2015/09/10/the-shapes-of-react-native/
- 2015.12.29~2015.12.30真题回顾!-- HTML5学堂
2015.12.29~2015.12.30真题回顾!-- HTML5学堂 吃饭,能够解决饥饿,提供身体运作机能.练习就像吃饭,强壮自己,提升编程技能,寻求编程技巧的最佳捷径!吃饭不能停,练习同样不能停 ...
- 2015.12.21~2015.12.24真题回顾!-- HTML5学堂
2015.12.21~2015.12.24真题回顾!-- HTML5学堂 山不在高,有仙则名!水不在深,有龙则灵!千里冰封,非一日之寒!IT之路,须厚积薄发!一日一小练,功成不是梦!小小技巧,尽在HT ...
- Visual Studio 2015和.Net 2015 预览版在线安装和ISO镜像安装光盘下载
微软刚刚宣布了 Visual Studio 2015和.Net 2015 预览版,并同时提供了下载. 微软在纽约正进行中的#Connect# 全球开发者在线大会上宣布了Visual Studio 20 ...
- #VSTS日志# 2015/12/10 – 终于可以删除工作项了
最近的更新不少,废话少说,直接上干货 定制工作项字段 本周的更新后,所有的用户都可以在vsts上直接给工作项添加字段了,具体内容包括– 添加新字段(日期,字符串,整形,数字)– 字段显示位置配置– 过 ...
随机推荐
- 设置二级域名共享一级域名Cookie和删除共享Cookie
设置共享Cookie: 二级域名要想共享一级域名的cookie,只需要设置cookie.Domain = ".一级域名.com"; 删除共享Cookie: HttpCook ...
- Error: [ng:areq] Argument 'xxxx' is not a function, got undefined
"Error: [ng:areq] Argument 'keywords' is not a function, got undefined" 代码类似这样的: <div n ...
- ASP.NET原理分析
ASP.NET请求与处理全过程分析 1.用户向服务器的某IP端口发送请求,此端口通过Http.sys来管理,请求报文被Http.sys接收,Http.sys在注册表中找能处理这个请求类型的应用程序,最 ...
- 怎么看网站是否开启CDN加速?测试网站全国访问速度方法详解
注意域名,动静分离的网站,只对静态文件的域名做了cdn 怎么看网站有没开启CDN? 要看一个网站是否开启CDN,方法很简单,只要在不同的地区ping网址就可以,比如在山东济南ping www.jb51 ...
- C#GDI+图像处理
支持格式:BMP.GIF.JPEG.EXIF.PNG.TIFF.ICON.WMF.EMF等,几乎涵盖所有常用格式 图像类: Image类:Bitmap和Metafile的类提供功能的抽象基类. Met ...
- 又一个绝对棒的对话框插件fancybox v1.3.4
http://www.jsfoot.com/jquery/demo/2011-07-30/fancybox/index.html jquery插件:fancybox Fancybox的特点如下: ...
- PL/sql developer连接数据库的问题以及oracle数据库中文乱码的问题
今天第二次配置PL/sql developer,表示很蛋疼,昨天因为动了一个东西然后莫名其妙的就再也连接不了数据库,总是显示各种错误,我动的东西是因为中文会显示乱码,(因为我是用32位的PL/sql ...
- PHP中函数sprintf .vsprintf (占位符)
sprintf()格式化字符串写入一个变量中. vsprintf()格式化字符串些写入变量中. <?php $num1 = 123; $num2 = 456; $txt = vsprintf(& ...
- qt-5.6.0 移植之纯净的linux文件系统的建立
为什么要建立一个最纯净的文件系统,一开始是想在qt-4.8.5的文件系统基础之上加东西,慎重想了一下,这方法行不通,以为有很多东西不熟悉.干脆就自己建立一个. 步骤很简单: 一:下载一个bulidro ...
- CentOS 7更换 安装源
centos 安转源更换 一:切换到目录 cd /etc/yum.repos.d二: [备份] mv CentOS-Base.repo CentOS-Base.repo.bak三 : [下载] wg ...