View Code of Problem 1063

#include <cstdio>
#include <stack>

using namespace std;
stack<int>s1,s2,s3;
void clear(stack<int> s)
{
	while(s.size())
		s.pop();
}

int main()
{
	
	int n,r,p,c;
	while(scanf("%d %d",&n,&r) != EOF && (n || r)){
		clear(s1);
		clear(s2);
		clear(s3);
		for(int i = 1;i <= n;i++)
			s1.push(i);
		for(int i = 0;i < r;i++){
			scanf("%d %d",&p,&c);
			for(int j = 1;j < p;j++){
				s2.push(s1.top());
				s1.pop();
			}
			for(int j = 1;j <= c;j++){
				s3.push(s1.top());
				s1.pop();
			}
			for(int j = 1;j < p;j++){
				s1.push(s2.top());
				s2.pop();
			}
			for(int j = 1;j <= c;j++){
				s1.push(s3.top());
				s3.pop();
			}
		}
		
		printf("%d\n",s1.top());
	}
	return 0;
}

Double click to view unformatted code.


Back to problem 1063