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 2dances with a girl number 2 and 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 3swaps 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.
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 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.
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
一道思路题,数组非常大,应该想到不能纯模拟。之前做过一道移动数字的题,那题就是找一个数字的移动就知道整个的移动了,而这道题,只知道一个数字的移动,这个数字不是纯向前向后移动,而是加上之间穿插向前向后移动一位,就看1这个数字,移动了多少位之后坐标变成了奇数,那如果有“2”操作就要向后移动,它后面的数就要向前移动,就是说如果1移动到了偶数位,就要向前移动,它的移动影响到它旁边的数,开始2是他旁边的数,所以再查找2最后的移动位置。总之,移动的时候是都移动的一样,但是奇偶互换的时候是整体的奇数移动,整体的偶数移动,。
就是说开始全是奇数坐标的数,经过一番移动全变成了奇数或者偶数坐标,这时候奇偶互换的话,此时的所有奇数坐标的数都向后移动,所有的偶数坐标都向前移动,这时候就不是所有的数都移动的一样了,而是所有的奇数移动的一样,所有的偶数移动的一样,所以,这题要找一个奇数和一个偶数的最终位置,最终才能知道所有数的最终位置。那我就找1和2是怎么移动的啦!
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<queue>
#include<math.h>
using namespace std;
#define N 1000006
int c[N];
int main()
{
int n,m;
scanf("%d%d",&n,&m);
int opr,nn;
int a=1,b=2;
while(m--)
{
scanf("%d",&opr);
if(opr==1)
{
scanf("%d",&nn);
a=(a+nn)%n;
b=(b+nn)%n;
if(a<=0)
a+=n;
if(b<=0)
b+=n;
}
else if(opr==2)
{
if(a&1)
a++;
else
a--;
if(b&1)
b++;
else
b--;
}
}
for(int i=a,k=1; k<n; k+=2,i+=2)
{
int z=i%n;
if(z)
c[z]=k;
else
c[n]=k;
}
for(int i=b,k=2; k<=n; k+=2,i+=2)
{
int z=i%n;
if(z)
c[z]=k;
else
c[n]=k;
}
for(int i=1; i<n; i++)
printf("%d ",c[i]);
printf("%d\n",c[n]);
return 0;
}