题目
思路
直接用栈颠来倒去容易超时
用队列front
和rear
的交换实现栈的转置
代码
#include<bits/stdc++.h>
using namespace std;
#define N 300003int a[N] = {
0};int main()
{
int q; cin>>q;int front = N/2-1, rear = N/2;for(int i = 0; i < q; i++){
int sig; cin>>sig;switch (sig){
case 1: // 进栈int x; cin>>x;if(front<rear){
a[front--] = x;cout<<a[front+1]<<endl;}else{
a[front++] = x;cout<<a[front-1]<<endl;}break;case 2: // 出栈if(front<rear){
a[++front] = 0;if(abs(front-rear)==1) // 队空cout<<-1<<endl;elsecout<<a[front+1]<<endl;}else{
a[--front] = 0;if(abs(front-rear)==1) // 队空cout<<-1<<endl;elsecout<<a[front-1]<<endl;}break;case 3: // 旋转if(abs(front-rear)==1) // 队空cout<<-1<<endl;else{
int tmp = front;front = rear;rear = tmp;if(front<rear)cout<<a[front+1]<<endl;elsecout<<a[front-1]<<endl;}break;default:break;}// if(front<rear)// {
// for(int k = front+1; k < rear; k++)// cout<<a[k]<<' ';// cout<<'*'<<endl;// }// else// {
// for(int k = rear+1; k < front; k++)// cout<<a[k]<<' ';// cout<<'%'<<endl;// }}system("pause");return 0;
}