CF初体验--Round #348,D

来源:互联网 发布:淘宝提前收款条件 编辑:程序博客网 时间:2024/05/17 13:08

D. Little Artem and Dance
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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 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.
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~n,有三种转换方式。一,左旋转。二,右旋转。三,位置1和位置2互换;位置3和位置4互换,以此类推。最后输出这组数据。
题解:
我的思路:1.q1[],q2[]储存变换方式的数据,2.旋转方式使用函数change(这是一个旋转的写法:首先,旋转可以看做是两段数据之间的变换,具体方式:两段数据分别做反转,再将整体做反转。例如:1234->12 34->21 43->3412,这样就实现了左旋转2位)3.互换,使用swap解决。
PS:最后这个方法失败了。当测试很大的数据的时候就time limit。。。估计是旋转方式需要对每一个数数据处理,太复杂。
AC方法:实际上,我们只需要确定1,2的位置就能找到整组数的排列方式,所以,所有的工作就是对于初始位于位置1,2的数字1,2的变换。
通过判断是否出现倒回(即从头一步之后到尾;从尾一步之后到头)分成两种情况
在有倒回的这种情况中,根据我的方法实际有效的移动的步数是q2[i]%n,这就出现了如果刚好回到原始点(即q2[i]%n==0)便不再实用我推的公式(这个就是自己被坑,调了好久才知道的地方),所以此情况再分成2种。

/*#include<iostream>#include<cstdio>using namespace std;void change(int *arr,int head,int bottem){for(;head<bottem;head++,bottem--){    int temp;    temp=arr[head];    arr[head]=arr[bottem];    arr[bottem]=temp;}}int main(){int n;int q;cin>>n>>q;int num[n+5];int q1[q+5];int q2[q+5];for(int i=1;i<=q;i++){scanf("%d",&q1[i]);if(q1[i]==1){scanf("%d",&q2[i]);}}for(int i=1;i<=n;i++){    num[i]=i;}for(int i=1;i<=q;i++){    if(q1[i]==1){        if(q2[i]>=0){            //向右移动            change(num,1,n-q2[i]);            change(num,n-q2[i]+1,n);            change(num,1,n);        }       if(q2[i]<0){        //向左移动        change(num,1,-q2[i]);        change(num,-q2[i]+1,n);        change(num,1,n);       }    }    else {//交换        for(int i=1;i<=n;){            swap(num[i],num[i+1]);            i+=2;        }    }}for(int i=1;i<=n;i++){    printf("%d ",num[i]);}}*/#include<iostream>#include<cstdio>using namespace std;int main(){int n;int q;cin>>n>>q;int num[n+5];int q1[q+5];int q2[q+5];for(int i=1;i<=q;i++){scanf("%d",&q1[i]);if(q1[i]==1){scanf("%d",&q2[i]);}}int index1=1;int index2=2;for(int i=1;i<=q;i++){    if(q1[i]==1){        if(q2[i]>=0){            if(index1+q2[i]<=n){index1+=q2[i];}            else {                if(q2[i]%n==0){;}                else{index1=index1+(q2[i]%n)-n;}            }            if(index2+q2[i]<=n){index2+=q2[i];}            else {                if(q2[i]%n==0){;}                else {index2=index2+(q2[i]%n)-n;}            }        }        if(q2[i]<0){            if(index2+q2[i]>=1){index2=q2[i]+index2;}            else {                if(-q2[i]%n==0){;}                else {index2=index2+n+(q2[i]%n);}            }            if(index1+q2[i]>=1){index1=q2[i]+index1;}            else {                if(-q2[i]%n==0){;}                else {index1=index1+n+(q2[i]%n);}            }        }    }    else{        if(index1%2==0){index1-=1;}        else{index1+=1;}        if(index2%2==0){index2-=1;}        else{index2+=1;}    }}num[index1]=1;num[index2]=2;for(int i=index1+2,j=index2+2,k=1;k<n/2;k++){    if(i>n)i-=n;    if(j>n)j-=n;    num[i]=2*k+1;    num[j]=2*k+2;    i+=2;    j+=2;}for(int i=1;i<=n;i++){    printf("%d ",num[i]);}}
0 0
原创粉丝点击