跳到正文

个人记录

T628213 值轮兵团站岗

rgjjy约 2 分钟
原文
文章信息与版本

文章信息

发布
2025年7月8日
收录
2025年12月12日
更新
2026年11月17日
篇幅
约 2 分钟

版本

  1. v1当前

题目

T628213 值轮兵团站岗

错误原因:

不知代码怎么写

思路

1.定义变量
2.出栈
C++
if(opt==1)
		{
			cin>>x;
			sum+=x;
			while(a[tail]+sum>k && tail-head+1>0)
			{
				tail--;
			}
		}
3.入栈
C++
if(opt==2)
		{
			cin>>x;
			sum-=x;
			while(a[head]+sum<-k && tail-head+1>0)
			{
				head++;
			}
		}
4.数数还有多少人
C++
if(opt==3)
		{
			cout<<tail-head+1<<endl;
		}

正确代码

C++
#include<bits/stdc++.h>
using namespace std;
const int N=3e5+5;
long long n,m,k,a[N],tail=-1,head;
long long sum,x,opt;
int main()
{
	cin>>n>>m>>k;
	for(int i=1;i<=n;i++)
	{
		cin>>a[++tail];
	}
	sort(a,a+n);
	while(m--)
	{
		cin>>opt;
		if(opt==1)
		{
			cin>>x;
			sum+=x;
			while(a[tail]+sum>k && tail-head+1>0)
			{
				tail--;
			}
		}
		if(opt==2)
		{
			cin>>x;
			sum-=x;
			while(a[head]+sum<-k && tail-head+1>0)
			{
				head++;
			}
		}
		if(opt==3)
		{
			cout<<tail-head+1<<endl;
		}
	}
	return 0;
} 
原文