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上直接给工作项添加字段了,具体内容包括– 添加新字段(日期,字符串,整形,数字)– 字段显示位置配置– 过 ...
随机推荐
- pyspider安装
官方文档上说的比较简单: pip install pyspider 但是实际安装时还是有些问题导致无法成功. windows下安装 先安装PhantomJS 可以依照自己的开发平台选择不同的包进行下载 ...
- HDOJ 3709 Balanced Number
数位DP... Balanced Number Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java ...
- Cotex-M3内核STM32F10XX系列时钟及其配置方法
一.背景 最近做个项目,需要使用STM32,还是以前一样的观点,时钟就是MCU心脏,供血即时钟频率输出,想要弄明白一个MCU,时钟是一个非常好的切入点.言归正传,网上已经有太多大神详述过STM32的详 ...
- POJ2104 —— K-th number
1.题目大意:区间第k小,什么修改没有... 2.分析:这个是可持久化线段树,也是主席树,解释一下,n个线段树是怎么存下的,就是每一颗线段树和前一个有logn个点不一样 然后我们只需要一个线段树开lo ...
- Unity手游之路<十二>手游资源热更新策略探讨
http://blog.csdn.net/janeky/article/details/17666409 上一次我们学习了如何将资源进行打包.这次就可以用上场了,我们来探讨一下手游资源的增量更新策略. ...
- metasploit连接数据库
表示一直出问题.不造哪里出错.望有知情人与本人取得联系! test
- Unity3D获取Andorid设备返回键,主页键等功能
在Unity开发中捕捉Android的常用事件其实很简单 在新建的脚本文件中就加入: 比如: // 返回键 if ( Application.platform == RuntimePlatform.A ...
- Android四种基本布局(LinearLayout \ RelativeLayout \ FrameLayout \ TableLayout)
------------------------------------------LinearLayout---------------------------------------------- ...
- SNMP报文抓取与分析(一)
SNMP报文抓取与分析(一) 1.抓取SNMP报文 SNMP报文的形式大致如下图所示 我们这里使用netcat这个工具来抓取snmp的PDU(协议数据单元).(因为我们并不需要前面的IP和UDP首部) ...
- 安装rabbitmq以及python调用rabbitmq--暂欠
一.安装erlang yum install erlang 二.安装rabbitmq rpm包: wget http://www.rabbitmq.com/releases/rabbitmq-serv ...