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. Weex-进阶笔记二

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Helvetica; color: #945200 } p.p2 { margin: 0.0p ...

  2. Python朝花夕拾

    Q1:HTTP Error 403: Forbidden python中经常使用urllib2.urlopen函数提取网页源码,但是有些时候这个函数返回的却是:HTTP Error 403: Forb ...

  3. android 5.0 -- Ripple 效果

    Ripple 水波纹效果,也就是涟漪效果. 波纹效果有两种: 1,波纹有边界:波纹涟漪效果只是显示在控件内部 android:background="?android:attr/select ...

  4. 如何使用Java、Servlet创建二维码

    归功于智能手机,QR码逐渐成为主流,它们正变得越来越有用.从候车亭.产品包装.家装卖场.汽车到很多网站,都在自己的网页集成QR码,让人们快速找到它们.随着智能手机的用户量日益增长,二维码的使用正在呈指 ...

  5. Java中的DateFormatter

    字母 日期或时间元素 表示 示例 G Era 标志符 Text AD y 年 Year 1996; 96 M 年中的月份 Month July; Jul;07 w 年中的周数 Number 27 W ...

  6. Node.js:回调函数

    概要:本篇博客主要通过对比node.js异步与同步方式读取文件的方式来解释node中回调函数的作用. 1.异步方式读取文件: 新建一个文本文档用于测试,如下图: 代码如下: // node异步方式读取 ...

  7. POJ 2599 A funny game#树形SG(DFS实现)

    http://poj.org/problem?id=2599 #include<iostream> #include<cstdio> #include<cstring&g ...

  8. CentOS安装Ruby组件

    ruby安装#安装ruby组件yum install ruby ruby-irb ruby-devel rubygems rpm-build -y#安装Apache服务器yum install htt ...

  9. delphi中ShellExecute使用详解

    http://jingyan.baidu.com/article/ae97a646ae00a2bbfd461d38.html 百度的讲解 http://www.cnblogs.com/del/arch ...

  10. 正规消息发送器-- ESFramework 4.0 进阶(06)

    在ESFramework 4.0 进阶(04)-- 驱动力:通信引擎(下)一文末尾我们已经将通信引擎以及整个消息骨架流程组装起来了,只要通信引擎一接收到消息,框架就会按照规定的流程进行运转.到这里,自 ...