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 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

题意  開始有n个盒子按1到n的顺序排列  对这些盒子进行m次操作  每次为把x移到y的左边  右边  交换x,y 颠倒顺序中的一个

求操作完毕后全部奇数位原盒子序号的和;

直接模拟肯定会超时  用stl中的链表也超时  仅仅能用数组自己模拟一个双向链表了   le[i],ri[i]分别表示第i个盒子左边盒子的序号和右边盒子的序号  代码中有凝视

#include<cstdio>
#include<cstring>
using namespace std;
const int N = 100005;
int le[N], ri[N], n, m;
typedef long long ll; void link (int l, int r) //连接l和r。l在左边
{
le[r] = l; ri[l] = r;
} int main()
{
int cas = 0, op, x, y, t;
while (scanf ("%d%d", &n, &m) != EOF)
{
for (int i = 1; i <= n; ++i)
ri[i] = i + 1, le[i] = i - 1;
ri[n] = 0, le[0] = n, ri[0] = 1;
int flag = 0; //推断是否翻转
while (m--)
{
scanf ("%d", &op);
if (op == 4) flag = !flag;
else
{
scanf ("%d%d", &x, &y);
if (flag && op != 3) op = 3 - op; //翻转后移动操作就相反了
if (ri[y] == x && op == 3) //方便后面推断交换是否相邻
t = x, x = y, y = t;
if ( (op == 1 && le[y] == x) || (op == 2 && ri[y] == x)) continue; if (op == 1) //x移到y右边
link (le[x], ri[x]), link (le[y], x), link (x, y);
else if (op == 2) //x移到y左边
link (le[x], ri[x]), link (x, ri[y]), link (y, x);
else if (y == ri[x]) //op==3&&x,y相邻
link (le[x], y), link (x, ri[y]), link (y, x);
else //不相邻
{
int ry = ri[y], ly = le[y];
link (le[x], y), link (y, ri[x]), link (ly, x), link (x, ry);
}
}
} t = 0; ll ans = 0;
for (int i = 1; i <= n; ++i)
{
t = ri[t];
if (i % 2) ans += t;
}
if (n % 2 == 0 && flag) //n为偶数且翻转过 故求的恰为偶数位的和
ans = (ll) n / 2 * (1 + n) - ans;
printf ("Case %d: %lld\n", ++cas, ans);
}
return 0;
}

版权声明:本文博主原创文章。博客,未经同意不得转载。

UVa 12657 Boxes in a Line(应用双链表)的更多相关文章

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

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

  2. UVA 12657 Boxes in a Line 双向链表

    题目连接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=47066 利用链表换位置时间复杂度为1的优越性,同时也考虑到使用实际 ...

  3. UVA 12657 Boxes in a Line

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

  4. UVA 12657 Boxes in a Line(双向链表+小技巧)

    题意:对于一行按照顺序排列盒子数字与位置都为 1,2,3,4....n 执行四种操作 c = 1    x 放到 y 的左边 c =2     x 放到 y 的右边 c =3 交换 x, y c =4 ...

  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. UVa12657 - Boxes in a Line(数组模拟链表)

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

  7. 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 ...

  8. 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 ...

  9. Problem UVA12657-Boxes in a Line(数组模拟双链表)

    Problem UVA12657-Boxes in a Line Accept: 725  Submit: 9255 Time Limit: 1000 mSec Problem Description ...

随机推荐

  1. Xvisor ARM32 启动分析

    Linux内核历史悠久,特性丰富,但是代码量庞大,各个子系统交叉繁琐.对于想要将操作系统内核各个特性研究一遍的人,有时候也只好"望Linux兴叹".Xvisor是一个较新的Type ...

  2. andriod first app-computer

    andriod first app-computer 个人信息:就读于燕大本科软件project专业 眼下大三; 本人博客:google搜索"cqs_2012"就可以; 个人爱好: ...

  3. 基于phonegap开发app的实践

    app开发告一段落.期间遇到不少问题,写篇文章记录一下. 为虾米要用phonegap 开发app,至少要考虑android和ios两个版本号吧,android偶能够应付,ios表示全然木有接触过.于是 ...

  4. Sql Server 删除所有表 脚本

    如果由于外键约束删除table失败,则先删除所有约束: --/第1步**********删除所有表的外键约束*************************/ DECLARE c1 cursor f ...

  5. ArcGIS Spatial Query

    Creates a spatial query which performs a spatial search for features in the supplied feature class a ...

  6. GridView在PopWindow中OnItemClick不响应

    在安卓4.0系统中,GridView在PopWindow中OnItemClick不响应,但是有按压效果.需要PopWindow的Show时加入这行代码: popupWindow.setFocusabl ...

  7. centos-mirrors

    http://mirrors.aliyun.com/centos/7.2.1511/os/x86_64/Packages/ http://mirrors.aliyun.com/centos/7.2.1 ...

  8. MySQL的表空间管理

    表空间: MySQL没有真正意义上的表空间管理. MySQL的Innodb包含两种表空间文件模式,默认的共享表空间和每个表分离的独立表空间. 一般来说,当数据量很小的时候建议使用共享表空间的管理方式. ...

  9. 小强的HTML5移动开发之路(44)——JqueryMobile中的按钮

    一.链接按钮 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <ti ...

  10. VNC不同用户 Oracle

    VNC登录用户缺省是root,但在安装oracle时必须用oracle用户的身份登录,下面我们就以oracle为例说明如何配置VNC,从而可以使用不同的用户登录到主机.步骤描述如下:    步骤一:修 ...