CodeForces 641C Little Artem and Dance (模拟)

http://codeforces.com/problemset/problem/641/C

题意:有n个男生和n个女生,各自形成一个环,刚开始时男女编号从1-n一一对应,之后有q次操作

1:x,如果x为正数,将所有男生按顺时针移动x位,若x为负,将所有男生按逆时针移动abs(x)位;

2:将1号2号男生调换....i号和i+1号男生调换,n-1号和n号男生调换

思路:用两个变量odd,even分别来记录奇数的编号移动几次,偶数的编号移动几次,1 3 5 ...是一样的情况;2 4 6...是一样的情况

#include <iostream>
#include <stdio.h>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
using namespace std;

typedef long long LL;
#define N 1100000
#define INF 0x3f3f3f3f
#define met(a, b) memset (a, b, sizeof(a))

int b[N];

int main ()
{
    int n, q, odd, even, p1, p2;

    while (scanf ("%d %d", &n, &q) != EOF)
    {
        met (b, 0);
        odd = 0, even = 1;

        while (q--)
        {
            int x, y;
            scanf ("%d", &x);
            if (x==1)
            {
                scanf ("%d", &y);
                odd = (odd + y + n) % n;
                even = (even + y + n) % n;
            }
            else
            {
                if (odd % 2)
                {
                    odd = (odd - 1 + n) % n;
                    even = (even + 1 + n) % n;
                }
                else
                {
                    odd = (odd + 1 + n) % n;
                    even = (even - 1 + n) % n;
                }
            }
        }

        int cnt = 1;
        while (cnt <= n)
        {
            b[odd] = cnt++;
            b[even] = cnt++;
            odd = (odd + 2 + n) % n;
            even = (even + 2 + n) % n;
        }

        for (int i=0; i<n-1; i++)
            printf ("%d ", b[i]);
        printf ("%d\n", b[n-1]);
    }
    return 0;
}


你可能感兴趣的:(CodeForces 641C Little Artem and Dance (模拟))