题目大意

你有一行盒子,从左到右依次编号为1, 2, 3,…, n。你可以执行四种指令:

1 X Y表示把盒子X移动到盒子Y左边(如果X已经在Y的左边则忽略此指令)。
2 X Y表示把盒子X移动到盒子Y右边(如果X已经在Y的右边则忽略此指令)。
3 X Y表示交换盒子X和Y的位置。
4 表示反转整条链。

盒子个数n和指令条数m(1<=n,m<=100,000)

题解

用数组来模拟链表操作,对于每个节点设置一个前驱和后继。

1操作是把x的前驱节点和x的后继节点连接,y节点的前驱和x节点连接,x节点和y节点连接。

2,3,的做法和1差不多

4操作由于操作两次就等于没有操作,所以只要判断它最终是不是执行了奇数次,如果是就把n个节点的前驱和后继交换下。还有就是在执行1,2的时候如果之前4操作了奇数次,那么1,2两个执行的操作分别是2操作和1操作。

代码:

 #include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <utility>
#include <vector>
#include <queue>
using namespace std;
#define INF 0x3f3f3f3f
#define maxn 111111
typedef long long LL;
int nxt[maxn], pre[maxn];
void init(int n)
{
for (int i = ; i <= n;i++)
{
pre[i] = i - ;
nxt[i] = i + ;
}
nxt[n] = ;
nxt[] = ; pre[] = n;
}
void link(int l, int r)
{
pre[r] = l; nxt[l] = r;
}
int main()
{
int n, m,kase=;
while (scanf("%d%d", &n, &m) != EOF)
{
int rev = ;
init(n);
while (m--)
{
int op, x, y;
scanf("%d", &op);
if (op == ) rev = - rev;
else
{
scanf("%d%d", &x, &y);
if ((op == || op == ) && rev) op = - op;
if (op == &&nxt[y] == x) swap(x, y);
int lx, rx, ly, ry;
lx = pre[x]; rx = nxt[x];
ly = pre[y]; ry = nxt[y];
if (op == )
{
if (nxt[x]==y) continue;
link(lx, rx);
link(ly, x);
link(x, y); }
else if (op == )
{
if (nxt[y]==x) continue;
link(lx, rx);
link(x, ry);
link(y, x);
}
else
{
if (nxt[x] == y)
{
link(lx, y);
link(y, x);
link(x, ry);
}
else
{
link(lx, y);
link(y, rx);
link(ly, x);
link(x, ry);
}
}
}
}
if (rev)
{
for (int i = ; i <= n; i++) swap(pre[i], nxt[i]);
}
int pos;
for (int i = ; i <= n; i++)
{
if (pre[i] == )
{
pos = i;
break;
}
}
int cnt = ;
LL ans = ;
while (pos!=)
{
if (cnt & ) ans += pos;
pos = nxt[pos];
cnt++;
}
printf("Case %d: %I64d\n", ++kase,ans);
}
return ;
}

UVa12657 - Boxes in a Line(数组模拟链表)的更多相关文章

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

  2. UVA11988-Broken Keyboard(数组模拟链表)

    Problem UVA11988-Broken Keyboard Accept: 5642  Submit: 34937 Time Limit: 1000 mSec Problem Descripti ...

  3. B - Broken Keyboard (a.k.a. Beiju Text) 数组模拟链表

    You're typing a long text with a broken keyboard. Well it's not so badly broken. The only problem wi ...

  4. PAT 甲级 1052 Linked List Sorting (25 分)(数组模拟链表,没注意到不一定所有节点都在链表里)

    1052 Linked List Sorting (25 分)   A linked list consists of a series of structures, which are not ne ...

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

  6. UVA12657 Boxes in a Line:题解

    题目链接:https://www.luogu.org/problemnew/show/UVA12657 分析: 此题使用手写链表+模拟即可.(其实可以用list,而且更简便,但是会大大的超时) 肯定是 ...

  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. 天梯赛 L2-022. (数组模拟链表) 重排链表

    题目链接 题目描述 给定一个单链表 L1→L2→...→Ln-1→Ln,请编写程序将链表重新排列为 Ln→L1→Ln-1→L2→....例如:给定L为1→2→3→4→5→6,则输出应该为6→1→5→2 ...

  9. CSUOJ 1329 一行盒子(数组模拟链表)

    题目:id=1329">http://acm.csu.edu.cn/OnlineJudge/problem.php? id=1329 题意: watermark/2/text/aHR0 ...

随机推荐

  1. Maven中心仓库

    当你使用Maven构建一个项目,Maven会检查你的pom.xml文件,找出需要下载的依赖包.首先它会到本地仓库查找所需的文件,如果没找到,就到默认的中心仓库(这是新的http://search.ma ...

  2. 转:linux下面/usr/local和opt目录有何区别

    /usr/local下一般是你安装软件的目录,这个目录就相当于在windows下的programefiles这个目录 /opt这个目录是一些大型软件的安装目录,或者是一些服务程序的安装目录 /opt ...

  3. css link和@import区别用法

    这里link与@import介绍的是html引入css的语法单词.两者均是引入css到html的单词. 1.link语法结构<link rel="stylesheet" ty ...

  4. Mac系统在终端中查看CPU信息的命令

    在mac os x的终端中以命令行的形式查看本机cpu信息: sysctl -n machdep.cpu.brand_string E.G. lis-mbp:Home jenkins$ sysctl ...

  5. Android ContentProvider和Uri详解 (绝对全面)

        ContentProvider的基本概念 : 1.ContentProvider为存储和读取数据提供了统一的接口 2.使用ContentProvider,应用程序可以实现数据共享 3.andr ...

  6. Windows下免费、开源邮件服务器hMailServer

    Windows下免费.开源邮件服务器hMailServer 一.Windows下搭建免费.开源的邮件服务器hMailServer 二.邮件服务器hMailServer管理工具hMailServer A ...

  7. HDU 1358 (所有前缀中的周期串) Period

    题意: 给出一个字符串,在所有长度大于1的前缀中,求所有的周期至少为2的周期串,并输出一个周期的长度以及周期的次数. 分析: 有了上一题 HDU 3746 的铺垫,这道题就很容易解决了 把next求出 ...

  8. [反汇编练习] 160个CrackMe之007

    [反汇编练习] 160个CrackMe之007. 本系列文章的目的是从一个没有任何经验的新手的角度(其实就是我自己),一步步尝试将160个CrackMe全部破解,如果可以,通过任何方式写出一个类似于注 ...

  9. POJ 3352 Road Construction(边双连通分量,桥,tarjan)

    题解转自http://blog.csdn.net/lyy289065406/article/details/6762370   文中部分思路或定义模糊,重写的红色部分为修改过的. 大致题意: 某个企业 ...

  10. BMP图像格式

    BMP(全称Bitmap)是Window操作系统中的标准图像文件格式,可以分成两类:设备相关位图(DDB)和设备无关位图(DIB),使用非常广.它采用位映射存储格式,除了图像深度可选以外,不采用其他任 ...