zoj 2164 Hanafuda Shuffle

zoj 2164 Hanafuda Shuffle

/*
用栈直接模拟
非常方便! 
*/
#define LOCAL
#include<iostream>
#include<stack>
using namespace std;
int main()
{
#ifdef LOCAL
       freopen("input.txt","r",stdin);
       freopen("output.txt","w",stdout);
#endif
    
    
    int n,r,p,q,i;
    while(cin>>n>>r,n)
    {
         stack<int> a,b,c;
         for(i=1;i<=n;i++)
              a.push(i);
         while(r--)
         {
                cin>>p>>q;
                p--;
                while(p--)
                {
                      b.push(a.top());
                      a.pop();          
                }
                while(q--)
                {
                      c.push(a.top());
                      a.pop();          
                }          
                while(!b.empty())
                {
                      a.push(b.top());
                      b.pop();           
                }
                while(!c.empty())
                {
                      a.push(c.top());
                      c.pop();           
                }
         }
         cout<<a.top()<<endl;
                     
    }
    return 0;
}

发表回复