暑假刷题第6天--7/16

128. 编辑器 - AcWing题库(对顶栈)--与对顶堆类似

#include<iostream>
#include<cstring>
#include<stack>
using namespace std;
int sum[1000006]={0},f[1000006];
int main(){
	stack<int>a,b;
	memset(f,-0x3f,sizeof(f));
	int Q;
	cin>>Q;
	while(Q--){
		char c;
		cin>>c;
		if(c=='I'||c=='Q'){
			int q;
			cin>>q;
			if(c=='I'){
				a.push(q);
				int k=a.size();
				sum[a.size()]=sum[a.size()-1]+q;
				f[a.size()]=max(f[a.size()-1],sum[a.size()]);
			}
			else {
				cout<<f[q]<<"\n";
			}
		}
		else {
			if(c=='L'&&!a.empty()){
				b.push(a.top());
				a.pop();
			}
			else if(c=='R'&&!b.empty()){
				a.push(b.top());
				sum[a.size()]=sum[a.size()-1]+b.top();
				f[a.size()]=max(f[a.size()-1],sum[a.size()]);
				b.pop();
			}
			else if(c=='D'){
				if(!a.empty()){
					a.pop();
				}
			}
		}
	}
}

129. 火车进栈 - AcWing题库(搜索)

#include<iostream>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<vector>
using namespace std;
vector<int>ans;
stack<int>q;
int t=1;
int n,cnt;
void dfs(int x){
	if(x==n+1){
		if(++cnt>20){
			exit(0);
		}
		for(auto it:ans){
			cout<<it;
		}
		stack<int>a;
		while(!q.empty()){
			cout<<q.top();
			a.push(q.top());
			q.pop();
		}
		while(!a.empty()){
			q.push(a.top());
			a.pop();
		}
		cout<<endl;
		return;
	}
	if(!q.empty()){
		ans.push_back(q.top());
		q.pop();
		dfs(x);
		q.push(ans.back());
		ans.pop_back();
	}
	q.push(x);
	dfs(x+1);
	q.pop();
}
int main(){
	cin>>n;
	dfs(1);
} 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值