求条
查看原帖
求条
1277599
ZZC_Lingyun楼主2025/1/15 16:31
#include<bits/stdc++.h>
using namespace std;
int n,c,f,m,stk[10005],top,a[10005];
int main(){
    cin>>n>>c;
    for(int i=1;i<=n;i++){
    	cin>>a[i];
	}
    while(m<n){
        if(f<n){
            int minn=INT_MAX,q;
            for(int i=f+1;i<=f+c-top&&i<=n;i++){
                if(a[i]<minn){
                	minn=a[i],q=i;
				}
            }
            if(top==0){
                for(int i=f+1;i<q;i++){
                	stk[++top]=a[i];
				}
				f=q;
                cout<<a[f]<<" ";
                m++;
            }else{
                if(stk[top]<a[q]){
                    cout<<stk[top]<<" ";
                    top--;
                    m++;
                }else{
                    for(int i=f+1;i<q;i++){
                    	stk[++top]=a[i];
					}
					f=q;
                    cout<<a[f]<<" ";
                    m++;
                }
            }
        }else{
            while(top){
            	cout<<stk[top--]<<" ";
				m++;
			}
        }
    }
    return 0;
}
2025/1/15 16:31
加载中...