Codeforces 669D 模拟

本文介绍了一个关于模拟男孩在舞蹈中通过跳转和交换位置的问题解决方法。初始时,男孩们按顺序与女孩配对形成圆圈。通过一系列指令(包括顺时针或逆时针跳转特定位置及奇偶位置交换),文章详细说明了如何计算出经过多次操作后男孩们的最终位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Little Artem and Dance
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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:

  1. Value x and some direction are announced, and all boys move x positions in the corresponding direction.
  2. 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.

Input

The first line of the input contains two integers n and q (2 ≤ n ≤ 1 000 0001 ≤ 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
input
6 3
1 2
2
1 2
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个男生,开始时候,第i个位置的男生的标号为i,环形的,有q次操作,输入一个t,如果t是1,再输入一个x,所有男生顺时针转x个位置,如果x为负值,则逆时针,如果t是2,则把位置i上的男生和位置i+1的男生交换(i=1,3,5...n-1),n一定是偶数,问你经过q次操作后,最终的序列是多少。



做法:因为交换时候,所有奇数交换是相对的,偶数也一样,所以只要算出来1,2的位置,就能够推出来其余数的位置。



#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
int num[1000005];
int main(){
	int n,q,i,t,j,x,a=1,b=2;
	scanf("%d%d",&n,&q);
	for(j=1;j<=q;j++){
		scanf("%d",&t);
		if(t==1){
			scanf("%d",&x);
			a+=x;
			while(a>n)a-=n;
			while(a<=0)a+=n;
			b+=x;
			while(b>n)b-=n;
			while(b<=0)b+=n;
		}
		else{
			if(a%2)a++;
			else a--;
			if(b%2)b++;
			else b--;
			if(a<=0)a=n;
			if(a>n)a=1;
			if(b<=0)b=n;
			if(b>n)b=1;
		}
	}
	int sum=0,o=1;
	while(sum<n/2){
		num[a]=o;
		a+=2;
		o+=2;
		a%=n;
		if(a==0)a=n;
		sum++;
	}
	sum=0,o=2;
	while(sum<n/2){
		num[b]=o;
		b+=2;
		o+=2;
		b%=n;
		if(b==0)b=n;
		sum++;
	}
	for(i=1;i<=n;i++){
		printf("%d ",num[i]);
	}
	printf("\n");
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值