两个栈存储数据
#include<bits/stdc++.h>
#define maxnn 1000005
using namespace std;
typedef long long ll;
int n,m;
stack<int> s;//存储学生队列
stack<int> tmp;//辅助栈
int arr[1005];//辅助数组
void init(int n)//初始化1~n的排列
{
for(int i=1;i<=n;i++)
s.push(i);
}
void find(int a)//将要移动的位置前的学生编号压入辅助栈
{
while(!s.empty())
{
int x = s.top();
s.pop();
if(x==a)
{
return;
}
else
{
tmp.push(x);
}
}
}
int main()
{
cin>>n>>m;
init(n);
int a,b;
while(m--)
{
cin>>a>>b;
if(b>0)
{
find(a);
for(int i=0;i<b;i++)
{
int x = tmp.top();
tmp.pop();
s.push(x);
}
s.push(a);
while(!tmp.empty()){
int x = tmp.top();
tmp.pop();
s.push(x);
}
}
else if(b<0)
{
b = -b;
find(a);
for(int i=0;i<b;i++)
{
int x = s.top();
s.pop();
tmp.push(x);
}
s.push(a);
while(!tmp.empty()){
int x = tmp.top();
tmp.pop();
s.push(x);
}
}
}
for(int i=n-1;i>=0;i--)
{
arr[i] = s.top();
s.pop();
}
for(int i=0;i<n;i++)
{
cout<<arr[i]<<' ';
}
return 0;
}