UVa12657 - Boxes in a Line(数组模拟链表)
题目大意
你有一行盒子,从左到右依次编号为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(数组模拟链表)的更多相关文章
- 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 ...
- UVA11988-Broken Keyboard(数组模拟链表)
Problem UVA11988-Broken Keyboard Accept: 5642 Submit: 34937 Time Limit: 1000 mSec Problem Descripti ...
- 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 ...
- PAT 甲级 1052 Linked List Sorting (25 分)(数组模拟链表,没注意到不一定所有节点都在链表里)
1052 Linked List Sorting (25 分) A linked list consists of a series of structures, which are not ne ...
- 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 ...
- UVA12657 Boxes in a Line:题解
题目链接:https://www.luogu.org/problemnew/show/UVA12657 分析: 此题使用手写链表+模拟即可.(其实可以用list,而且更简便,但是会大大的超时) 肯定是 ...
- 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 ...
- 天梯赛 L2-022. (数组模拟链表) 重排链表
题目链接 题目描述 给定一个单链表 L1→L2→...→Ln-1→Ln,请编写程序将链表重新排列为 Ln→L1→Ln-1→L2→....例如:给定L为1→2→3→4→5→6,则输出应该为6→1→5→2 ...
- CSUOJ 1329 一行盒子(数组模拟链表)
题目:id=1329">http://acm.csu.edu.cn/OnlineJudge/problem.php? id=1329 题意: watermark/2/text/aHR0 ...
随机推荐
- ipc$漏洞利用实例
一.什么是IPC$ IPC$(Internet Process Connection)是共享"命名管道"的资源,它是为了让进程间通信而开放的命名管道, 通过提供可信任的用户名和口令 ...
- 基于Struts2框架实现登录案例 之 使用Struts2标签库简化表单+继承ActionSupport完成输入交验
一,使用Struts2标签库简化表单 在文章[基于Struts2框架实现登录案例]的基础上,通过使用Struts标签库可以简化登录页面login2.jsp <%@ page language=& ...
- Android yyyymmdd转成yyyy-MM-dd格式
//把yyyymmdd转成yyyy-MM-dd格式 public static String formatDate(String str){ SimpleDateFormat sf1 = new Si ...
- ubuntu服务器/home/分区替换3T硬盘
一,关机,将硬盘连接到主机. 二,开机 1,查看/dev/ 是否有多余的sdb 注,sda,sdb可以直接看做一整块硬盘. 而sda1,sda2即sda硬盘上的分区. 2,执行fdisk –l,确保添 ...
- PCL—低层次视觉—点云滤波(基于点云频率)
1.点云的频率 今天在阅读分割有关的文献时,惊喜的发现,点云和图像一样,有可能也存在频率的概念.但这个概念并未在文献中出现也未被使用,谨在本博文中滥用一下“高频”一词.点云表达的是三维空间中的一种信息 ...
- 基于Jquery+Ajax+Json+高效分页
摘要 分页我相信大家存储过程分页已经很熟悉了,ajax更是耳熟能详了,更别说我们的json,等等. 如果说您没用过这些东东的话,我相信看完这篇博文会对您有帮助的,,如果有任何问题不懂或者有bug没问题 ...
- Ubuntu中MySQL中文乱码解决
1.以root登陆,在终端输入命令 sudo gedit /etc/mysql/my.cnf在打开的文件中找到[client]在下面加入default-character-set=utf8 找到 [m ...
- 连续多行输入--C++ 中字符串标准输入的学习及实验(续篇)
编程中常常会用到连续多行输入的情况,如果事先知道要输入多少行的话,可以直接定义一个变量,然后用循环就可以实现了,但有时候事先并不知道,要输入多少行,于是就可以用到输入流碰到文件终止符的情况了,具体 ...
- ACM刷题常用链接
武汉科技大学 http://acm.wust.edu.cn/ 华中科技大学 http://acm.hust.edu.cn/vjudge/toIndex.action 杭州电子科技大学 http:/ ...
- 宏buf_pool_t
typedef struct buf_pool_struct buf_pool_t; struct buf_pool_struct{ /** @name General fields */ /* @{ ...