求条一道团队题
  • 板块学术版
  • 楼主wsy_I
  • 当前回复0
  • 已保存回复0
  • 发布时间2025/2/7 09:42
  • 上次更新2025/2/7 11:51:22
查看原帖
求条一道团队题
1152799
wsy_I楼主2025/2/7 09:42

题面:

数列操作(一)

题目描述

先给定一个长度为n数列,再给定m个操作,现在需要维护五个操作:

1 x:在光标的前面插入一个数字x。

2:删除光标前的最后一个数字,如果光标前没有数字则忽略。

3:左移一格光标,如果光标已经在最左边则忽略。

4:右移一格光标,如果光标已经在最右边则忽略。

5 k:求前k个数的前缀和,保证k<=数列长度。

初始时光标在数列的最后一个数后面

输入格式

参照问题描述

输出格式

对于每个询问,输出一个答案,每个答案占一行

样例 #1

样例输入 #1

2
3 2
5
3
1 6
4
2
5 2

样例输出 #1

9

提示

数据范围:n,m,k<=100000,x<=int

我的代码:

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=300000+10;
long long st1[maxn],st2[maxn],top1,top2;
long long s1[maxn],s2[maxn],x,n,m,opt;
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>x;
		top1++;
		st1[top1]=x;
		s1[top1]=s1[top1-1]+x;
	}
	cin>>m;
	for(int i=1;i<=m;i++){
		cin>>opt;
		if(opt==1){
			cin>>x;
			top1++;
			st1[top1]=x;
			s1[top1]=s1[top1-1]+x;
		}
		else if(opt==2){
			if(top1>0) top1--;
		}
		else if(opt==3){
			if(top1>0){
				top2++;
				st2[top2]=st1[top1];
				s2[top2]=s2[top2-1]+s1[top1];
			}
		}
		else if(opt==4){
			if(top2>0){
				top1++;
				st1[top1]=st2[top2];
				s1[top1]=s1[top1-1]+st2[top2];
			}
		}
		else if(opt==5){
			int k;
			cin>>k;
			if(k<=top1) cout<<s1[k]<<endl;
			else{
				long long t;
				t=s1[top1]+s2[top2]+s2[top2+top1-k];
				cout<<t;
			} 
		}
	} 
	return 0;
}

思路是维护两个对顶栈 st1st1st2st2,以及两个站的前缀和 s1s1s2s2

2025/2/7 09:42
加载中...