Little Artem is fond of dancing. Most of all dances Artem likes rueda — Cuban dance that is danced by pairs of boys and girls forming a circle and dancing together.
More detailed, there are n pairs of boys and girls standing in a circle. Initially, boy number 1 dances with a girl number 1, boy number 2 dances with a girl number 2and so on. Girls are numbered in the clockwise order. During the dance different moves are announced and all pairs perform this moves. While performing moves boys move along the circle, while girls always stay at their initial position. For the purpose of this problem we consider two different types of moves:
- Value x and some direction are announced, and all boys move x positions in the corresponding direction.
- Boys dancing with even-indexed girls swap positions with boys who are dancing with odd-indexed girls. That is the one who was dancing with the girl 1 swaps with the one who was dancing with the girl number 2, while the one who was dancing with girl number 3 swaps with the one who was dancing with the girl number 4 and so one. It's guaranteed that n is even.
Your task is to determine the final position of each boy.
Input
The first line of the input contains two integers n and q (2 ≤ n ≤ 1 000 000, 1 ≤ q ≤ 2 000 000) — the number of couples in the rueda and the number of commands to perform, respectively. It's guaranteed that n is even.
Next q lines contain the descriptions of the commands. Each command has type as the integer 1 or 2 first. Command of the first type is given as x ( - n ≤ x ≤ n), where 0 ≤ x ≤ n means all boys moves x girls in clockwise direction, while - x means all boys move x positions in counter-clockwise direction. There is no other input for commands of the second type.
Output
Output n integers, the i-th of them should be equal to the index of boy the i-th girl is dancing with after performing all q moves.
Examples
6 3
1 2
2
1 2
4 3 6 5 2 1
2 3
1 1
2
1 -2
1 2
4 2
2
1 3
1 4 3 2
题意:有n对男女围成一个圈跳舞,初始状态时,按顺时针给他们标上1-n号,男生1号和女生1号跳舞,男生2号和女生2号.....男生n号和女生n号。然后就下来有2种操作,操作1是所有男生移动 i 个位置,若 i 为负数,则向顺时针移动i个位置;
否则,操作2是相邻的两个男生交换位置。在所有操作中,牛生是不动的。问,在一系列操作之后,女生1-n对应的男生分别是多少号。
思路:在移动的过程中,一个数左右的数虽然在不断的改变,但是可以发现一点,所有奇数的相对位置不会改变,所有偶数的相对位置不会改变。
因为如果是移动一定的位置,是不会改变所有数的相对位置的;而如果是交换,则奇数之间是同步交换的,而偶数之间也是同步交换的。
所以,不管如何操作,一个奇数左边的第一个奇数和右边的第一个奇数都是不会改变的,偶数也是如此。(即相对位置没有改变)。
所以,这题的解法就明了了:我们可以记录下数字1和2的位置,开始时,数字1就在第一位,数字2就在第二位,若他们移动,标记也跟着移动。
确定了1和2的位置之后,就能推出所有数的位置了。
代码:
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<string> 5 #include<cmath> 6 #include<algorithm> 7 #include<stack> 8 #include<queue> 9 #define eps 1e-7 10 #define ll long long 11 #define inf 0x3f3f3f3f 12 #define pi 3.141592653589793238462643383279 13 using namespace std; 14 int ans[1000006]; 15 int main() 16 { 17 int n,m,oper,far; 18 while(cin>>n>>m) 19 { 20 int sum = 0; 21 int flag1 = 0,flag2 = 1; //flag1标记1的位置(初始在第一个位置 0),flag2标记2的位置(初始在第二个位置1) 22 for(int i=0; i<m; ++i) 23 { 24 scanf("%d",&oper); 25 if(oper == 1) //如果是操作1,表示是平移 26 { 27 scanf("%d",&far); 28 flag1 += far; //标记也平移 29 flag2 += far; 30 flag1 = (flag1%n + n)%n; //为防止超过范围,要进行取模 31 flag2 = (flag2%n + n)%n; 32 } 33 else //操作2是交换 34 { 35 if(flag1%2==0) flag1++; //若 1 在偶数位置,则说明是和右边的交换 36 else flag1--; //否则和左边交换(下标从 0 开始) 37 38 if(flag2%2==0) flag2++; // 2 同理 39 else flag2--; 40 41 flag1 = (flag1%n + n)%n; //取模防越界 42 flag2 = (flag2%n + n)%n; 43 } 44 } 45 for(int i=0; i<n; i+=2) //根据1,2的位置推出所有数的位置 46 { 47 ans[(flag1+i)%n] = i+1; 48 ans[(flag2+i)%n] = i+2; 49 } 50 for(int i=0; i<n; ++i) 51 printf("%d%c",ans[i],i==n-1?' ':' '); 52 } 53 return 0; 54 }