队列(queue)

# include <iostream>
# include <queue>
# include <list>
# include <deque> 

using namespace std;


    队列(queue):
        先进先出

    定义:
        queue<int,deque<int> > q;
        queue<int,list<int> >  q;
        queue<int,vector<int> > q;  error  vector不能再两端进行操作 
    操作:
        q.empty(); 是否为空 
        q.size();  大小 
        q.front(); 查看队首元素 
        q.back();  查看队尾的元素 
        q.pop();  在队首删除数据,就是出队 
        q.push(item);  在队尾插入数据 

    队尾插入,队首删除 
    值可以在一端插入另一端删除 

NCPC2015-D Disastrous Downtime

https://nanti.jisuanke.com/t/28879

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_iti​ 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 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

题目描述:第一行输入两个数:需要处理的请求n、每个服务器每秒处理的个数k。以下n行:每个请求到来的时间(可能相同)。求至少需要的处理器个数。

思路:找1000ms时段里的最大请求数,再ans=(ans+k-1)/k

官方代码

#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdio>
using namespace std;

#define rep(i,a,b) for(__typeof(b) i=a; i<(b); ++i)

int main()
{
  int n, k; scanf("%d %d", &n, &k);
  queue<int> q;
  size_t res = 0;
  rep(i,0,n)
  {
    int x; scanf("%d", &x);
    q.push(x);
    while (q.front() <= q.back() - 1000) q.pop();
    res = max(res, (q.size() + k - 1) / k);
  }
  cout << res << endl;
}

我的代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
	int n,k;
	ll ans=1;
	ll a[100010];
	memset(a,-1,sizeof(a));
	scanf("%d%d",&n,&k);
	for(int i=0;i<n;i++) {
		scanf("%lld",&a[i]);
	}
	for(int i=0;i<n;i++) {
		ll sum=1;
		for(;a[i+sum]>=a[i]&&a[i+sum]<a[i]+1000;sum++);
			i+=sum;
			if(sum>ans) ans=sum;
	}
	printf("%lld\n",(ans+k-1)/k);
}

附加一个网上看的别人的,也很有意思:

#include <iostream>
#define N 102005
using namespace std;

int n,m;
int an[N];

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>m;
    for(int i=0;i<n;++i){
        int x;
        cin>>x;
        an[x]++;
    }
    int ans = 0;
    for(int i=0;i<100000;i++){
        for(int j = i+1;j<i+1000;j++){
            an[i]+=an[j];
        }
        ans = max(ans,an[i]);
    }
    if(ans%m==0){
        cout<<ans/m<<endl;
    }else{
        cout<<ans/m+1<<endl;
    }
    return 0;
} 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我爱吃狮子头

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值