社区讨论

20pts

P1750出栈序列参与者 1已保存回复 0

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
0 条
当前快照
1 份
快照标识符
@mhjohg5g
此快照首次捕获于
2025/11/04 05:54
4 个月前
此快照最后确认于
2025/11/04 05:54
4 个月前
查看原帖
CPP
#include<bits/stdc++.h>
using namespace std;
long long n,c,a[100050],cnt;
stack<long long> sta;
int main(){
	cin>>n>>c;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	long long tot=1,r=c,in=0;
	while(cnt+sta.size()<n){
		r=c-sta.size();
		long long mn=0x3f3f3f3f,id;
		for(int i=tot;i<=min(tot+1+r,n);i++){
			if(mn>a[i]){
				mn=a[i];
				id=i;
			}
		}
		if(sta.empty()||sta.top()>mn){
			for(int i=tot;i<=id;i++){
				sta.push(a[i]);
			}
			tot=id+1;
		}
		while(!sta.empty()&&sta.top()<=a[tot]){
			cout<<sta.top()<<" ";
			sta.pop();
			cnt++;
		}
		
	}
	while(!sta.empty()){
		cout<<sta.top()<<" ";
		sta.pop();
	}
	return 0;
}

回复

0 条回复,欢迎继续交流。

正在加载回复...