跳到正文

个人记录

405J1R训练四

zuoyou66约 4 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 4 分钟

版本

  1. v1当前

值轮兵团站岗

思路

1.用模拟队列输入数据:

C++
for(int i=1;i<=n;i++){
		cin>>a[++wei];
}

2.为数据做排序:

代码
sort(a+tou,a+wei+1);

3.做1个t来存储操作,做每次查询;1.若指令为1,t+=x,做while循环从尾开始,条件:a[i]+t>范围k&&队列不为空,尾-1出队,2若指令为2,t-=x,做while循环从头开始,条件:a[i]-t<范围-k&&队列不为空,头+1出队3.输出尾-头+1:

C++
while(m--){
		int top;
		cin>>top;
		if(top==1){
			int x;
			cin>>x;
			t+=x;
			while(a[wei]+t>k&&wei-tou+1>0){
				wei--;
			}
		}else if(top==2){
			int x;
			cin>>x;
			t-=x;
			while(a[tou]+t<-k&&wei-tou+1>0){
				tou++;
			}
		}else{
			cout<<wei-tou+1<<"\n";
		}
	}

代码:

C++
#include<bits/stdc++.h>
using namespace std;
long long a[2000005];
long long tou=1;
long long wei=0;
int main(){
	int n,m,k;
	cin>>n>>m>>k;
	for(int i=1;i<=n;i++){
		cin>>a[++wei];
	}
	sort(a+tou,a+wei+1);
	long long t=0;
	while(m--){
		int top;
		cin>>top;
		if(top==1){
			int x;
			cin>>x;
			t+=x;
			while(a[wei]+t>k&&wei-tou+1>0){
				wei--;
			}
		}else if(top==2){
			int x;
			cin>>x;
			t-=x;
			while(a[tou]+t<-k&&wei-tou+1>0){
				tou++;
			}
		}else{
			cout<<wei-tou+1<<"\n";
		}
	}
	return 0;
}

交通管制

思路:

1.先输入数据:

C++
cin>>n;
	int m;
	cin>>m;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}

2.用输组b[i]存储两坑之间的距离:

C++
for(int i=1;i<n;i++){
		b[i]=a[i+1]-a[i]-1;
}

3.做一函数方便之后排序:

C++
bool ac(int a,int b){
	return a>b;
}

4.对b数组进行排序:

代码
sort(b+1,b+n,ac);

5.做循环从1开始到m-1,ans+=b[i]:

C++
for(int i=1;i<m;i++){
		ans+=b[i];
}

6.输出:a[n]-a[1]+1-ans:

代码
cout<<a[n]-a[1]+1-ans;

代码:

C++
#include<bits/stdc++.h>
using namespace std;
int a[15005];
int b[15005];
int n;
bool ac(int a,int b){
	return a>b;
}
int main(){
	cin>>n;
	int m;
	cin>>m;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	for(int i=1;i<n;i++){
		b[i]=a[i+1]-a[i]-1;
	}
	sort(b+1,b+n,ac);
	int ans=0;
	for(int i=1;i<m;i++){
		ans+=b[i];
	}
	cout<<a[n]-a[1]+1-ans;
	return 0;
}
原文