Nordic Collegiate Programming Contest 2015 D. 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 \le n \le 100 0001≤n≤100000 and 1 \le k \le 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 \le t_i \le 100 0000≤ti≤100000 each, specifying that the ii-th request will happen t_itimilliseconds 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.
样例输入1
2 1 0 1000
样例输出1
1
样例输入2
3 2 1000 1010 1999
样例输出2
2
题目来源
Nordic Collegiate Programming Contest 2015
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <vector> 5 #include <string> 6 #include <string> 7 #include <map> 8 #include <cmath> 9 #include <algorithm> 10 using namespace std; 11 const int N=1e5+9; 12 int t[N]; 13 int n,k; 14 int MAX,ans;
//举个例子
// n=7 k=3
// 1000 1010 1090 2000 2080 2089 3010
// 3 3 4 3 3 2 1
// 1 1 2
15 int main() 16 { 17 scanf("%d%d",&n,&k); 18 for(int i=0;i<n;i++) 19 scanf("%d",&t[i]); 20 MAX=0; 21 int i,j; 22 for(i=0,j=0;i<n&&j<n;) 23 { 24 while(t[j]-t[i]<1000&&j<n) j++; 25 if(j==n) break; 26 MAX=max(MAX,j-i);//一秒应该处理的请求数目,大于k时,1个机器就不够了。 27 i++; 28 } 29 MAX=max(MAX,j-i); 30 ans=MAX/k; 31 if(MAX%k) ans++;//不能整除就必须要在加一个。 32 printf("%d\n",ans); 33 return 0; 34 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现