蒟蒻求助 30分代码求调
查看原帖
蒟蒻求助 30分代码求调
539764
Hane_Eat_Fruit楼主2022/12/3 19:39
#include <bits/stdc++.h>
using namespace std;
int read() {
	int x=0,f=1;
	char c=getchar();
	while(c<'0'||c>'9') {
		if(c=='-') f=-1;
		c=getchar();
	}
	while(c>='0'&&c<='9') x=(x<<3)+(x<<1)+(c^48),c=getchar();
	return x*f;
}

//void add(int u,int v,int w) {
//	e[++cnt].v=v;
//	e[cnt].w=w;
//	e[cnt].next=head[u];
//	head[u]=cnt;
//}
int n,c,a[100000];
int stk[100000];
int main(){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	ios::sync_with_stdio(false);
	cin>>n>>c;
	for(int i=1;i<=n;i++) {
		cin>>a[i];
	}
	int top=0,l=1,count=0;
	while(count+top<n&&l<=n) {
		int mini=1e9;
		int id=0;
		for(int i=l;i<l+c-top&&i<=n;i++) {
			if(a[i]<mini) {
				mini=a[i];
				id=i;	
			}
		}
		if(mini>stk[top]&&top>0) {
			cout<<stk[top]<<" ";
			top--;	
		} else {
			cout<<mini<<" ";
			for(int i=l;i<id;i++) {
				stk[++top]=a[i];
			}
			l=id+1;
		}
		count++;
	}
	while(top>0) {
		cout<<stk[top]<<" ";
		top--;
	}
	return 0;
}

2022/12/3 19:39
加载中...