CodeForces - 668B Little Artem and Dance 模拟水题

版权声明:转载于[email protected]的博客 https://blog.csdn.net/nuoyanli/article/details/89385470

题目来源:

https://cn.vjudge.net/contest/296029#problem/J

https://codeforces.com/contest/668/problem/B

Description

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 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 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.

Sample Input

6 3
1 2
2
1 2

Sample Output

4 3 6 5 2 1

Input

2 3
1 1
2
1 -2

Output

1 2

Input

4 2
2
1 3

Output

1 4 3 2

题意

给你n个数,一开始所有数的编号是1~n 

现在有两个操作:

第一个操作是所有数向左或者右移动k个位置(负左右正

第二个操作奇数和偶数的位置互换

思路:

比赛时候一直暴力时间复杂度每一次都是o(n) 而且n and q (2 ≤ n ≤ 1 000 000, 1 ≤ q ≤ 2 000 000) 肯定会tl,回来改了一下比赛代码不wa了tl了

百度过才知道比赛的时候没有想到的就是,奇数和偶数是相互间隔的所以其相对位置是不会变的,也就是说我们只要知道1和2这两个位置的数是啥就好了,那么交换的时候,我们只需要模拟一下这两个位置的交换就好了QAQ.....

参考代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int inf = 0x3f3f3f3f;
int n,q,a,b;
int main()
{
    scanf("%d%d",&n,&q);
    b=0,a=0;//a是1的位置b是2的位置
    for(int i=1; i<=q; i++)
    {
        int op;
        scanf("%d",&op);
        if(op==1)
        {
            int k;
            scanf("%d",&k);
            a = (n+a-k)%n;
            b = (n+b-k)%n;
            if(k%2)swap(a,b);
        }
        else
        {
            a = (a+n-1)%n;
            b = (b+n+1)%n;
            swap(a,b);
        }
    }
    for(int i=1; i<=n; i++)
    {
        if(i%2)
            printf("%d ",(a+i-1+n)%n+1);
        else
            printf("%d ",(b+i-1+n)%n+1);
    }
   printf("\n");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/nuoyanli/article/details/89385470