L2-041 插松枝

模拟题一直是自己的噩梦,调了一上午才调出来这道题。

感觉模拟题做起来痛苦的主要原因在于:

1 题目需求布豪实现,题意晦涩

2 很难像其它算法题那样学到思路,出现错误,往往是一些细节问题导致的,并非思维和知识点上的错误,修正这些细节错误感觉很难学到东西。

代码如下:

#include<bits/stdc++.h>

using namespace std;

const int N = 100010,M= 110;

int n , m , k ; 

deque<int> q;
stack<int> stk;
stack<int> s;

void print(){
///	cout << "print :" << endl;
	stack<int> outpt;
	while(s.size()){
		int t = s.top();
		s.pop();
		outpt.push(t);
	}
	while(outpt.size()>1){
		int t = outpt.top();
		outpt.pop();
		cout << t << ' ' ;
	}
//	cout << outpt.size() << endl;
	int t = outpt.top();
	cout << t << endl ;
//	cout << outpt[outpt.size()-1] << endl;
}

int main (){
	cin >> n >> m >> k ;
	
	for(int i=1;i<=n;i++){
		int x ;
		cin >> x ; 
		q.push_back(x);
	}
	while(q.size()||stk.size()||s.size()){
	//	cout << q.size() << endl;
		int t;
		
	//
	//	cout << "q:" << q.size() << endl;
	//	cout <<"s:" <<  s.size() << endl;
	//	cout << "stk:" << stk.size() << endl;
	//	if(s.size()) cout << s.top() << endl;
		if(!stk.size()){
		//	cout << "0000" << endl;
			if(q.size()==0){
				print();
				continue;
			}

			t = q.front();
			//cout << q.size() << "test1" << endl;;
			q.pop_front();
			//cout << q.size() << "test2" << endl;
		//	cout << "t:" << t << endl;
			
			//q.pop_front();
			if(!s.size()){
				s.push(t);
			}else {
				if(t>s.top()){
					stk.push(t);
				}else{
					s.push(t);
				}	
			}
		}else{
		//	cout << 1111 << endl;
	//	cout << stk.size() << endl;
			t = stk.top();
			stk.pop();
			if(!s.size()) {
				s.push(t) ;
				continue;	
			}
						//cout << "debug not empty:"<< t << endl;
			if(t>s.top()){
				stk.push(t);
				if(q.size()==0){
					print();
					continue;
				}
				t = q.front();
				q.pop_front();
			//	if(t==15) cout << "debug15" << endl;
				if(t>s.top()){
					if(stk.size()==m){
						print();
//						cout << "end of print" << endl;
						q.push_front(t);
					}else{
						stk.push(t);					
					}
				}else{
					s.push(t);						
				}
			}else{
				s.push(t);
			}
		}
		//cout << "come here" << endl;
		if(s.size()==k){
			print();
		}	
	}
	
	
	return 0 ; 
} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值