错题:
T628213 值轮兵团站岗
错误原因:
去死嗑前面的题了,这题题目都没看。
正确思路:
这道题我们可以运用于队列相关的知识做。
先定义一个队头( )、队尾( ), 要从, 在结束。
int head=0,tail=-1,w;然后创建队列并进行排序。
for(int i=1;i<=n;i++){
cin>>w;
a[++tail]=w;
}
sort(a+head,a+tail+1);//注意这里的结点不是tail,而是tail+1。然后可以用 循环来做 次操作。
while(m--)//这里的的范围是3e5*3e5=9e10,不会超时。接下来是三种操作,但在之前,我们可以定义一个标记变量 ,方便记录 的总量。
int t=0;·当 :
if(opt==1){
cin>>x;
t+=x;
while(a[tail]+t>k && tail-head>-1){//这里表示当队列不为空时才会去减人数。
tail--;
}
}·当 :
if(opt==2){
cin>>x;
t-=x;
while(a[head]+t<-k && tail-head>-1){//与上面同理。
head++;
}
}·当 :
if(opt==3){
cout<<tail-head+1<<'\n';
}三种情况都做完了,但还有一个小细节,就是数据范围。 用 ,肯定会超,所以得开 。
正确代码:
#include<bits/stdc++.h>
using namespace std;
const int N=3e5+5;
long long n,m,k,opt,x;
long long a[N],head=0,tail=-1,w;
int main(){
cin>>n>>m>>k;
for(int i=1;i<=n;i++){
cin>>w;
a[++tail]=w;
}
sort(a+head,a+tail+1);
long long t=0;
while(m--){
cin>>opt;
if(opt==1){
cin>>x;
t+=x;
while(a[tail]+t>k && tail-head>-1){
tail--;
}
}
if(opt==2){
cin>>x;
t-=x;
while(a[head]+t<-k && tail-head>-1){
head++;
}
}
if(opt==3){
cout<<tail-head+1<<'\n';
}
}
return 0;
}