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
链表的第二份例题,比上一个好理解多了…就是开两个链表罢了
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define Maxn 100005
using namespace std;
int lef[Maxn],rig[Maxn];
inline void Link (int L,int R) {
lef[R] = L; rig[L] = R;
}
/*
双向链表 分别存储左边是谁,右边是谁
Link函数的想法 参考紫书 刘老师的处理
我本来每次都在程序里面写 lef和rig的交换 那样倒是不错,不过太麻烦了...................
*/
int main(int argc,char *argv[]) {
int n,m,op,x,y,Cas = 0;
bool flag = 0;
while(scanf("%d %d",&n,&m) == 2) {
flag = 0;
for(int i=1; i<=n; i++)
lef[i] = i-1,rig[i] = i + 1;
rig[n] = 0; rig[0] = 1;lef[0] = n;
while(m--) {
scanf("%d",&op);
if(op == 4) flag = !flag;
else {
scanf("%d %d",&x ,&y);
if(op == 3 && rig[y] == x) swap(x,y);
if(op != 3 && flag) op = 3 - op ;
if(op == 1 && rig[x] == y) continue;//x就在y的左侧
if(op == 2 && rig[y] == x) continue;//x就在y的右侧
int LX = lef[x],RX = rig[x],LY = lef[y],RY = rig[y];
if(op == 1) {
Link(LX,RX); Link(LY,x);Link(x,y);
}
else if(op == 2) {
Link(LX,RX); Link(y,x); Link(x,RY);
}
else if(op == 3) {// 交换xy的位置
if(lef[y] == x){ Link(LX,y); Link(y,x); Link(x,RY); }//在交换之前 x。y是相邻的
else {
Link(LX,y); Link(y,RX);
Link(LY,x); Link(x,RY);
}
}
}
}
int num = 0;
long long Ans = 0;
for(int i=1; i<=n; i+=2) {
num = rig[num];
Ans += num;
num = rig[num];
}
if(flag && n % 2 == 0) Ans = (long long)n * (n+1) / 2 - Ans;
// n是奇数时 整条链反转多少次都没有用 原来奇数位置的还在奇数位置上
//n 是偶数时 整条链翻转奇数次(flag == true)原来奇数位置上的元素菜有所改变
printf("Case %d: %lld\n",++Cas,Ans);
}
return 0;
}
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#define Maxn 100005
#define LL long long
using namespace std;
int lef[Maxn],rig[Maxn];
inline void Link(int L,int R) { lef[R] = L; rig[L] = R; }
int main(int argc,char *argv[]) {
int n,m,kase = 0;
while(scanf("%d %d",&n,&m) == 2) {
for(int i=1; i<=n; i++){
lef[i] = i - 1; rig[i] = i + 1;
}
rig[n] = 0; lef[0] = n; rig[0] = 1;
int op,X,Y,flag = 0;
while(m--) {
scanf("%d",&op);
if(op == 4) flag = !flag;
else {
scanf("%d %d",&X,&Y);
if(op == 3 && rig[Y] == X) swap(X,Y);// 方便下面统一处理op==3的情况
if(op != 3 && flag) op = 3 - op;
if(op == 1 && lef[Y] == X) continue;
if(op == 2 && rig[Y] == X) continue;
int LX = lef[X],RX = rig[X],LY = lef[Y],RY = rig[Y];
if(op == 1) { Link(LX,RX); Link(LY,X); Link(X,Y); }
else if(op == 2){ Link(LX,RX); Link(Y,X); Link(X,RY); }
else {
if(rig[X] == Y) { Link(LX,Y); Link(Y,X); Link(X,RY); }
else { Link(LX,Y); Link(Y,RX); Link(LY,X); Link(X,RY); }//x,y本来不相邻
}
}
}
int Num = 0;
LL Ans = 0;
for(int i=1; i<=n; i++) {
Num = rig[Num];
if(i % 2 == 1) Ans += Num;
}
if(flag && n % 2 == 0) Ans = (LL)n*(n + 1)/2 - Ans;
printf("Case %d: %lld\n",++kase,Ans);
}
return 0;
}