D. Little Artem and Dance

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

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

思路如下

  • 题意:经过题目上的两种操作后,输出 从 第一个 女孩 到 最后一个 女孩 所对应的 男孩的编号。
  • 思路: 这一题我开始想的是 直接用 “环形双向链表”(应该是叫这个名字吧)进行模拟,但是由于链表访问中间某个位置的元素浪费时间太长了,造成时间超时了。。。。。 然后又决定 将一开始的模拟思路用 数组来实现模拟 环形双向链表 功能,这样可以快速的 解决 对中间元素的开素访问,但是遗憾的是还是 超时。。。。。

    然后就决定 去看看题解怎么写的,在别人的题解中得到了 方法:

    规律如下:
  1. 在奇数位置排列的相对位置是不会变的:XX 1 XX 3 XX 5 XX 7XX;

    在偶数位置排列的相对位置也是不变的:XX 2 XX 4 XX 6 XX 8 XX.
  2. 所有的 奇数位置 总是 在所有的偶数位置 前边 或 后边,而且 相邻的奇数与偶数之间的位置 总是相邻的(如果把位置形成一个环)

    原因:经过操作 1 之后显然 奇数之间的相对的位置是不会改变的,偶数也同理,而且 相邻的奇数与偶数之间的位置 总是相邻的(如果把位置形成一个环)

    经过操作 2 之后,所有相邻的奇、偶数位置进行交换,因此也不会破坏 规律1 :奇数之间、偶数之间的相对位置不改变 ;但是会 改变奇偶数位置之间的 相对位置(例如:之前所有相邻的的 偶数位置在 奇数位置的前边:2 、1、4、3、6 、5,现在则变成相反的情况:1、2、3、4、5、6 )。
  3. 在有了这些规律之后,我们在模拟的时候只需要模拟 奇数位置的第一个 b = 1 和 偶数位置的第一个数 a = 0 的变化情况,则最终得到的 a 、b 位置的值 为 1、2 其它的 位置 则只需要根据 规律1 :奇数之间、偶数之间的相对位置是不变的 、规律2: 相邻两个奇偶数位置之间总是 相邻的。就能把 数组中剩余位置的值 填补出来!

虽然前两个题解都超时了,但是还是可以从里面学到很多的知识的,尤其是练习了 对 链表 的使用

超时题解一

#include<iostream>
#include<cstdlib>
using namespace std; struct Node
{
char girl_data;
int data;
Node * last;
Node * next;
} * head; int main()
{
//freopen("test.txt","r",stdin);
head = (Node *)malloc(sizeof(Node));
head -> data = 1;
int n,q;
scanf("%d %d",&n,&q);
Node * normal;
Node * last_pos = head;
for(int i = 2; i <= n; i ++)
{
normal = (Node *)malloc(sizeof(Node));
normal -> data = i;
normal -> last = last_pos;
last_pos -> next = normal;
last_pos = normal;
}
head -> last = last_pos;
last_pos -> next = head; while(q --)
{
int type;
scanf("%d", &type); if(type == 1)
{
int step;
scanf("%d", &step);
if(step > 0)
{
Node * st_pos = head;
for(int i = 0; i < step; i ++)
{
st_pos = st_pos -> last;
}
head = st_pos;
}
else
{
Node * st_pos = head;
for(int i = 0; i < -step; i ++)
{
st_pos = st_pos -> next;
}
head = st_pos;
}
}
else
{
Node * pos = head;
for(int i = 1; i <= n; i ++)
{
pos = pos -> next;
if(i % 2 != 0)
{
int tem = pos -> data;
pos -> data = pos -> last -> data;
pos -> last -> data = tem;
}
}
}
} Node * pos = head;
for(int i = 1; i <= n; i ++)
{
printf("%d ",pos ->data);
pos = pos -> next;
} return 0;
}

超时题解二

#include<iostream>
using namespace std;
const int Len = 1000005;
int node[Len]; int main()
{
//freopen("T.txt","r",stdin);
int n,q;
scanf("%d %d",&n,&q); int head = 0;
for(int i = 0; i < n; i ++)
node[i] = i + 1; while(q --)
{
int type;
scanf("%d", &type);
if(type == 1)
{
int step;
scanf("%d",&step);
if(step >= 0)
{
head -= step;
if(head < 0)
head += n;
}
else
{
head -= step;
head %= n;
}
}
else
{
int st_pos = head;
for(int i = 1; i <= n/2; i ++)
{
int tem = node[st_pos];
node[st_pos] = node[(st_pos + 1) % n];
node[(st_pos + 1) % n] = tem;
st_pos += 2;
st_pos %= n;
}
}
}
for(int i = 1; i <= n; i ++)
{
printf("%d ",node[head]);
head ++;
head %= n;
} return 0;
}

正解

#include<iostream>
using namespace std;
const int Len = 1000005;
int node[Len]; int main()
{
// freopen("test.txt","r",stdin);
int a = 0,b = 1; //只对 “奇、偶前开头前两个位置” 进行模拟操作(⚠️ 注意:这里的 下标是从 0 位置开始的)
int n,q;
scanf("%d %d",&n,&q);
int type,step;
while(q --)
{
scanf("%d",&type);
if(type == 1)
{
scanf("%d",&step);
a += step; //根据 step 的正、负 进行 模拟顺时针、逆时针 移动
(a+=n) %= n; //处理以防止越界 b += step;
(b+=n) %= n;
}
else //相邻奇偶位置进行位置交换(奇数位置 变成 偶数位置 ;偶数位置 变成 奇数位置)
{
if(a % 2 == 0) //如果是 偶数 位置 +1 变成 奇数位置
{
a ++;
a %= n; //防止 越界
}
else //如果是 奇数 - 1 变 偶数位置
{
a --;
if(a < 0)
a += n;
} if(b % 2 == 0) //同理
{
b ++;
b %= n;
}
else
{
b --;
if(b < 0)
b += n;
}
}
} int zhi = 1;
//往数组里填充数字 (注意为什么这样填数是可行的? 因为 位置 a、b要么是相邻的的 要么是 一个在开头位置,一个在结尾位置,这样在通过取余数 就可以保证 填上连续的数字了)
for(int i = 0;i < n; i += 2)
{
node[(a + i) % n] = zhi;
node[(b + i) % n] = zhi + 1;
zhi += 2;
} for(int i = 0; i < n; i ++)
printf("%d ",node[i]); return 0;
}

