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上直接给工作项添加字段了,具体内容包括– 添加新字段(日期,字符串,整形,数字)– 字段显示位置配置– 过 ...
随机推荐
- R语言 奇怪的NA
> 1+NA [1] NA > NA==1 [1] NA > c(NA,1:50) [1] NA 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...
- PHP基础 之 数组(一)
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- linux 模块加载
错误: rmmod 时提示 rmmod: chdir(xxx): No such file or directory 解决方法: http://blog.csdn.net/luckywang1103/ ...
- vncserver和Ubuntu Xfce4远程桌面环境的配置,解决不显示图形界面
vncserver和Ubuntu Xfce4远程桌面环境的配置 参考的http://blog.163.com/thinki_cao/blog/static/8394487520130301453180 ...
- BZOJ3172——[Tjoi2013]单词
1. 题目大意:一篇论文是由许多单词组成,现在想知道每个单词分别在论文中出现多少次. 2.分析:对着 广义后缀自动机的图看,我们就会发现玄机,答案不就是这个单词下的后缀个数吗? 于是建立自动机,然后求 ...
- BZOJ1861——book
就是给你一摞书,然后又询问第k大,编号为x的书是第几大,然后修改书的位置 splay模板题...然而我还是不会,,,又遇到lrj的书的坑了,rj的书里没有father,顿时弄得我ask不会搞了 注意合 ...
- hiho #1223 不等式
#1223 : 不等式 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定n个关于X的不等式,问最多有多少个成立. 每个不等式为如下的形式之一: X < C X ...
- java计算时间差
比如:现在是2016-03-26 13:31:40 过去是:2016-01-02 11:30:24 我现在要获得两个日期差,差的形式为:XX天XX小时XX分XX秒 方法一: DateFo ...
- java中类名,方法,变量,包名等大小写命名规范
类名:首字母大写,其他单词中首字母大写,其他小写方法名:首字母小写,其他单词中首字母大写,其他小写变量:与方法名规则同包名:全部小写接口interface:I开头
- 将 JAR 转为 EXE – JSMOOTH 的使用教程(第二期)(转载)
http://www.iteknical.com/convert-jar-to-exe-phase-ii-jsmooth-use-tutorial/