You have n boxes in a line on the table numbered 1 . . . n from left to right. Your task is to simulate 4kinds 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 executing2 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 nfrom 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>
using namespace std; const int size = 100000 + 5; void Link(int L, int R, int* right, int*left)
{
right[L] = R;
left[R] = L;
} void op1(int X, int Y, int* right, int*left) //操作一
{
int lx = left[X];
int rx = right[X];
int ly = left[Y];
Link(X, Y, right, left);
Link(ly, X, right, left);
Link(lx, rx, right, left);
} void op2(int X, int Y, int* right, int*left) //操作二
{
int lx = left[X];
int rx = right[X];
int ry = right[Y];
Link(Y, X, right, left);
Link(X, ry, right, left);
Link(lx, rx, right, left);
} void op3(int X, int Y, int* right, int*left) //操作三
{
int lx = left[X];
int rx = right[X];
int ly = left[Y];
int ry = right[Y];
Link(X, ry, right, left);
Link(ly, X, right, left);
Link(Y, rx, right, left);
Link(lx, Y, right, left);
} int main()
{
int right[size] = {0};
int left[size] = {0}; int n, m, kcase = 0;
while (cin >> n >> m)
{
//初始化
for (int i = 1; i <= n; i++)
{
left[i] = i - 1;
right[i] = i + 1;
}
left[0] = n;
right[0] = 0;
int op, X, Y, inv = 0; //inv是一个操作,如果进行了操作就变为1 while (m--)
{
cin >> op;
if (op == 4)inv = 1;
else
{
cin >> X >> Y;
if (op == 3 && right[Y] == X)
{
int rx = right[X];
int ly = left[Y];
Link(ly, X, right, left);
Link(Y, rx, right, left);
Link(X, Y, right, left);
}
else if (op == 3 && right[X] == Y)
{
int lx = left[X];
int ry = right[Y];
Link(X, ry, right, left);
Link(lx, Y, right, left);
Link(Y, X, right, left);
}
else if (op == 3 && right[X] != Y&&right[Y] != X)
{
op3(X, Y, right, left);
}
else if (op == 1 && inv)op2(X, Y, right, left);
else if (op == 2 && inv)op1(X, Y, right, left);
else if (op == 1 && X == left[Y])continue;
else if (op == 2 && Y == right[X])continue;
else if (op == 1 && !inv)op1(X, Y, right, left);
else if (op == 2 && !inv)op2(X, Y, right, left);
}
}
int b = 0;
long long result = 0;
for (int i = 1; i <= n; i++)
{
b = right[b];
if (i % 2 != 0)result += b;
}
if (inv&&n % 2 == 0)result = (long long)n*(n + 1) / 2 - result;
cout << "Case " << ++kcase << ": " << result << endl;
} return 0;
}

**如果数据结构上的某个操作很耗时,有时可以用加标记的方式处理,而不需真的执行那个操作,但同时,该数据结构的所有其他操作都要考虑这个标记。

Boxes in a Line(移动盒子)的更多相关文章

  1. Boxes in a Line

    Boxes in a Line You have n boxes in a line on the table numbered 1 . . . n from left to right. Your ...

  2. UVa 12657 Boxes in a Line(应用双链表)

    Boxes in a Line You have n boxes in a line on the table numbered 1 . . . n from left to right. Your ...

  3. Problem B Boxes in a Line

     省赛B题....手写链表..其实很简单的.... 比赛时太急了,各种手残....没搞出来....要不然就有金了...注:对相邻的元素需要特判..... Problem B Boxes in a Li ...

  4. uva-12657 - Boxes in a Line(双向链表)

    12657 - Boxes in a Line You have n boxes in a line on the table numbered 1 . . . n from left to righ ...

  5. C - Boxes in a Line 数组模拟链表

    You have n boxes in a line on the table numbered 1 . . . n from left to right. Your task is to simul ...

  6. Boxes in a Line UVA - 12657

      You have n boxes in a line on the table numbered 1...n from left to right. Your task is to simulat ...

  7. UVa12657 - Boxes in a Line(数组模拟链表)

    题目大意 你有一行盒子,从左到右依次编号为1, 2, 3,…, n.你可以执行四种指令: 1 X Y表示把盒子X移动到盒子Y左边(如果X已经在Y的左边则忽略此指令).2 X Y表示把盒子X移动到盒子Y ...

  8. UVA 12657 Boxes in a Line

    双向链表 注意:如果算法是最后处理翻转情况时,注意指令4翻转后1,2两个指令也要翻转处理: 指令3 中交换盒子要注意两个盒子相邻的情况 #include <iostream> #inclu ...

  9. UVa 12657 Boxes in a Line(数组模拟双链表)

    题目链接 /* 问题 将一排盒子经过一系列的操作后,计算并输出奇数位置上的盒子标号之和 解题思路 由于数据范围很大,直接数组模拟会超时,所以采用数组模拟的链表,left[i]和right[i]分别表示 ...

随机推荐

  1. 转发一个javascript的编码规范

    google出品:http://chajn.org/jsguide/javascriptguide.html

  2. 描述符和property内建函数

    首先我们搞清楚__getattr__ ,__get__ 和 __getattribute__ 作用的不同点. __getattr__在授权中会用到. __getattribute__  当要访问属性时 ...

  3. Github朝花夕拾

    删除fork的项目   下载指定revision的repository 通过git log查看提交历史,最好是GUI查看 然后执行命令git reset –hard <sha1> 同步到最 ...

  4. Linux下各类压缩文件命令小结

    .tar 解包:tar xvf FileName.tar    解包后原始文件仍存在 打包:tar cvf FileName.tar DirName1 Filename1 - 列出内容:tar tvf ...

  5. flume-hdfs sinks报错

    flume使用hdfs sinks时候报错:org.apache.flume.EventDeliveryException: java.lang.NullPointerException: Expec ...

  6. 触发器实现对插入数据的字段更改 Oracle+SQL Server

    最近有个使用触发器实现对插入数据的某个列做更改的需求,因此整理了Oracle和SQL Server对于此类需求的触发器写法,本文仅提到了Insert触发器. 首先我们创建一张表: --创建Test表 ...

  7. iOS通过URL Scheme启动app(收集了常用的app的URL Scheme)

    URL Scheme不仅可以打开其他的app,当然你也能为自己的app设置URL Scheme 1.如何设置给app设置URL Scheme 添加URL types字段数组 设置URL identif ...

  8. win32api大全

    win32api大全 http://files.cnblogs.com/files/landv/Win32API%E5%A4%A7%E5%85%A8.zip

  9. mobiscroll 插件札记(一)

    mobiscroll 插件笔记(一) 文章参照  http://www.cnblogs.com/headwolf/archive/2013/12/23/3487207.html 最近切一个移动页面,需 ...

  10. Docker私有仓库2

    http://www.cnblogs.com/womars/p/5906410.html 接着上篇,上面为上篇地址. #通过docker tag将该镜像标志为要推送到私有仓库 [root@lh- ~] ...