D. Little Artem and Dance(带环模拟 + 规律)的更多相关文章

  1. Codeforces Round #348 (VK Cup 2016 Round 2, Div. 2 Edition) D. Little Artem and Dance 模拟

    D. Little Artem and Dance 题目连接: http://www.codeforces.com/contest/669/problem/D Description Little A ...

  2. lintcode:带环链表

    带环链表 给定一个链表,判断它是否有环. 解题 定义两个指针p1 p2 p1每次向前走一步 p2每次向前走两步 当p2能赶上p1的时候说明有环 /** * Definition for ListNod ...

  3. [PHP] 算法-请找出带环链表的环的入口结点的PHP实现

    给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null 1.找链表倒数第k个结点,输入一个链表,输出该链表中倒数第k个结点.第一个指针走(k-1)步,到达第k个节点,两个指针同时往后 ...

  4. [LintCode] Linked List Cycle(带环链表)

    描述 给定一个链表,判断它是否有环. 样例 给出 -21->10->4->5, tail connects to node index 1,返回 true. 这里解释下,题目的意思, ...

  5. HDU 4443 带环树形dp

    思路:如果只有一棵树这个问题很好解决,dp一次,然后再dfs一次往下压求答案就好啦,带环的话,考虑到环上的点不是 很多,可以暴力处理出环上的信息,然后最后一次dfs往下压求答案就好啦.细节比较多. # ...

  6. CodeForces 668B Little Artem and Dance

    B. Little Artem and Dance time limit per test 2 second memory limit per test 256 megabytes input sta ...

  7. D. Little Artem and Dance

    题目链接:http://codeforces.com/problemset/problem/669/D D. Little Artem and Dance time limit per test 2 ...

  8. codeforces 669D D. Little Artem and Dance(乱搞题)

    题目链接: D. Little Artem and Dance time limit per test 2 seconds memory limit per test 256 megabytes in ...

  9. Codeforces 669D Little Artem and Dance (胡搞 + 脑洞)

    题目链接: Codeforces 669D Little Artem and Dance 题目描述: 给一个从1到n的连续序列,有两种操作: 1:序列整体向后移动x个位置, 2:序列中相邻的奇偶位置互 ...

随机推荐

  1. hadoop之完全分布式集群配置(centos7)

    一.基础环境 现在我们有两台虚拟机了,再克隆两台: 克隆好之后需要做三件事:1.更改主机名称 2.修改ip地址 3.将ip地址和对应的主机号加入到/etc/hosts文件中 1.永久修改主机名 hos ...

  2. Jave基本数据类型

    基本类型,或者叫做内置类型,是JAVA中不同于类的特殊类型.它们是我们编程中使用最频繁的类型,因此面试题中也总少不了它们的身影,在这篇文章中我们将从面试中常考的几个方面来回顾一下与基本类型相关的知识. ...

  3. linux构建DHCP服务器

    1.DHCP(Dynamic Host Configuration Protocol,动态主机配置协议)是一个局域网的网络协议,使用UDP协议工作,主要用途:给内部网络或网络服务供应商自动分配IP地址 ...

  4. Linux系统简单文件操作命令

    项目 内容 作业课程归属 班级课程链接 作业要求 作业要求链接 学号-姓名 17041419-刘金林 作业学习目标 1)学习Linux的基本操作命令:2)在终端上运用命令行去实现基本文件操作 1.查看 ...

  5. 数据库介绍以及MySQL数据库的使用

    一 数据库介绍 1.1 数据库定义 数据库就是存储数据的仓库  本质上就是一套cs结构的TCP程序   客户端连接到服务器 向服务器发送指令  完成数据的操作 1.2  常见数据库 关系型数据库 就是 ...

  6. 【原创】(四)Linux进程调度-组调度及带宽控制

    背景 Read the fucking source code! --By 鲁迅 A picture is worth a thousand words. --By 高尔基 说明: Kernel版本: ...

  7. 使用JS检测自定义协议是否存在

    [该博客是拼接他人的,原因我们这边PC的开发人员问我,有没有关于js某个对象直接能检测手机或者电脑的自定义协议的,我上网搜了下,貌似移动端的解决比较多] 最终解决方案:还是需要github上面大神写的 ...

  8. vscode使用cnpm报错

     1.在wind10搜索框里输入 Windows PowerShell 进入这个界面 2.打开Windows PowerShell 之后 输入命令:set-ExecutjionPolicy Remot ...

  9. SQL的模糊查询(转载)

    本文由转载而来: 原文地址链接:http://www.cnblogs.com/GT_Andy/archive/2009/12/25/1921914.html 在进行数据库查询时,有完整查询和模糊查询之 ...

  10. LeetCode-使数组唯一的最小增量

    题目描述: 给定整数数组 A,每次 move 操作将会选择任意 A[i],并将其递增 1. 返回使 A 中的每个值都是唯一的最少操作次数. 示例: 输入:[1,2,2] 输出:1 解释:经过一次 mo ...