题目
You have n boxes in a line on the table numbered 1 … n from left to right. Your task is to simulate 4
kinds of commands:
• 1 X Y : move box X to the left to Y (ignore this if X is already the left of Y )
• 2 X Y : move box X to the right to Y (ignore this if X is already the right of Y )
• 3 X Y : swap box X and Y
• 4: reverse the whole line.
Commands are guaranteed to be valid, i.e. X will be not equal to Y .
For example, if n = 6, after executing 1 1 4, the line becomes 2 3 1 4 5 6. Then after executing
2 3 5, the line becomes 2 1 4 5 3 6. Then after executing 3 1 6, the line becomes 2 6 4 5 3 1.
Then after executing 4, then line becomes 1 3 5 4 6 2
Input
There will be at most 10 test cases. Each test case begins with a line containing 2 integers n, m
(1 ≤ n, m ≤ 100, 000). Each of the following m lines contain a command.
Output
For each test case, print the sum of numbers at odd-indexed positions. Positions are numbered 1 to n
from left to right.
Sample Input
6 4
1 1 4
2 3 5
3 1 6
4
6 3
1 1 4
2 3 5
3 1 6
100000 1
4
Sample Output
Case 1: 12
Case 2: 9
Case 3: 2500050000
解决方法:
其实也就是简单的模拟,只不过是用双向链表的方式进行的,然而这种双向链表由于数字本身的连续性,所以没有采用原始的前面节点,本节点值,后面节点的方式,而是采用了两个数组的方式,left和right,其中left[n]就是n这个值得坐节点的值,right[n]就是n的右边节点的值,然后为了后面方便的输出,所以设了0这个节点,这个节点的左边为最大值,右边为1,从而形成一个循环链表。
然后就是其中最重要的一点,对于小紫书上的解决方案是
void Link(int l,int r){
right[l]=r;
left[r]=l;
}
这样一个函数来解决1,2,3的的移动,直接节点连接,这样一个函数确实方便很多,也不用去慢慢分析到底两个数组的值会怎么变化,但是我还是采用了原始方法,直接分析每一个步骤会产生什么样的反应,以及每一个数字的移动会使左右节点怎么变化。然后慢慢的根据变化去改变两个数组的值。
**
重要的是在4这个操作上,这个操作一次就会导致两个数组的值全部变化,这会非常浪费时间,所以说会用另外的思路,设置个flag,当出现一次4时就flag至1,再次出现又置0,只是做个标志,到最后输出结果的时候再判断,因此最多做一次反转就可以(而且会发现后面根本不用做反转),但是由于4的出现会导致1和2的行为改变,所以得判断1和2改变成什么样了。
最后输出的时候根据flag决定时输出left还是right
代码
里面很多调试的代码在,可以尝试加入注释注解掉的内容,看看输出结果
#include <cstdio>
#include <iostream>
using namespace std;
const int maxn=1000000;
void cal(int qq);
int n;
int main(){
int qq=1;
while(scanf("%d",&n)==1){
cal(qq++);
}
return 0;
}
void cal(int qq){
int m;
int left[maxn],right[maxn];
scanf("%d",&m);
for(int i=1;i<=n;i++){
left[i]=i-1;
right[i]=(i+1)%(n+1);
}
left[0]=n;
right[0]=1;
int flag=0;
int cg;
while(m--){
scanf("%d",&cg);
if(cg==4){
flag=!flag;
continue;
}
else{
int x,y;
scanf("%d %d",&x,&y);
if(cg<3&&flag==1)
cg=3-cg;
if(cg==3){
if(x==y)
continue;
else if(right[x]==y||right[y]==x)
{
if(right[y]==x)
swap(x,y);
right[left[x]]=y;
left[right[y]]=x;
right[x]=right[y];
left[y]=left[x];
left[x]=y;
right[y]=x;
}
else{
right[left[x]]=y;
left[right[y]]=x;
left[right[x]]=y;
right[left[y]]=x;
swap(left[x], left[y]);
swap(right[x], right[y]);
}
}
else if(cg==2){
if(x==right[y])
continue;
else{
right[left[x]]=right[x];
left[right[x]]=left[x];
left[right[y]]=x;
right[x]=right[y];
left[x]=y;
right[y]=x;
}
}
else if(cg==1){
if(x==left[y])
continue;
else{
right[left[x]]=right[x];
left[right[x]]=left[x];
right[left[y]]=x;
left[x]=left[y];
right[x]=y;
left[y]=x;
}
}
}
}
int aa;
long long num=0;
aa=0;
if(flag==1){
for(int i=0;i<=n;i++){
//cout<<aa<<endl;
if(i%2==1)
num+=aa;
aa=left[aa];
//aa=left[aa];
}
}
else{
//cout<<"rigth"<<endl;
for(int i=0;i<=n;i++){
//cout<<"a:"<<aa<<endl;
if(i%2==1)
num+=aa;
//cout<<num<<endl;
aa=right[aa];
}
}
cout<<"Case "<<qq<<": "<<num<<endl;
}